【发布时间】:2021-03-28 18:29:46
【问题描述】:
我有一个带有箱线图(geom_boxplot)的图,上面覆盖了一些标记点(geom_point)。默认情况下,图例显示为全部混合在一起,但我想将其拆分,以便每个 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
【问题讨论】:
-
如果您包含一个简单的reproducible example 以及可用于测试和验证可能的解决方案的示例输入,则更容易为您提供帮助。
-
我会添加一些东西
-
我已经更新了问题。希望对您有帮助
-
我认为您需要对数据进行一些旋转,以便可以使用
aes(shape = )映射,然后添加scale_shape_manual。我们很难真正向您展示,因为您提供的数据如此之少,也没有major_spacing和minor_spacing的副本。