How to add multiple legend titles (columns) in ggp

2019-05-31 01:20发布

In the stacked bar chart, I have different colors for treatment group and control group. Green for control group and blue for treatment group. In the legend part, I want to show two columns of legends, one for each group. My code is shown below:

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())) %>%
        mutate(helper = as.character(group_indices(., Group, Response)))

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

library(ggplot2)
library(scales) 

# Different colors for treatment and control groups    
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)+
guides(fill=guide_legend(ncol=2))

enter image description here

Is there a way to create legend title for each column? This is the graph I want to create. Does anyone know how to create the column names for the two columns of legends?

enter image description here

1条回答
可以哭但决不认输i
2楼-- · 2019-05-31 02:09

I found a simple solution to it. I still have two columns of legends side by side. I added some text to the graph using "grid.text" function. Below shows my code.

p<-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\n\n",  values = Palette, labels = c("Very Severe", "Moderate", "None", "Very Severe", "Moderate", "None")) +
  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"), legend.text=element_text(size=11),legend.title=element_text(size=15))+
  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)+
  guides(fill=guide_legend(ncol=2))

library(grid)  
p
grid.text("Control", x = unit(0.81, "npc"), y = unit(0.54, "npc"))
grid.text("Treatment", x = unit(0.92, "npc"), y = unit(0.54, "npc"))

This is the graph with multiple legend titles I got.

查看更多
登录 后发表回答