'How would I filter ggplot to display the top 15 values?

I would like to filter the ggplot by the top 15 values of start_station_name.

This is my code:

ggplot(trips_data2) + 
  geom_bar(mapping = aes(x=start_station_name, fill=member_casual), 
           position = "dodge", width = .7) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(labels = scales::comma) + 
  labs(x="Station", y="Total Trips", fill = "Member") + 
  ggtitle("Popular Start Stations") + 
  theme(plot.title = element_text(hjust = 0.5))

This is a sample from my data using dput(head(trips_data2)):

trips_data2 <- structure(list(ride_id = c(
  "A847FADBBC638E45", "5405B80E996FF60D",
  "5DD24A79A4E006F4", "2A59BBDF5CDBA725", "27AD306C119C6158", "356216E875132F61"
), rideable_type = c(
  "docked_bike", "docked_bike", "docked_bike",
  "docked_bike", "docked_bike", "docked_bike"
), started_at = structure(c(
  1587923100,
  1587143280, 1585763640, 1586263800, 1587205320, 1588269300
), class = c(
  "POSIXct",
  "POSIXt"
), tzone = "UTC"), ended_at = structure(c(
  1587924720,
  1587143820, 1585764480, 1586264520, 1587208500, 1588269660
), class = c(
  "POSIXct",
  "POSIXt"
), tzone = "UTC"), start_station_name = c(
  "Eckhart Park",
  "Drake Ave & Fullerton Ave", "McClurg Ct & Erie St", "California Ave & Division St",
  "Rush St & Hubbard St", "Mies van der Rohe Way & Chicago Ave"
), start_station_id = c("86", "503", "142", "216", "125", "173"), end_station_name = c(
  "Lincoln Ave & Diversey Pkwy", "Kosciuszko Park",
  "Indiana Ave & Roosevelt Rd", "Wood St & Augusta Blvd", "Sheridan Rd & Lawrence Ave",
  "Streeter Dr & Grand Ave"
), end_station_id = c(
  "152", "499",
  "255", "657", "323", "35"
)), class = "data.frame", row.names = c(NA, -6L))


Solution 1:[1]

Try this?

library(tidyverse)

trips_data2 %>% 
  mutate(start_station_name = fct_lump(start_station_name, n = 15)) %>% 
  count(start_station_name) %>% 
  mutate(start_station_name = fct_reorder(start_station_name, n)) %>% 
  ggplot(aes(x=start_station_name, y=n, fill=member_casual))) +
  geom_col()+
  coord_flip() +
  labs(x="Station", y="Total Trips", fill = "Member") + 
  ggtitle("Popular Start Stations") + 
  theme(plot.title = element_text(hjust = 0.5))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1