0

I am trying to elaborate a pie chart, my data are number (1:61), I have placed them as a character when the dataset was imported y my script is..

    porcentaje<-review_a_r %>% 
  group_by(s_s) %>% 
  count() %>% 
  ungroup() %>% 
  mutate(percentage=`n`/sum(`n`) * 100)

ggplot(porcentaje, aes(x=1, y=percentage, fill=s_s)) +
   geom_bar(stat="identity") +
  geom_text(aes(label = paste0(round(percentage,1),"%")),
            position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") + 
   theme_void()

enter image description here

How can I obtain only the classes with a percent >= of 10% and the others are in a section of "others"??

  • Please include your data with `dput()` or similar to make your code reproducible. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on how to include data – Jan Boyer May 08 '20 at 21:32
  • 1
    You may benefit from the forcats::fct_lump function. It has options for a minimum number of a minimum proportion. https://forcats.tidyverse.org/reference/fct_lump.html – ravic_ May 08 '20 at 23:19

1 Answers1

0

Consider this approach:

porcentaje <- porcentaje %>% mutate(flag = case_when(percentage <= 10 ~ "Other",
                                                     percentage > 10 ~ s_s))

Here I've created a new variable. case_when() is effectively a vectorised if/else, but what I've done is assign anything with a percentage of less than or equal to 10 the flag "other" and everything else a flag equal to s_s. You can then colour by the "flag" variable, i.e.

ggplot(porcentaje, aes(x=1, y=percentage, fill=flag)) +
   geom_bar(stat="identity") +
  geom_text(aes(label = paste0(round(percentage,1),"%")),
            position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") + 
   theme_void()
Jack
  • 173
  • 8