如何增加ggplot2中分组条之间的空间?

Posted

技术标签:

【中文标题】如何增加ggplot2中分组条之间的空间?【英文标题】:How to increase the space between grouped bars in ggplot2? 【发布时间】:2019-01-24 08:21:10 【问题描述】:

[在文章末尾生成绘图的数据和代码]

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

我在***上找到了最接近解决方案的here(有人在未回答的评论中问了我的问题)、here、here,但我没有设法在没有累积错误的情况下应用这些酒吧。有人可以指出我要调整的正确参数吗?

数据:

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

我用来制作情节的代码(请原谅 cmets,我正在学习如何使用 ggplot,发现做笔记很有帮助)

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

【问题讨论】:

这里可能有用的信息:***.com/questions/50077342/… 您可以在geom_bar 语句中使用position = position_dodge(0.5), width = 0.25 并在geom_errorbar 中调整position = position_dodge(0.5) 以及... 谢谢,但是 position_dodge 不起作用。这也会在黄条和白条之间插入一个空白,这是我不想要的。 【参考方案1】:

是怎么回事? 1. 建议使用geom_col 而不是geom_bar。 2. 指定合适的position_dodge(0.5)width=0.5 和 3. 删除不必要的代码。

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

【讨论】:

您能否解释一下为什么 geom_col 比 geom_bar 更好?非常感谢您花时间清理我的代码!!非常有助于更好地理解代码的哪一部分做什么,在我在学习时拆开和回收的所有示例中:)【参考方案2】:

感谢大家的思考和 AntoniosK 提供指向此 question 的链接,它帮助我找到了适合我的解决方案(虽然感觉有点笨拙): 手动更改条的宽度,调整主题中的纵横比(绘图的宽度与高度),调整误差条中的 position_dodge 以匹配条的宽度:

geom_bar(width = 0.7)
theme(aspect.ratio = 3/5)
geom_errorbars(position=position_dodge(.7))

(我还把图例移到了图的顶部,在截图中看不到)

【讨论】:

以上是关于如何增加ggplot2中分组条之间的空间?的主要内容,如果未能解决你的问题,请参考以下文章

如何使ggplot2中的可变条宽不重叠或间隙

R语言ggplot2可视化因子分组并排条形图柱状图可视化添加误差条:barplot with several variables side by side grouped by a factor

使用 ggplot2 将平均值添加到 R 中的分组箱形图

R语言ggplot2可视化增加轴标签(ticks)和轴标题(title)之间的距离实战

R语言ggplot2可视化:默认情况下ggplot2在x轴和y轴的刻度线和轴之间保留了一些空间设置ggplot2可视化去除可视化结果与坐标轴之间的空间可视化结果与坐标轴紧紧贴合,没有空白区域

ggplot2:逻辑回归 - 绘制概率和回归线