Romain
Romain

Reputation: 193

R dplyr group, ungroup, top_n and ggplot

I have an object with several values including cities, states, year and number of murders. I use dplyr to group it by city and calculate the total murders over all years for the top 10 cities like this:

MurderNb_reshaped2 %>%
  select(city, state, Year, Murders) %>%
  group_by(city) %>%
  summarise(total = sum(Murders)) %>%
  top_n(10, total) %>%
  ggplot(aes(x = Year, y = Murders, fill = "red")) +
  geom_histogram(stat = "identity") +
  facet_wrap(~city)

I would like to plot this for only the top ten cities, but 'x = year' is not found because it has been grouped by city. Can anyone explain how I can accomplish this?

EDIT: this the original source data https://interactive.guim.co.uk/2017/feb/09/gva-data/UCR-1985-2015.csv And here is my code:

Deaths <- read.csv("UCR-1985-2015.csv", stringsAsFactors = F)
MurderRate <- Deaths[, -c(5:35)]
MurderNb <- Deaths[, -c(36:66)]
colnames(MurderNb) <- gsub("X", "", colnames(MurderNb))
colnames(MurderNb) <- gsub("_raw_murder_num", "", colnames(MurderNb))

MurderNb_reshaped <-  melt(MurderNb, id = c("city", "Agency", "state", "state_short"))
colnames(MurderNb_reshaped) <- c("city", "Agency", "state", "state_short", "Year", "Murders")


MurderNb_reshaped2 <- MurderNb_reshaped

MurderNb_reshaped2 %>%
  select(city, state, Year, Murders) %>%
  group_by(city) %>%
  summarise(total = sum(Murders)) %>%
  top_n(10, total) %>%
  ggplot(aes(x = Year, y = Murders, fill = "red")) +
  geom_bar(stat = "identity") +
  facet_wrap(~city)

Upvotes: 1

Views: 1984

Answers (1)

Ian Wesley
Ian Wesley

Reputation: 3624

Ok there were a couple minor issue. This should do the trick:

#this gives you the top cities
topCities <- MurderNb_reshaped2 %>%
  select(city, state, Year, Murders) %>%
  group_by(city) %>%
  summarise(total = sum(Murders)) %>%
  top_n(10, total)

#you then need to filter your original data to be only the data for the top cities
MurderNb_reshaped2 <- filter(MurderNb_reshaped2, city %in% topCities$city)

ggplot(data = MurderNb_reshaped2,  aes(x = Year, y = Murders, fill = "red")) +
geom_bar(stat = "identity") +
facet_wrap(~city)

Upvotes: 3

Related Questions