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

library(dplyr)
df <- data.frame(
  type=c("car","bike","horse","boat","yacht","train"),freq=c(20,2,5,60,11,10))

#Plot
top_n(df, n=3, freq) %>%
          ggplot(., aes(x=type, y=freq))+
              geom_bar(stat='identity')

If someone knows how to figure it out
Thanks a lot
Hersh

This is a way to do it

library(tidyverse)

df <- data.frame(
    type=c("car","bike","horse","boat","yacht","train"),
    freq=c(20,2,5,60,11,10)
    )

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

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.