如何统一ggplot2子图图例,仅显示模型对应颜色的点?
问题:让ggplot子图图例仅显示模型对应的点(隐藏loess曲线的图例元素)
我需要在每个子图中为三个模型(A/B/C)绘制不同颜色的loess拟合曲线,但当前代码生成的图例同时混合了点和线,希望图例只保留不同颜色的点,对应三个模型。
原代码如下:
mydata1 <- data.frame("model" = rep("A", 1000), "n" = sample.int(10, 1000, replace = T), "n1" = seq(1,1000), "values" = rnorm(1000)) mydata2 <- data.frame("model" = rep("B", 1000), "n" = sample.int(10, 1000, replace = T), "n1" = seq(1,1000), "values" = rnorm(1000)) mydata3 <- data.frame("model" = rep("C", 500), "n" = c(rep(6,100),rep(7,100),rep(8,100),rep(9,100),rep(10,100)), "n1" = seq(1,500), "values" = rnorm(500)) mydata <- rbind(mydata1, mydata2, mydata3) fig_list <- list() for (i in sort(unique(mydata$n))) { data <- mydata[mydata$n == i, ] fig_list[[i]] <- ggplot(data, aes(x = n1, y = values, colour = model)) + geom_point(size = 0.6, show.legend = TRUE) + scale_color_discrete(limits = unique(mydata$model)) + labs(title = paste0("n = ", i), x = "n1", y = "values") + theme(plot.title = element_text(hjust = 0.5, size = 10)) + geom_smooth(method = "loess", span = 0.3, fill = "lightskyblue", level = 0.95) } fig_all <- ggarrange( plotlist = fig_list, ncol = 2, nrow = ceiling(length(fig_list) / 2), common.legend = TRUE, legend = "top" ) annotate_figure(fig_all, top = text_grob(paste0("All the plots are here.")))
解决方案
只需对geom_smooth进行修改,阻止它生成图例元素,同时确保geom_point的图例正常显示:
- 在
geom_smooth中添加show.legend = FALSE参数,让拟合曲线不参与图例的构建 - 保持
geom_point的show.legend = TRUE,确保图例只展示模型对应的颜色点
修改后的完整代码:
mydata1 <- data.frame("model" = rep("A", 1000), "n" = sample.int(10, 1000, replace = T), "n1" = seq(1,1000), "values" = rnorm(1000)) mydata2 <- data.frame("model" = rep("B", 1000), "n" = sample.int(10, 1000, replace = T), "n1" = seq(1,1000), "values" = rnorm(1000)) mydata3 <- data.frame("model" = rep("C", 500), "n" = c(rep(6,100),rep(7,100),rep(8,100),rep(9,100),rep(10,100)), "n1" = seq(1,500), "values" = rnorm(500)) mydata <- rbind(mydata1, mydata2, mydata3) fig_list <- list() for (i in sort(unique(mydata$n))) { data <- mydata[mydata$n == i, ] fig_list[[i]] <- ggplot(data, aes(x = n1, y = values, colour = model)) + geom_point(size = 0.6, show.legend = TRUE) + scale_color_discrete(limits = unique(mydata$model)) + labs(title = paste0("n = ", i), x = "n1", y = "values") + theme(plot.title = element_text(hjust = 0.5, size = 10)) + # 添加show.legend = FALSE,隐藏拟合曲线的图例元素 geom_smooth(method = "loess", span = 0.3, fill = "lightskyblue", level = 0.95, show.legend = FALSE) } fig_all <- ggarrange( plotlist = fig_list, ncol = 2, nrow = ceiling(length(fig_list) / 2), common.legend = TRUE, legend = "top" ) annotate_figure(fig_all, top = text_grob(paste0("All the plots are here.")))
补充说明
如果需要进一步确保图例只显示点(避免其他潜在元素干扰),可以在ggplot中添加guides()参数,强制图例隐藏线的样式:
fig_list[[i]] <- ggplot(data, aes(x = n1, y = values, colour = model)) + geom_point(size = 0.6, show.legend = TRUE) + scale_color_discrete(limits = unique(mydata$model)) + labs(title = paste0("n = ", i), x = "n1", y = "values") + theme(plot.title = element_text(hjust = 0.5, size = 10)) + geom_smooth(method = "loess", span = 0.3, fill = "lightskyblue", level = 0.95, show.legend = FALSE) + # 强制图例只显示点,不显示线 guides(colour = guide_legend(override.aes = list(linetype = "blank")))
内容的提问来源于stack exchange,提问作者Phoebe
相关产品推荐
相关产品推荐

