Kit
Kit

Reputation: 27

Wanting Top ten to plot in ggplot

I have 16000 ish missing persons data that I am trying to order by Count and then plot on a graph. this is the code i am using. I am wanting to plot only the top ten.

mp.city <-mp.All %>%
  group_by(State, City, Sex) %>%
  summarise(Count = n()) 

mp.city %>%
  arrange(desc(Count)) %>%
  slice(1:10) %>%
  ggplot(aes(y = City)) +
  geom_bar()

the code will run but the plot is garbage. Any help would be amazing thank you!

enter image description here

Upvotes: 0

Views: 184

Answers (1)

s__
s__

Reputation: 9495

I think you can manage it con head():

url<-'https://raw.githubusercontent.com/kitapplegate/fall2020/master/mpAll.csv'
mp.All<-read.csv(url)

library(ggplot2)
library(dplyr)

mp.city <-mp.All %>%
  group_by(State, City, Sex) %>%
  summarise(Count = n()) 

mp.city %>%
  # sort
  arrange(desc(Count)) %>%
  # top 10 overall
  head(10) %>%
  # plot ordered
  ggplot(aes(x = reorder(City,Count), y = Count))+
  geom_bar( stat = "identity") +
  # flipped
  coord_flip() +
  # label for x axis (flipped)
  xlab("City")

enter image description here

P.S.
Next time try to share your data with dput(head(yourdata)) and posting the result, it's way better.

Upvotes: 1

Related Questions