ggplot2:使用 geom_bar 中的填充指定颜色时缺少图例

问题描述 投票:0回答:2

在堆积条形图中,我尝试指定不同条形的颜色。例如,治疗组使用渐变绿色,对照组使用渐变蓝色。但是,指定颜色后,我丢失了图例。有没有办法重新添加传说?

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

当我指定每个条形的颜色时,图例消失了。

我想要的图表如上所示。有谁知道怎么找回传说吗?一组用于治疗组,一组用于对照组。或者有没有办法手动添加图例?

r colors bar-chart legend geom-bar
2个回答
1
投票

我不是

ggplot2
方面的专家,但我认为您丢失了图例,因为您使用参数分配
geom_bar
删除了
fill=Palette
中的美学映射。从本质上讲,你的
fill
审美不仅仅取决于
Response
,而是由
Response
Group
组合而成,因为每个组对于相同的响应都有不同的颜色(这可能是一种有问题的做法,但这不是由我决定)。

我认为这段代码可以满足您的需求。我在

helper
中添加了
data1
字段,以便具有适当的
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)

0
投票

我使用了 Zack 建议的代码,并添加了

guides(fill=guide_legend(ncol=2))

这是我得到的图表:

Revised graph

© www.soinside.com 2019 - 2024. All rights reserved.