2

I wish to plot a nested, 2-layer donut chart with curved text using geomtextpath::geom_textpath() following the code provided by Allan Cameron in this post. Unfortunately, the text is obscured by a black connector line that makes the plot unreadible and I cannot figure out how to remove it.

enter image description here

Here is some data for a reproducible example:

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

I plot the donut chart using ggplot and 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") 

I suspect this line is somewhat due to the aes(group = ) argument.

2 Answers 2

2

Simply set text_only=TRUE in geomtextpath::geom_textpath to show only the label:

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

Sign up to request clarification or add additional context in comments.

1 Comment

I accepted this as an answer but Robert's answer works equally well. Thanks to both !
2

You could try using linetype = "blank" in geom_textpath to prevent the line from being drawn, so it doesn't obscure the text:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.