更改ggplot中的条形顺序

时间:2019-02-03 17:41:15

标签: r ggplot2

我画了一个小图,它几乎运行良好。但是,关于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

1 个答案:

答案 0 :(得分:1)

  

我画了一个小图,它几乎运行良好。但是,关于x轴的描述与沿着x轴在每个刻度上方绘制的一对条形图不一致。因此,勾号“动机问题”上绘制的两个条实际上属于勾号“没有问题”。此外,在刻度“知识相关问题”上绘制的两条线实际上属于刻度“动机问题”。此外,刻度“两个问题”上的两个条实际上属于刻度“没有问题”。

我可能是错的,但我怀疑标签与条​​形的错误关联可能与您使用的R和ggplot的版本有关。对于R 3.5.2和ggplot2 3.1.0,使用您的代码不会发生这种情况。

  

有人可以帮助我更正我的代码吗?我想要以下顺序的酒吧:没有问题,动机问题,与知识相关的问题,都是问题。

正如dww在他的评论中指出的那样,现有帖子解释了如何订购这些酒吧。

尽管如此,以下显示了两种以所需方式排列条形的方法。第一个对问题的因数进行重新排序,第二个对limit_x_discrete使用限制参数。如果您使用第二种方法,您显然会希望将顺序更改为您要求的顺序。

来自帮助(scale_x_discrete):

  

limits一个字符向量,定义了可能的                 规模及其顺序。

#!/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