Select and plot top frequencies and add an "Other" category

Hello all,

I would simply like to obtain in my plot the 3 top categories (with the highest frequencies) and have all the others in a single "Other" category where their frequencies are combined.

I know how to get the plot where only the top 3 is displayed but I can't create the "Other" category

df <- data.frame(

top_n(df, n=3, freq) %>%
          ggplot(., aes(x=type, y=freq))+

If someone knows how to figure it out
Thanks a lot

This is a way to do it


df <- data.frame(

df %>% 
    mutate(type = fct_lump_n(type, n = 3, w = freq)) %>%
    group_by(type) %>% 
    summarise(freq = sum(freq)) %>% 
    ggplot(aes(x = type, y = freq)) +

Created on 2022-04-26 by the reprex package (v2.0.1)

1 Like

Great thanks a lot @andresrcs !

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.