如何在图例ggplot2中添加多个标题?

时间:2019-04-02 08:56:41

标签: r ggplot2

如何在图例ggplot中添加多个标题?

library(RColorBrewer)
library(ggplot2)
data1<-data.frame(x= rnorm(20, 0,1), 
                  y=rnorm(20, 1,1), 
                  section=c("a1", "a2", "a3", "a4", "a5", "a6", "a7", 
                        "a8",  "a9","a10","b1", "b2", "c1",   "c2", "c3", 
                         "d1", "d2", "e1", "e2","f1"),
                  region=c(rep("a",10), rep("b",2),rep("c",3),rep("d",2), 
                             rep("e",2), rep("f",1))) #data

newpalette<-c(colorRampPalette(brewer.pal(9,"Blues"))(15)[15:6], 
            brewer.pal(9,"Greens")[c(4,8)], 
          brewer.pal(9,"Oranges")[c(4,7,9)], 
          brewer.pal(9,"Purples")[c(4,8)],      
          brewer.pal(9,"BrBG")[c(1,3)], 
          brewer.pal(9,"Greys")[5]) #20 color values
labels<-c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9","a10",
          "b1", "b2", 
           "c1",   "c2", "c3",
           "d1", "d2", 
           "e1", "e2",
           "f1")
 data1$section<-factor(data1$section,levels= labels)     
 p.legend<-ggplot( ) + 
           geom_point(data=data1, aes(x, y, colour = section, 
           shape=section), size=2)+
           scale_color_manual(values =newpalette ,labels= labels)+
           scale_shape_manual(values = 
           c(rep(1,10),0,0,2,2,2,3,3,5,5,6),labels= labels)

我希望区域值作为图例中的字幕,如下图所示。

enter image description here

也许我们可以修改一些特定图例键(例如,图例键a10,c3)的间距。然后使用grid.text将文本(a,b,c ...)添加到How to add multiple legend titles (columns) in ggplot之类的空格中。但是我的子标题更加复杂,我不知道如何修改某些特定图例键的间距。

或者我们可以创建其他一些值(例如a11)并使其在图例中变得清晰。然后将文本b添加到a11间距中。但是我不知道如何删除特定的图例键和文本。

第三种方式,我曾经将\n\n添加到一些标签代码中以创建间距。

labels2<- c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9","a10\n\n",
          "b1", "b2", 
           "c1",   "c2", "c3\n\n",
           "d1", "d2\n\n", 
           "e1", "e2\n\n",
           "f1")
p.legend<-ggplot( ) + 
           geom_point(data=data1, aes(x, y, colour = section, 
           shape=section), size=2)+
           scale_color_manual(values =newpalette ,labels= labels2)+
           scale_shape_manual(values = 
           c(rep(1,10),0,0,2,2,2,3,3,5,5,6),labels= labels2)

但是标签和键不会像下图那样对齐

enter image description here

这是我尝试过和失败的三种方法。

1 个答案:

答案 0 :(得分:1)

使用@Mark Peterson的建议(ggplot2: Divide Legend into Two Columns, Each with Its Own Title)使用cowplot,这是一种方法:

library(cowplot)

# create chart without legend
p <- ggplot( ) + 
  geom_point(data=data1, aes(x, y, colour = section, shape = section), size = 2) +
  scale_color_manual(values = newpalette) + scale_shape_manual(values = c(rep(1,10),0,0,2,2,2,3,3,5,5,6)) +
  theme(legend.position = "none")

# To create individaul ggplot for each region
region_i <- unique(data1$region)

for (i in 1:length(region_i)) {

  region_d <- data1[data1$region == region_i[i], ]

  # this is to keep same shapes as above
  shape_ind <- switch(i, 
                      "1" = "1", 
                      "2" = "0",
                      "3" = "2", 
                      "4" = "3",
                      "5" = "5", 
                      "6" = "6")

  # ggplots to be saved in p1, p2 etc...
  nam <- paste0("p", i)

  # ggplots for each region with different colours and shapes for these regions
  my_plot <- ggplot() + 
    geom_point(data = region_d, aes(x, y, colour = section, shape = section), size=2) +
    scale_color_manual(values = newpalette[which(data1$region == region_i[i])]) +
    scale_shape_manual(values = c(rep(as.numeric(shape_ind), nrow(region_d)))) + 
    labs(color = unique(region_d$region), shape = unique(region_d$region))

  # call p1, p2 etc.. to see the plots
  assign(nam, my_plot)

}

# cowplot to combine the plots
plot_grid(p, 
          plot_grid(get_legend(p1), get_legend(p2), ncol = 1), # a and b to be kept in one column
          plot_grid(get_legend(p3), get_legend(p4), get_legend(p5), get_legend(p6), ncol = 1),  # c, d, e and f in one column
          ncol = 3, rel_widths = c(4, 1, 1))

enter image description here