首页 > 解决方案 > 如何在按 x 变量而不是填充变量分组的条形图上绘制 geom_line?

问题描述

我有一个数据框 df

  Group Time_Period     mean      uci      lci
1     A      Before 4.712195 5.054539 4.369852
2     A       After 5.881463 6.241784 5.521142
3     B      Before 5.349754 5.872940 4.826567
4     B       After 6.653595 7.246231 6.060959

我想绘制这个图来说明组间的平均增长没有差异。我尝试了以下方法:

ggplot(df, aes(x=Time_Period, y=mean, fill=Group)) +
  geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
  geom_point(position = position_dodge(width = 1))+
  geom_line(aes(group=Group, color=Group), color=c("cyan4","firebrick","cyan4","firebrick"), size =1, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
  theme_bw() +
  scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
  theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
  theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
  labs(y="Sales", x="Time Period", fill="Category") +
  theme(axis.text.x = element_text(face="bold", size=12)) +
  theme(axis.text.y = element_text(face="bold", size=12)) +
  theme(axis.title.x = element_text(face="bold", size=16)) +
  theme(axis.title.y = element_text(face="bold", size=16)) +
  theme(legend.text= element_text(face="bold", size=12)) +
  theme(legend.title= element_text(face="bold", size=16))

其中情节:

情节1

但是我的经理担心由于重叠而难以区分两条线,所以他告诉我重新排列列,使 x 是 Group,fill 是 Time Period。

我尝试了以下方法:

ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
  geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
  geom_point(position = position_dodge(width = 1))+
  geom_line(aes(group=Group), color="black", size =1, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
  theme_bw() +
  scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
  theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
  theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
  labs(y="Sales", x="Group", fill="Time Period") +
  theme(axis.text.x = element_text(face="bold", size=12)) +
  theme(axis.text.y = element_text(face="bold", size=12)) +
  theme(axis.title.x = element_text(face="bold", size=16)) +
  theme(axis.title.y = element_text(face="bold", size=16)) +
  theme(legend.text= element_text(face="bold", size=12)) +
  theme(legend.title= element_text(face="bold", size=16))

但我无法弄清楚如何让线条在两个条之间正确绘制,而不是在中心垂直绘制,即使我调整了 position_dodge 的“宽度”参数:

在此处输入图像描述

请问有人可以告诉我如何解决这个情节吗?

标签: rggplot2geom-bar

解决方案


你正在寻找position_dodge2(). 在ggplot2 dodge 参考中有一些关于它的内容,在 Github 上的实际代码中有更多内容。下面的相关部分,添加了一些重点:

闪避在调整水平位置的同时保留几何图形的垂直位置。position_dodge2position_dodge 安排箱线图的一种特殊情况,箱线图可以有可变的宽度。position_dodge2 也适用于条形和矩形。但与 不同的是position_dodgeposition_dodge2在图层中没有分组变量的情况下工作。

所以这是代码,删除了一些主题。

library(tidyverse)
txt = "
Group Time_Period     mean      uci      lci
1     A      Before 4.712195 5.054539 4.369852
2     A       After 5.881463 6.241784 5.521142
3     B      Before 5.349754 5.872940 4.826567
4     B       After 6.653595 7.246231 6.060959"

df <- read.table(text = txt, header = TRUE) %>%
  mutate(Group = fct_relevel(Group, "A", "B")) %>%
  mutate(Time_Period = fct_relevel(Time_Period, "Before", "After"))

ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
  geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
  geom_point(position = position_dodge(width = 1))+
  geom_line(aes(group=Group), color="black", size =1, 
            position = position_dodge2(width = 1)) +
  geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
  theme_bw() +
  scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
  labs(y="Sales", x="Group", fill="Time Period") 

reprex 包(v0.3.0)于 2019 年 11 月 21 日创建


推荐阅读