首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ggplot2中增加分组栏之间的空间?

如何在ggplot2中增加分组栏之间的空间?
EN

Stack Overflow用户
提问于 2018-08-17 09:57:47
回答 2查看 20.1K关注 0票数 16

数据和代码,以便在文章末尾生成绘图

我用ggg图绘制了一个带有错误条的条形图,条形图按两个因素分组(一个在X轴上,一个在填充上)。I希望增加xaxis上的组之间的绿色距离,以使图更容易阅读:

与堆栈溢出解决方案最接近的是,我找到了here (有人在未回答的评论中问了我的问题),herehere,但我无法在不聚集错误栏的情况下应用这些解决方案。有人能给我指个合适的参数来调整吗?

数据:

代码语言:javascript
复制
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")

即:

代码语言:javascript
复制
   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,并发现做笔记很有帮助)

代码语言:javascript
复制
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())
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-17 11:04:56

怎么样? 1.使用geom_col而不是推荐的geom_bar。2.指定合适的position_dodge(0.5)width=0.5;3.删除不必要的代码。

代码语言:javascript
复制
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())

票数 14
EN

Stack Overflow用户

发布于 2018-08-17 10:33:50

感谢大家的思考,感谢AntoniosK提供到这个question的链接,它帮助我找到了一个适合我的解决方案(尽管感觉有点烦人):手动更改条形图的宽度,调整主题的纵横比(情节的宽度与高度),调整错误条中的position_dodge以匹配条形图的宽度:

代码语言:javascript
复制
geom_bar(width = 0.7)
theme(aspect.ratio = 3/5)
geom_errorbars(position=position_dodge(.7))

(我还把传说移到了情节的顶端,在截图中看不见)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51892875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档