如何移除ggplot2图例中符号周围的方框
问题
无法移除ggplot2图例中符号周围的方框,以下是代码、数据片段及生成的图表:
代码
theme_opts_V5 <- list(theme( panel.grid.minor.x = element_line(color = "transparent"), panel.grid.major.x = element_line(color = "transparent"), panel.grid.minor = element_line(color = "grey"), panel.grid.major = element_line(color = "grey"), panel.background = element_blank(), plot.background = element_rect(), panel.border = element_blank(), axis.line = element_line(colour = "black") )) ggplot(data, aes(x = value, y = old_value, fill = category, color = category)) + geom_hline( yintercept = 0, linetype = "solid", color = "#898800", size = 1 ) + geom_vline( xintercept = 57, linetype = "solid", color = "#898800", size = 1 ) + geom_text(aes(x = 61, label = "Mean", y = 15), colour = "#898800", angle = 0, size = 3.4) + geom_point(size = 3, shape = 23) + labs( title = "Title", subtitle = "Subtitle", caption = "Source" ) + xlab("new, in %") + ylab("change to old") + scale_color_manual(values = c("#440154", "#3b528b")) + scale_fill_manual(values = c("#440154", "#3b528b")) + theme( legend.title = element_blank(), legend.position = "bottom", legend.key = element_rect(fill = NA) ) + theme(legend.background = element_blank()) + theme_opts_V5 + scale_x_continuous(limits = c(30, 80), breaks = seq(30, 80, by = 10), expand = c(0, 0)) + scale_y_continuous(limits = c(-30, 20), breaks = seq(-30, 20, by = 10), expand = c(0, 0))
数据片段
structure(list(city = c("Aadorf", "Affeltrangen", "Altnau", "Amlikon-Bissegg", "Amriswil", "Arbon", "Basadingen-Schlattingen", "Berg", "Berlingen", "Bettwiesen", "Bichelsee-Balterswil", "Birwinken", "Bischofszell", "Bottighofen", "Braunau", "Bürglen", "Bussnang", "Diessenhofen", "Dozwil", "Egnach"), value = c(55L, 48L, 60L, 70L, 63L, 76L, 52L, 42L, 53L, 49L, 49L, 57L, 70L, 36L, 49L, 69L, 47L, 47L, 44L, 57L), old_value = c(4L, 2L, -1L, 0L, 0L, 0L, 3L, -3L, -2L, 6L, 5L, -11L, -2L, -3L, -3L, 6L, -3L, 0L, -8L, -3L), category = c("low", "low", "low", "low", "high", "high", "low", "low", "low", "low", "low", "low", "low", "low", "low", "low", "low", "low", "low", "low")), row.names = c(NA, 20L), class = "data.frame")
生成图表

补充说明
theme_opts_V5仅定义绘图的背景和网格线:
theme_opts_V5 <- list(theme(panel.grid.minor.x = element_line(color = "transparent"), panel.grid.major.x = element_line(color = "transparent"), panel.grid.minor = element_line(color = "grey"), panel.grid.major = element_line(color = "grey"), panel.background = element_blank(), plot.background = element_rect(), panel.border = element_blank(), axis.line = element_line(colour = "black")))
解决方案
问题出在主题设置的加载顺序:你先设置了legend.key = element_rect(fill = NA),但后面加载的theme_opts_V5没有指定legend.key的边框颜色,导致默认黑色边框被保留。要彻底移除方框,需要同时设置legend.key的fill和colour为NA,并确保该设置不被后续主题覆盖。
有两种修改方式:
方式一:调整theme设置顺序
把自定义图例的theme代码移到theme_opts_V5之后,避免被覆盖:
ggplot(data, aes(x = value, y = old_value, fill = category, color = category)) + geom_hline( yintercept = 0, linetype = "solid", color = "#898800", size = 1 ) + geom_vline( xintercept = 57, linetype = "solid", color = "#898800", size = 1 ) + geom_text(aes(x = 61, label = "Mean", y = 15), colour = "#898800", angle = 0, size = 3.4) + geom_point(size = 3, shape = 23) + labs( title = "Title", subtitle = "Subtitle", caption = "Source" ) + xlab("new, in %") + ylab("change to old") + scale_color_manual(values = c("#440154", "#3b528b")) + scale_fill_manual(values = c("#440154", "#3b528b")) + theme_opts_V5 + # 先加载通用主题 theme( legend.title = element_blank(), legend.position = "bottom", legend.key = element_rect(fill = NA, colour = NA), # 同时设置填充和边框为透明 legend.background = element_blank() ) + scale_x_continuous(limits = c(30, 80), breaks = seq(30, 80, by = 10), expand = c(0, 0)) + scale_y_continuous(limits = c(-30, 20), breaks = seq(-30, 20, by = 10), expand = c(0, 0))
方式二:整合图例设置到通用主题
直接修改theme_opts_V5,加入图例相关设置,减少重复代码:
theme_opts_V5 <- list(theme( panel.grid.minor.x = element_line(color = "transparent"), panel.grid.major.x = element_line(color = "transparent"), panel.grid.minor = element_line(color = "grey"), panel.grid.major = element_line(color = "grey"), panel.background = element_blank(), plot.background = element_rect(), panel.border = element_blank(), axis.line = element_line(colour = "black"), # 新增图例设置 legend.title = element_blank(), legend.position = "bottom", legend.key = element_rect(fill = NA, colour = NA), legend.background = element_blank() )) ggplot(data, aes(x = value, y = old_value, fill = category, color = category)) + geom_hline( yintercept = 0, linetype = "solid", color = "#898800", size = 1 ) + geom_vline( xintercept = 57, linetype = "solid", color = "#898800", size = 1 ) + geom_text(aes(x = 61, label = "Mean", y = 15), colour = "#898800", angle = 0, size = 3.4) + geom_point(size = 3, shape = 23) + labs( title = "Title", subtitle = "Subtitle", caption = "Source" ) + xlab("new, in %") + ylab("change to old") + scale_color_manual(values = c("#440154", "#3b528b")) + scale_fill_manual(values = c("#440154", "#3b528b")) + theme_opts_V5 + scale_x_continuous(limits = c(30, 80), breaks = seq(30, 80, by = 10), expand = c(0, 0)) + scale_y_continuous(limits = c(-30, 20), breaks = seq(-30, 20, by = 10), expand = c(0, 0))
内容的提问来源于stack exchange,提问作者user11841097
相关产品推荐
相关产品推荐

