簡體   English   中英

ggplot2:使用geom_bar中的填充指定顏色時缺少圖例

[英]ggplot2: Missing legend when specify color using fill in geom_bar

在堆疊的條形圖中,我嘗試為不同的條形指定顏色。 例如,治療組的漸變綠色和對照組的漸變藍色。 但是,指定顏色后,我丟失了圖例。 有沒有辦法添加圖例?

# Create data set
ID<-c(rep(1:4, 6))
Group<-c(rep(0,4), rep(1,4), rep(0,4), rep(1,4), rep(0,4), rep(1,4))
Time<-c(rep("Time 1",8), rep("Time 2",8), rep("Time 3",8))
Response<-c(1,2,3,1,2,3,1,2,2,3,3,1,1,3,2,1,2,1,3,1,2,2,1,3)

data <- data.frame(ID, Group, Time, Response)
data$Response<-as.factor(data$Response)

library(dplyr)
data1<-as.data.frame(
  data %>% 
    group_by(Group, Time, Response) %>%                     
    summarise(N= n()))

# Define the color for control and treatment groups
trtCol <- c("#3182bd", "#9ecae1", "#deebf7")
conCol <- c("#31a354", "#a1d99b", "#e5f5e0")
Palette<-c(conCol, trtCol, conCol, trtCol, conCol, trtCol)

library(ggplot2)
library(scales)

# Specify color in ggplot using "geom_bar (fill=Palette)"
ggplot(data1, aes(Group, N, fill = Response))+ 
  geom_bar(position = "fill",stat = "identity", fill=Palette) + 
  facet_wrap(~Time, strip.position = "bottom") +
  labs(title="Distribution of Responses by Groups over Time", 
       x="Time Points", y="Percentage")+
  scale_y_continuous(labels = percent_format()) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5), 
        axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        panel.spacing = unit(0.1, "lines"))+
  geom_text(aes(x=0,y=-0.05,label="Control\nGroup"), size=3.5)+
  geom_text(aes(x=1,y=-0.05,label="Treatment\nGroup"), size=3.5)

當我為每個條指定顏色時,圖例消失了。

情節1

情節2

我想要的圖形顯示在上方。 有誰知道如何重新獲得傳說? 一為治療組,一為對照組。 還是有一種手動添加圖例的方法?

我不是ggplot2的專家,但是我認為您輸掉了圖例,因為您在geom_bar使用參數分配fill=Palette刪除了美學映射。 本質上,您的fill美學不僅取決於Response ,而且ResponseGroup的組合,因為對於同一響應,每個組都有不同的顏色(這可能是一個有問題的做法,但這不是我要決定的)。

我認為這段代碼可以為您提供所需的內容。 我向data1添加了一個helper字段,以具有適當的fill美感。 注意我需要手動覆蓋scale_fill_manual的圖例標簽

library(dplyr)
data1<- as.data.frame(data %>% 
    group_by(Group, Time, Response) %>%                     
    summarise(N= n())) %>%
  mutate(helper = as.character(group_indices(., Group, Response)))

# Define the color for control and treatment groups
trtCol <- c("#deebf7", "#9ecae1","#3182bd")
conCol <- c("#e5f5e0", "#a1d99b", "#31a354")
Palette<-c(conCol, trtCol, conCol, trtCol, conCol, trtCol)

library(ggplot2)
library(scales)

# Specify color in ggplot using "geom_bar (fill=Palette)"
ggplot(data1, aes(Group, N, fill = helper)) + 
  facet_wrap(~Time, strip.position = "bottom") +
  labs(title="Distribution of Responses by Groups over Time", x="Time Points", y="Percentage") +
  scale_fill_manual(name = "Response", values = Palette, labels = c(1, 2, 3, 1, 2, 3)) +
  geom_bar(position = "fill",stat = "identity") + 
  scale_y_continuous(labels = percent_format()) + 
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        panel.spacing = unit(0.1, "lines"))+
  geom_text(aes(x=0,y=-0.05,label="Control\nGroup"), size=3.5)+
  geom_text(aes(x=1,y=-0.05,label="Treatment\nGroup"), size=3.5)

我使用了Zack建議的代碼,並在其中添加了“ guides(fill = guide_legend(ncol = 2))”。 這是我得到的圖

修改圖

暫無
暫無

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

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