在ggplot条修改订单

问题描述 投票:-1回答:1

我画了一个barplot它几乎运作良好。然而,在x轴上的描述不与对杆去沿着沿x轴的每个蜱上述作图。因此,两家酒吧绘制在勾选“动机问题”实际上属于蜱“没有问题”。此外,两间酒吧上打钩“知识相关的问题”策划实际上属于蜱“动机问题”。此外,在勾选“这两个问题的”两个酒吧实际上属于蜱没有问题。

有人可以帮助我纠正我的代码?我想的吧顺序如下:没问题,动机问题,知识有关的问题,这两个问题。

Strategytype <- c("Cognitive", "Cognitive", "Cognitive", "Cognitive", 
                  "Motivational", "Motivational", "Motivational", "Motivational")
Problem <- c("No Problems", "Motivational Problems", "Knowledge related Problems", 
             "Both Problems", "No Problems", "Motivational Problems", 
             "Knowledge related Problems", "Both Problems")
len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df <- cbind(Strategytype, Problem, len, sd)
df <- as.data.frame(df)

df$Problem <- levels(df$Problem) <- c("No Problems", "Motivational Problems", 
                                      "Knowledge related Problems", 
                                      "Both Problems", "No Problems", 
                                      "Motivational Problems", 
                                      "Knowledge related Problems")
df$len <- as.numeric(df$len)
df$sd <- as.numeric(df$sd)

len <- ("Anzahl Strategytypen (KI 95%)")

p <- ggplot(df, aes(x = Problem, y = len, fill = Strategytype)) + 
  geom_bar(stat = "identity", color="black", position=position_dodge()) +
  geom_errorbar(aes(ymin = len-sd, ymax = len+sd), 
                width=.2, position = position_dodge(.5)) 
print(p)

df$len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
df$sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df$len <- as.numeric(df$len)
df$sd <- as.numeric(df$sd)

p <- ggplot(df, aes(x=Problem, y=len, fill=Strategytype)) + 
  geom_bar(width = 0.5, stat = "identity", color = "black", 
           position = position_dodge()) +
  scale_fill_manual(values = c('darkgrey', 'firebrick'))+
  geom_errorbar(aes(ymin = len-sd, ymax = len+sd), 
                width = .2, position = position_dodge(.5)) 
print(p)

p + 
  scale_x_discrete(breaks = c("No Problems", "Motivational Problems", 
                              "Knowledge related Problems", "Both Problems"),
                   labels = c("No Problems", "Motivational Problems", 
                              "Knowledge related \n Problems", "Both Problems")) + 
  theme_classic()

last_plot() + ylab("Anzahl kognititver und motivationaler\n Strategytypeen (KI 95%)")

last_plot() + xlab("Problemart")

enter image description here

r ggplot2
1个回答
1
投票

我画了一个barplot它几乎运作良好。然而,在x轴上的描述不与对杆去沿着沿x轴的每个蜱上述作图。因此,两家酒吧绘制在勾选“动机问题”实际上属于蜱“没有问题”。此外,两间酒吧上打钩“知识相关的问题”策划实际上属于蜱“动机问题”。此外,在勾选“这两个问题的”两个酒吧实际上属于蜱没有问题。

我可能是错的,但我怀疑的标签给你看可能与R的版本和ggplot您正在使用的酒吧不正确的关联。有R 3.5.2和3.1.0 GGPLOT2不会发生使用你的代码。

有人可以帮助我纠正我的代码?我想的吧顺序如下:没问题,动机问题,知识有关的问题,这两个问题。

作为dww在他的评论中指出,有一些解释如何订购吧现有职位。

尽管如此,下面显示了两种方式来获得条命令你想要的方式。第一重新排序系数问题和第二个使用的限制参数,以scale_x_discrete。如果使用第二种方法,你会明显地要更改为你所要求的订单。

从帮助(scale_x_discrete):

限制,其限定的规模和它们的顺序的可能值的特征向量。

#!/usr/bin/env Rscript                                                                                                                                                                                           

library(ggplot2)
library(stringr)
library(cowplot)

Strategytype <- c("Cognitive", "Cognitive", "Cognitive", "Cognitive",
                  "Motivational", "Motivational", "Motivational", "Motivational")

Problem <- c("No Problems", "Motivational Problems", "Knowledge related Problems",
             "Both Problems", "No Problems", "Motivational Problems",
             "Knowledge related Problems", "Both Problems")

len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df <- data.frame(Strategytype, Problem, len, sd)

g <- ggdraw()

# reorder bars by explicitly ordering factor levels                                                                                                                                                              

x.ordered <- factor(Problem, levels=c("No Problems", "Motivational Problems",
                                      "Knowledge related Problems", "Both Problems"))

p <- ggplot(df, aes(x=x.ordered, y=len, fill=Strategytype)) +
    geom_bar(width = 0.5, stat = "identity", color = "black",
             position = position_dodge()) +
    scale_fill_manual(values = c('darkgrey', 'firebrick')) +
    geom_errorbar(aes(ymin = len-sd, ymax = len+sd),
                  width = 0.2, position = position_dodge(0.5)) +
    labs(x="Problemart", y="Anzahl Strategytypen (KI 95%)") +
    theme_classic()

p1 <- p + scale_x_discrete(labels = function(x) str_wrap(x, width = 10))

# reorder bars using scale_x_discrete limits. See help(scale_x_discrete)                                                                                                                                         

p2 <- p +
    scale_x_discrete(limits = c("Motivational Problems", "No Problems",
                                "Knowledge related Problems", "Both Problems"),
                     labels = function(x) str_wrap(x, width = 10))

# draw plots                                                                                                                                                                                                     

g <- g + draw_plot(p1, 0, 0, 1, 0.5)
g <- g + draw_plot(p2, 0, 0.5, 1, 0.5)

print(g)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.