R dplyr group,ungroup,top_n和ggplot

时间:2017-04-05 10:23:28

标签: r ggplot2 group-by dplyr

我有一个具有多个值的对象,包括城市,州,年份和谋杀数量。我使用dplyr按城市进行分组,计算出这十大城市的所有年份的谋杀总数:

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)

我想仅为前十个城市绘制此图,但找不到'x = year',因为它已按城市分组。任何人都可以解释我是如何做到这一点的吗?

编辑:这是原始来源数据https://interactive.guim.co.uk/2017/feb/09/gva-data/UCR-1985-2015.csv 这是我的代码:

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)

1 个答案:

答案 0 :(得分:0)

好的还有一些小问题。这应该可以解决问题:

#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)