ggplot中如何独立设置折线与点的大小、颜色、线型等样式?
问题解决方案
你遇到的冲突是因为geom_line和geom_point同时将size美学映射到了Genotype变量,ggplot默认对同一种美学的映射共用同一个缩放规则,所以散点的大小设置会同步影响折线。
下面提供两种实现独立设置的方案:
方案1:无需额外依赖包,手动拆分折线图层
适合分组较少的场景,直接为不同基因型分别绘制折线,不需要做美学映射,自然不会和散点的缩放规则冲突。
修改后的完整代码:
# 加载包 library(ggplot2) # 数据集代码 Genotype<-rep(c("GenotypeA","GenotypeB"),each=6) Nitrogen<- rep(c(0,20,50,100,150,200),2) Yield<- c(25,27,26,27,30,35,10,12,14,16,18,20) dataA<- data.frame(Genotype,Nitrogen,Yield) # 绘图代码 ggplot(data=dataA, aes(x=Nitrogen, y=Yield, fill=Genotype, shape=Genotype))+ # 单独绘制两种基因型的折线,直接指定样式,不做映射 geom_line(data = subset(dataA, Genotype == "GenotypeA"), size = 1, color = "Green", linetype = "solid") + geom_line(data = subset(dataA, Genotype == "GenotypeB"), size = 2, color = "Blue", linetype = "dashed") + # 散点图层和原有逻辑一致 geom_point(aes(size=Genotype)) + scale_shape_manual(values= c(21,22)) + scale_fill_manual(values= c("White", "Orange")) + scale_size_manual(values=c(9,6))+ scale_x_continuous(breaks = seq(0,200,50),limits = c(0,200)) + scale_y_continuous(breaks = seq(0,40,5),limits = c(0,40)) + labs(x="Nitrogen", y="Yield") + theme(axis.title.x = element_text (face = "plain", size = 15, color = "black"), axis.title.y = element_text (face = "plain", size = 15, color = "black"), axis.text.x = element_text(size= 15, margin = margin(t=0, r=0, b=10, l=0)), axis.text.y = element_text(size= 15, margin = margin(t=0, r=0, b=0, l=10)), axis.line = element_line(size = 0.5, colour = "black"), legend.position = 'bottom', legend.key = element_rect(color = "white", fill = "white"), legend.key.size = unit(0.5,"cm"), legend.title = element_text(face = "plain", size = 14, color = "Black"), legend.text = element_text(face = "plain", size = 14, color = "Black"), plot.margin=unit(c(0,0,0,0),"cm")) + windows(width=5.5, height=5)
方案2:使用ggnewscale包创建独立缩放规则
适合分组较多的场景,通过新增独立的size缩放规则实现样式分离,代码更简洁易维护。
需要先安装加载依赖包:
install.packages("ggnewscale") library(ggnewscale)
修改后的完整绘图代码:
ggplot(data=dataA, aes(x=Nitrogen, y=Yield))+ # 先绘制折线图层,映射相关美学 geom_line(aes(color = Genotype, linetype = Genotype, size = Genotype, group = Genotype)) + # 设置折线的样式规则,统一name保证图例合并 scale_color_manual(name = "Genotype", values = c("GenotypeA" = "Green", "GenotypeB" = "Blue")) + scale_linetype_manual(name = "Genotype", values = c("GenotypeA" = "solid", "GenotypeB" = "dashed")) + scale_size_manual(name = "Genotype", values = c("GenotypeA" = 1, "GenotypeB" = 2)) + # 新增独立的size缩放规则,后续的size映射和前面折线的size完全独立 new_scale("size") + # 绘制散点图层,映射散点的专属美学 geom_point(aes(fill = Genotype, shape = Genotype, size = Genotype)) + # 设置散点的样式规则,name和折线保持一致,图例自动合并 scale_shape_manual(name = "Genotype", values= c(21,22)) + scale_fill_manual(name = "Genotype", values= c("White", "Orange")) + scale_size_manual(name = "Genotype", values=c(9,6))+ # 后续坐标轴和主题设置不变 scale_x_continuous(breaks = seq(0,200,50),limits = c(0,200)) + scale_y_continuous(breaks = seq(0,40,5),limits = c(0,40)) + labs(x="Nitrogen", y="Yield") + theme(axis.title.x = element_text (face = "plain", size = 15, color = "black"), axis.title.y = element_text (face = "plain", size = 15, color = "black"), axis.text.x = element_text(size= 15, margin = margin(t=0, r=0, b=10, l=0)), axis.text.y = element_text(size= 15, margin = margin(t=0, r=0, b=0, l=10)), axis.line = element_line(size = 0.5, colour = "black"), legend.position = 'bottom', legend.key = element_rect(color = "white", fill = "white"), legend.key.size = unit(0.5,"cm"), legend.title = element_text(face = "plain", size = 14, color = "Black"), legend.text = element_text(face = "plain", size = 14, color = "Black"), plot.margin=unit(c(0,0,0,0),"cm")) + windows(width=5.5, height=5)
内容的提问来源于stack exchange,提问作者J.K Kim
相关产品推荐
相关产品推荐

