簡體   English   中英

如何在按 x 變量分組的條形圖上 plot geom_line 不填充變量?

[英]How to plot geom_line over bar chart grouped by x variable not fill variable?

我有一個數據框 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

我想用 plot 這個來說明組之間的平均增加沒有差異。 我嘗試了以下方法:

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

但我無法弄清楚如何在兩個條之間正確地獲得 plot 的線條,而不是在中心垂直,即使我調整 position_dodge 的“寬度”參數:

在此處輸入圖像描述

請任何人告訴我如何修復 plot?

您正在尋找position_dodge2() ggplot2 閃避參考中有一些關於它的內容,在 Github 上的實際代碼中有更多內容。 下面的相關部分,添加了一些重點:

閃避保留幾何圖形的垂直 position,同時調整水平 position。 position_dodge2position_dodge的一種特殊情況,用於排列可以有可變寬度的箱形圖。 position_dodge2也適用於條形和矩形。 但與position_dodge不同, position_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") 

代表 package (v0.3.0) 於 2019 年 11 月 21 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM