Home > Mobile >  using geom_textpath to add labels to a double donut chart , some labels color are smimilar with the
using geom_textpath to add labels to a double donut chart , some labels color are smimilar with the

Time:04-13

I'm using geom_textpath to add labels to a double donut chart but there are some labels color are smimilar with the fill color ,so not it's not clear. How to handle it?

library(magrittr)
library(ggplot2)
library(geomtextpath)

pie_data <- data.frame(
  category = c("A", "B", "C", "A", "B", "C"),
  year = c(2020, 2020, 2020, 2021, 2021, 2021),
  sales = c(40, 30, 20, 10, 15, 10)
)

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))  
  geom_col(position = "fill", width = 1, color = "white")  
  lims(x = c(2018, 2023))  
  geom_textpath(
    position = position_fill(vjust = .5), angle = 90, alpha = 1,
    aes(
      color = factor(year),
      label = paste0(category, ":", sales)
    )
  )  
  coord_polar(theta = "y")  
  theme_void()

CodePudding user response:

You can specify the text color scale to make them stand out:

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))  
  geom_col(position = "fill", width = 1, color = "white")  
  lims(x = c(2018, 2023))  
  geom_textpath(
    position = position_fill(vjust = .5), angle = 90, alpha = 1,
    aes(
      color = factor(year),
      label = paste0(category, ":", sales)
    ), size = 6, fontface = 2
  )  
  coord_polar(theta = "y")  
  scale_colour_manual(values = c("white", "yellow"))  
  theme_void()

enter image description here

  • Related