如何移除ggplot中第二个图例的legend.key背景框
解决ggplot2图例点周围灰色背景框问题
问题说明
我制作的双Y轴图表包含两个图例,右侧点元素对应的图例标题为“I”(对应代码中scale_color_manual或guides()内的color参数)。需要保留该图例及其中的点,但移除点周围的深灰色背景框。尝试过legend.key = element_rect(fill = NA)、替换为NULL,以及legend.key.width = unit(0, "mm"),均未解决问题。
测试数据
dput(del1) structure(list(month = structure(c(6L, 6L, 6L, 6L, 6L), levels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), class = "factor"), n_inf = c(2L, 2L, 2L, 2L, 2L), zone = c("Zone 5", "Zone 5", "Zone 5", "Zone 5", "Zone 5"), fac1 = c(120L, 120L, 120L, 120L, 120L), gr_mo1 = c(5L, 5L, 5L, 5L, 5L), gr_mo2 = c(9L, 9L, 9L, 9L, 9L), variables = c("pct0", "pct1", "pct2", "pct3", "pct4"), vals = c(0.268043178947998, 0.465607260195834, 0.265910323616373, 0.000438256452026712, 9.80787768735849e-07)), row.names = 146:150, class = "data.frame")
原可复现代码
library(tidyverse) library(patchwork) label_map <- c("pct0" = "0", "pct1" = "1", "pct2" = "2", "pct3" = "3", "pct4" = "4") ggplot(data = del1, aes(x=month)) + geom_bar(aes(y=vals, fill=factor(variables, labels=label_map)), color = "white", stat = "identity", show.legend = T) + geom_point(aes(y = n_inf/fac1, col = "grey20"), size = 3) + theme(panel.grid.major.x= element_blank(), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), axis.title.x = element_blank(), plot.title = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(size = 14, face = "bold", color = "black"), axis.text.y.left = element_blank(), axis.text.y.right = element_text(size = 14, face = "bold", color = "black"))+ scale_y_continuous(sec.axis = sec_axis(~.*120, name = "Infections, N")) + scale_color_manual(values = "grey20", name = "Infection Count") + guides(color = guide_legend(order = 2, title = "I", theme = theme(legend.title = element_text(size = 20, face = "bold"), legend.text = element_blank(), legend.key = element_rect(fill = NULL, color = NULL)), override.aes = list(color = "grey20")))
解决方案
问题出在仅针对color图例单独设置legend.key,但填充图例(柱状图对应的fill图例)的默认灰色背景会影响视觉效果,且fill = NULL无法完全透明化背景。正确做法是在全局theme()中统一设置legend.key = element_rect(fill = NA, color = NA),让所有图例的key背景完全透明,同时保留color图例的其他自定义设置。
修改后的代码
library(tidyverse) library(patchwork) label_map <- c("pct0" = "0", "pct1" = "1", "pct2" = "2", "pct3" = "3", "pct4" = "4") ggplot(data = del1, aes(x=month)) + geom_bar(aes(y=vals, fill=factor(variables, labels=label_map)), color = "white", stat = "identity", show.legend = T) + geom_point(aes(y = n_inf/fac1, col = "grey20"), size = 3) + theme(panel.grid.major.x= element_blank(), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), axis.title.x = element_blank(), plot.title = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(size = 14, face = "bold", color = "black"), axis.text.y.left = element_blank(), axis.text.y.right = element_text(size = 14, face = "bold", color = "black"), # 全局设置图例key背景透明,边框也透明 legend.key = element_rect(fill = NA, color = NA))+ scale_y_continuous(sec.axis = sec_axis(~.*120, name = "Infections, N")) + scale_color_manual(values = "grey20", name = "Infection Count") + guides(color = guide_legend(order = 2, title = "I", theme = theme(legend.title = element_text(size = 20, face = "bold"), legend.text = element_blank()), override.aes = list(color = "grey20")))
说明
fill = NA会将图例key的背景设为完全透明,而color = NA会移除key的边框(如果有的话)- 移除了
guides(color)内部的legend.key设置,因为全局theme已经统一处理,避免冲突
内容的提问来源于stack exchange,提问作者John Polo
相关产品推荐
相关产品推荐

