代码之家  ›  专栏  ›  技术社区  ›  jlklein

带有geom_textpath()的多层圆环图:如何删除黑色连接线?

  •  1
  • jlklein  · 技术社区  · 1 年前

    我希望按照Allan Cameron在中提供的代码,使用geomtextpath::geom_textpath()绘制一个嵌套的、带有弯曲文本的2层圆环图 this 邮递不幸的是,文本被一条黑色的连接线遮住了,这使得情节无法阅读,我不知道如何删除它。

    enter image description here

    以下是可重复示例的一些数据:

    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 = g3 <- 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)) 
    

    我使用ggplot和geomtextpath绘制甜甜圈图

    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") 
    

    我怀疑这一行在某种程度上是由于aes(group=)参数引起的。

    2 回复  |  直到 1 年前
        1
  •  3
  •   Robert Long    1 年前

    你可以尝试使用 linetype = "blank" 在里面 geom_textpath 以防止绘制线条,从而不会模糊文本:

    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.3, label = value, group = value), hjust = 0.5, vjust = 0.5, color = "black", size = 3, linetype = "blank") +
      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(theta = "x") +
      theme_void() +
      theme(legend.position = "none")
    

    enter image description here

        2
  •  2
  •   stefan    1 年前

    简单设置 text_only=TRUE 在里面 geomtextpath::geom_textpath 只显示标签:

    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),
        text_only = TRUE
      ) +
      scale_alpha_manual(values = c(1, 0.2)) +
      scale_fill_manual(
        values = c("#4bdca4", "#bf65e6", "#eb2b42"),
        aesthetics = c("color", "fill")
      ) +
      scale_y_continuous(limits = c(-0.5, 3.6)) +
      coord_polar() +
      theme_void() +
      theme(legend.position = "none")