更改ggplot中条的顺序 [英] Change order of bars in ggplot

查看:50
本文介绍了更改ggplot中条的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 策略类型<-c(认知",认知",认知",认知",动机",动机",动机",动机")问题<-c(没有问题",动机问题",与知识相关的问题",两个问题",没有问题",动机问题",与知识相关的问题",两个问题")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(策略类型,问题,len,sd)df <-as.data.frame(df)df $ Problem<-级别(df $ Problem)<-c(没有问题",动机问题",与知识相关的问题",两个问题",没有问题",动机问题",与知识相关的问题")df $ len<-as.numeric(df $ len)df $ sd<-as.numeric(df $ sd)len<-("Anzahl Strategytypen(KI 95%)")p<-ggplot(df,aes(x =问题,y = len,填充=策略类型))+geom_bar(stat ="identity",color ="black",position = position_dodge())+geom_errorbar(aes(ymin = len-sd,ymax = len + sd),宽度= .2,位置= position_dodge(.5))打印(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 =问题,y = len,fill =策略类型))+geom_bar(width = 0.5,stat ="identity",color ="black",位置= position_dodge())+scale_fill_manual(values = c('darkgrey','firebrick'))+geom_errorbar(aes(ymin = len-sd,ymax = len + sd),宽度= 0.2,位置= position_dodge(.5))打印(p)p +scale_x_discrete(breaks = c(没有问题",动机问题",与知识相关的问题",两个问题"),标签= c(没有问题",动机问题",与知识有关的\ n问题",两个问题"))+theme_classic()last_plot()+ ylab(安扎尔知识和动机\ n策略类型(KI 95%)")last_plot()+ xlab("Problemart") 

解决方案

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

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

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

正如

I drew up a barplot and it almost worked well. However, the description on the x axis does not go along with the pair of bars plotted above each tick along the x axis. So, the two bars plotted on the tick "Motivational Problems" actually belong to the tick "No Problems". Further, the two bars plotted on the tick "Knowledge related Problems" belong actually to the tick "Motivational Problems". Further, the two bars on the tick "Both Problems" actually belong to the tick No Problems.

Can someone help me correcting my code? I want the following order of bars: No problems, motivational problems, knowledge related problems, both problems.

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")

解决方案

I drew up a barplot and it almost worked well. However, the description on the x axis does not go along with the pair of bars plotted above each tick along the x axis. So, the two bars plotted on the tick "Motivational Problems" actually belong to the tick "No Problems". Further, the two bars plotted on the tick "Knowledge related Problems" belong actually to the tick "Motivational Problems". Further, the two bars on the tick "Both Problems" actually belong to the tick No Problems.

I may be wrong but I suspect the incorrect association of labels to bars that you see might be related to the version of R and ggplot you are using. With R 3.5.2 and ggplot2 3.1.0 that does not occur using your code.

Can someone help me correcting my code? I want the following order of bars: No problems, motivational problems, knowledge related problems, both problems.

As dww pointed out in his comment, there are existing posts that explain how to order the bars.

Nonetheless, the following shows two ways to get the bars ordered the way you want. The first reorders the factor for Problem and the second uses the limits parameter to scale_x_discrete. If you use the second method you'll obviously want to change the order to the order you requested.

From help(scale_x_discrete):

limits A character vector that defines possible values of the scale and their order.

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

这篇关于更改ggplot中条的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆