0

With the following code, I used waffle package to generate a waffle chart

library(tidyverse)
library(waffle)
df <- structure(list(
  parts = c("case_1", "case_2", "case_3"),
  values = c(11, 34, 55)
),
row.names = c(NA, -3L),
class = c("tbl_df", "tbl", "data.frame")
)

df
p <- df %>% ggplot() +
  geom_pictogram(
    n_rows = 10, aes(label = parts, values = values, color = parts),
    family = "FontAwesome5Free-Solid",
    flip = TRUE,
    size = 10
  ) +
  scale_label_pictogram(
      name = "Cases", values = c("male"),
  ) +
  scale_color_manual(
      name = "Cases", 
      values = c("case_1" = "red", "case_2" = "green", "case_3" = "grey85"
    )
  ) +
  coord_equal() +
  theme_minimal() +
  theme(legend.position = "bottom")
p

Then, I tried to add "45%" to the chart with geom_text. However, it did not show "45%" but the icon instead like this:

p + geom_text(aes(x = 5, y = 7, label = "45%"),
  size = 24,
  show.legend = FALSE
)

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • 3
    Probably you can put the `label = "45%"` outside the `aes()` to prevent mapping it to the label scale. – teunbrand Oct 18 '22 at 11:08
  • 1
    Alternatively, you might use the `annotate()` function instead – teunbrand Oct 18 '22 at 11:10
  • Thank you, it works. In my real work `label` is a value from a dataframe together with `x` and `y`. I guess I can get the value first and put it outside the `aes()`. It should be fine for my purpose. – Zhiqiang Wang Oct 18 '22 at 11:35

0 Answers0