使用geom_textpath绘制双层环形图:如何移除黑色连接线?
移除geom_textpath环形图中的黑色连接线
我参考Allan Cameron的代码,尝试用geomtextpath::geom_textpath绘制带弧形文本的双层嵌套环形图,但文本被黑色连接线遮挡,导致图表无法阅读,找不到移除该连接线的方法。

可复现数据
g3 <- structure(list(top_level = c("1-ha plot", "Relevé", "Transect", "1-ha plot", "1-ha plot", "1-ha plot", "Relevé","Relevé","Transect", "Transect", "Transect", "Transect", "Transect", "Transect"), width = c(240L, 240L, 858L, 189L, 30L, 21L, 132L, 108L, 48L, 9L, 644L, 47L, 32L, 78L), name = c("level1", "level1", "level1","level2", "level2", "level2","level2", "level2", "level2", "level2","level2", "level2", "level2", "level2"), value = c("1-ha plot","Relevé", "Transect", "Cameroon", "DRC", "Gabon", "Cameroon","Gabon", "Cameroon", "Equatorial Guinea", "Gabon", "Guinea","Liberia", "São Tomé and Príncipe"), ymid = c(1, 1, 1, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2), ymax = c(1.5, 1.5, 1.5, 2.5, 2.5,2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5), ymin = c(0.5, 0.5,0.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5), xmin = c(0, 240, 480, 0, 189, 219, 240, 372, 480, 528, 537,1181, 1228, 1260), xmax = c(240L, 480L, 1338L, 189L, 219L,240L, 372L, 480L, 528L, 537L, 1181L, 1228L, 1260L, 1338L),xmid = c(120, 360, 909, 94.5, 204, 229.5, 306, 426, 504,532.5, 859, 1204.5, 1244, 1299)), class = c("grouped_df","tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), groups = structure(list(name = c("level1", "level2"), .rows = structure(list(1:3,4:14), ptype = integer(0), class = c("vctrs_list_of","vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), .drop = TRUE))
原绘图代码
library(ggplot2) library(geomtextpath) g3 %>% ggplot(aes(xmid, ymid, fill = top_level)) + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, alpha = name, color = top_level)) + scale_alpha_manual(values = c(1, 0.2)) + geomtextpath::geom_textpath(aes(y = ymid + 0.25, label = value, group = value)) + scale_alpha_manual(values = c(1, 0.2)) + scale_fill_manual(values = c("#4bdca4", "#bf65e6", "#eb2b42")) + scale_colour_manual(values = c("#4bdca4", "#bf65e6", "#eb2b42")) + scale_y_continuous(limits = c(-0.5, 3.6)) + coord_polar() + theme_void() + theme(legend.position = "none")
解决方法
你看到的黑色连接线是geom_textpath默认绘制的文本路径线条,和group参数无关,只需在geom_textpath中添加linecolor = NA参数即可完全移除这条线;或者设置linecolor为和背景一致的颜色(这里是白色)也能达到效果。
另外你的代码中重复调用了scale_alpha_manual,可以删掉其中一个避免冗余。修改后的代码如下:
library(ggplot2) library(geomtextpath) g3 %>% ggplot(aes(xmid, ymid, fill = top_level)) + geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, alpha = name, color = top_level)) + scale_alpha_manual(values = c(1, 0.2)) + geomtextpath::geom_textpath(aes(y = ymid + 0.25, label = value, group = value), linecolor = NA) + # 移除文本路径线条 scale_fill_manual(values = c("#4bdca4", "#bf65e6", "#eb2b42")) + scale_colour_manual(values = c("#4bdca4", "#bf65e6", "#eb2b42")) + scale_y_continuous(limits = c(-0.5, 3.6)) + coord_polar() + theme_void() + theme(legend.position = "none")
如果需要保留线条但调整样式,还可以通过linewidth(调整粗细)、linetype(调整线型)等参数进一步修改。
内容的提问来源于stack exchange,提问作者jlklein
相关产品推荐
相关产品推荐

