如何在ggplot中添加多个图例标题(列) [英] How to add multiple legend titles (columns) in ggplot

查看:1165
本文介绍了如何在ggplot中添加多个图例标题(列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在堆积的条形图中,治疗组和对照组有不同的颜色.对照组为绿色,治疗组为蓝色.在图例部分,我想显示两列图例,每组一个.我的代码如下所示:

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

是否可以为每列创建图例标题? 这是我要创建的图形.有谁知道如何为图例的两列创建列名?

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?

推荐答案

我找到了一个简单的解决方案.我仍然有两列并排的图例.我使用"grid.text"功能在图形中添加了一些文本.下面显示了我的代码.

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

这是包含多个图例标题的图形.

这篇关于如何在ggplot中添加多个图例标题(列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆