如何增加ggplot2中分组条形之间的间隔? [英] How to increase the space between grouped bars in ggplot2?

查看:1405
本文介绍了如何增加ggplot2中分组条形之间的间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[用于在帖子末尾生成情节的数据和代码]

[Data and code to produce plot at the end of the post]

使用ggplot,我绘制了带有误差线的条形图,条形图由两个因素分组(一个在X轴上,一个在填充). 我想增加x轴上各组之间的绿色距离,以使绘图更易于阅读:

Using ggplot, I plotted a bargraph with error bars, bars are grouped by two factors (one on X axis, one with fill). I would like to increase the green distance between the groups on the xaxis to make the plot easier to read:

我在此处找到了最接近解决方案的东西(其中有人在未回答的评论中问我的问题),此处

The closest thing to a solution on stackoverflow I found here (where someone asked my question in an unanswered comment), here, here, but I didn't manage to apply these without massing up the error bars. Can someone point me to the right parameter to tweak?

数据:

structure(list(Condition = c("Difficult", "Easy", "Difficult", 
"Easy", "Difficult", "Easy", "Difficult", "Easy", "Easy", "Difficult", 
"Easy", "Difficult"), Measure = c("Competence", "Competence", 
"Value", "Value", "Interest", "Interest", "JOL", "JOL", "Difficulty", 
"Difficulty", "Effort", "Effort"), mean = c(5.5, 4.72, 4.04, 
5.39, 3.51, 3.77, 4.34, 4.61, 3.51, 1.51, 3.44, 1.73), sd = c(1.26, 
1.62, 1.94, 1.34, 1.46, 1.46, 1.73, 1.68, 1.5, 0.86, 1.53, 1.1
), se = c(0.14, 0.18, 0.22, 0.15, 0.16, 0.16, 0.19, 0.19, 0.17, 
0.1, 0.17, 0.12), s.size = c(80, 80, 80, 80, 80, 80, 80, 80, 
80, 80, 80, 80)), .Names = c("Condition", "Measure", "mean", 
"sd", "se", "s.size"), row.names = c(NA, -12L), class = "data.frame")

这是:

   Condition    Measure mean   sd   se s.size
1  Difficult Competence 5.50 1.26 0.14     80
2       Easy Competence 4.72 1.62 0.18     80
3  Difficult      Value 4.04 1.94 0.22     80
4       Easy      Value 5.39 1.34 0.15     80
5  Difficult   Interest 3.51 1.46 0.16     80
6       Easy   Interest 3.77 1.46 0.16     80
7  Difficult        JOL 4.34 1.73 0.19     80
8       Easy        JOL 4.61 1.68 0.19     80
9       Easy Difficulty 3.51 1.50 0.17     80
10 Difficult Difficulty 1.51 0.86 0.10     80
11      Easy     Effort 3.44 1.53 0.17     80
12 Difficult     Effort 1.73 1.10 0.12     80

我用来绘制情节的代码(对不起,我正在学习如何使用ggplot并发现做笔记会有所帮助)

Code that I used to make the plot (excuse the comments, I'm learning how to use ggplot and find it helpful to take notes)

library(ggplot2)
ggplot(DF, aes(x=Measure, y=mean,fill=Condition)) + 
  geom_bar(stat="identity",
           colour="black",    # Black outline for all
           position=position_dodge())+# Put bars side-by-side instead of stacked
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
                position=position_dodge(.9), 
                width=.25)+
  #order the groups on the xaxis
  scale_x_discrete(limits = c("Interest", "Value","Effort","Difficulty","Competence","JOL"))+
  coord_cartesian(ylim=c(0,7)) +
  #change color of bars
  scale_fill_manual(values=c("#ffcc00ff","#ffffff"), name = "Condition") + 
  #change ticks on yaxis
  scale_y_continuous(breaks=seq(0,7,by =1)) + 
  geom_hline(yintercept=0) +
  geom_vline(xintercept=0)+
  theme_bw()+
  labs(x="", y = "Rating (0-7)")+
  theme(axis.line.y = element_line(color="black"),
        axis.title.y = element_text(margin = margin(r=8)),
        axis.title.x = element_text(margin = margin(r=25)),
        panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(),
        panel.border = element_blank())

推荐答案

关于什么? 1.建议使用geom_col代替geom_bar. 2.指定合适的position_dodge(0.5)width=0.5,然后3.删除不必要的代码.

What's about? 1. Use geom_col instead of geom_bar as recommended. 2. specify suitable position_dodge(0.5) and width=0.5 and 3. remove unnecessary code.

ggplot(d, aes(x=Measure, y=mean, fill=Condition)) + 
  geom_col(colour="black",width=0.5,    
           position=position_dodge(0.5)) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
                position=position_dodge(0.5), width=.25)+
  scale_x_discrete(limits = c("Interest", "Value","Effort","Difficulty","Competence","JOL")) +
  scale_y_continuous(breaks=seq(0,7,by =1),limits = c(0,7), expand = c(0,0))+
  scale_fill_manual(values=c("#ffcc00ff","#ffffff"), name = "Condition") + 
  labs(x="", y = "Rating (0-7)")+
  theme_minimal() +
  theme(axis.line = element_line(color="black"),
        axis.ticks = element_line(color="black"),
        panel.border = element_blank())

这篇关于如何增加ggplot2中分组条形之间的间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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