繁体   English   中英

如何在 ggplot2 中拆分图例

[英]How to Split a Legend in ggplot2

在此处输入图片说明 我有一个图,其中有一些标记点(geom_point)覆盖的箱线图(geom_boxplot)。 默认情况下,图例显示为全部混合在一起,但我想将其拆分,以便在图例中单独列出每个 geom_point 项目。

library(tidyverse)  # data manipulation etc
library(scales)     # for log scales
library(viridis)    # for colour-blind friendly palettes

PlotData_HIL %>%
  ggplot(aes(Analyte, Concentration, fill = Analyte)) +                                  # Plot analyte vs Concentration, with a different colpour per analyte
  geom_boxplot(outlier.shape = NA, varwidth = TRUE, alpha = 0.7, colour = "grey40")+     # Boxplot with circles for outliers and width proportional to count
  scale_y_log10(breaks = major_spacing, minor_breaks = minor_spacing, labels = number) + # Log scale for Y axis
  geom_jitter(aes(fill = Analyte), shape = 21, size = 2.5, alpha = 0.3, width = 0.1)+                          # overlay data points to show actual distribution and clustering
  geom_point(aes(Analyte,GIL_fresh), colour="red", shape=6, size = 3)+                                 # Choose the HIL set to apply
  geom_point(aes(Analyte,ADWG), colour="red", shape=4, size = 3)+
  geom_point(aes(Analyte,HSLAB_sand_2-4), colour="red", shape=3, size = 3)+
  labs(title = "Box Plots", subtitle = "Box width is proportional to the square root of the number of samples.  Individual data points overlaid as circles.\nGILs shown as red triangless.ADWG values shown as red Xs. HSLs shown as red +s.") +
  ylab("Concentration (\u03BCg/L)") +                                                        # Label for Y axis
  xlab("") +                                                                             # X axis already done
  scale_color_viridis(discrete = TRUE, option = "viridis")+                              # Colour-blind friendly outlines
  scale_fill_viridis(discrete = TRUE, option ="viridis") +                               # Colour-blind friendly fill
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5), panel.grid.major.y = element_line(size = 0.5))+
  theme(strip.background = element_rect(colour = "black", fill = "white"),               # White label strips, black text and border
        strip.text.x = element_text(colour = "black", face = "bold"),
        panel.border = element_rect(colour = "black", fill = NA),
        axis.title = element_text(colour = "black", face = "bold"),
        axis.text = element_text(colour = "black", face = "bold")
  )

图例显示,对于每个分析物,ggplot 调用中 geom_* 函数的 eack 的条目叠加在彼此之上。 我想将它们分开,以便 geom_boxplot 的图例条目与每个 geom_point 条目的图例条目不同,以便我可以标记三角形代表什么,以及 X 代表什么。

我正在从电子表格中读取数据,但不确定如何在代码中设置虚拟数据,但示例数据在这里:

Analyte Concentration GIL_fresh GIL_marine  ADWG HSLAB_sand_2_4 HSLAB_sand_4_8 HSLAB_sand_8 HSLC_sand_2_4 HSLC_sand_4_8 HSLC_sand_8 HSLD_sand_2_4 HSLD_sand_4_8 HSLD_sand_8 HSLAB_silt_2_4 HSLAB_silt_4_8
   <fct>           <dbl>     <dbl>      <dbl> <dbl>          <dbl>          <dbl>        <dbl> <lgl>         <lgl>         <lgl>               <dbl>         <dbl>       <dbl>          <dbl>          <dbl>
 1 Arsenic          12       13          NA      10             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 2 Cadmium           1        0.2         0.7     2             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 3 Chromi…          24        1           4.4    50             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 4 Copper           42        1.4         1.3  2000             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 5 Lead             24        3.4         4.4    10             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 6 Mercury           0.1      0.06        0.1     1             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 7 Nickel            8       11           7      20             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 8 Zinc            100        8          15      NA             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
 9 Ammonia        2252       NA          NA      NA             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA
10 Arsenic          10       13          NA      10             NA             NA           NA NA            NA            NA                     NA            NA          NA             NA             NA

我尝试创建一些类似于您的数据,我怀疑具有形状的列(例如GIL_fresh )很可能是从某些合并中获得的。 用单独的 data.frame 绘制它们可能会更好:

analytes = c("Ammonia", "Arsenic", "Cadmium", "Chromium", "Copper", "Lead", "Mercury", "Nickel", "Zinc")
PlotData_HIL = data.frame(
                Analyte = rep(analytes,each=5),
                Concentration = runif(45,0,100),
                GIL_fresh  = rep(c(10,rep(NA,8)),5),
                ADWG = rep(c(15,rep(NA,8)),5))

对于红点,您需要在aes(..)内指定shape=以便出现形状图例,最后,我删除填充图例的点,因为它看起来非常多余:

PlotData_HIL %>%
ggplot(aes(Analyte, Concentration, fill = Analyte)) +                                  
geom_boxplot(outlier.shape = NA, varwidth = TRUE, alpha = 0.7, colour = "grey40")+     
scale_y_log10() + 
geom_jitter(aes(fill = Analyte), shape = 21, size = 2.5, alpha = 0.3, width = 0.1)+                         
geom_point(aes(Analyte,GIL_fresh,shape="GIL_fresh"), colour="red", size = 3)+                                 
geom_point(aes(Analyte,ADWG,shape="ADWG"), colour="red", size = 3) +
scale_shape_manual(values=c(4,6))+
scale_color_viridis(discrete = TRUE, option = "viridis")+                              
scale_fill_viridis(discrete = TRUE, option ="viridis") +
guides(fill = guide_legend(override.aes = list(shape = NA) ))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM