如何用R来映射美国所有州与每个州发生的犯罪数量? [英] How to map all the states of US using R with the number of crimes occurred in each state?

查看:257
本文介绍了如何用R来映射美国所有州与每个州发生的犯罪数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在学习关于R的知识,并且我想将美国各州与每个州发生的犯罪数量进行对比。我想创建下面的图片。



我用下面的代码在网上可以找到,但是我不能标出犯罪的数量。

  library(ggplot2)
library(fiftystater)

data(fifty_states)
罪行< - data.frame(state = tolower(rownames(USArrests)),USArrests)
p < - ggplot(crime,aes(map_id = state))+
#map points到50_状态数据
geom_map(aes(fill = Assault),map = fifty_states)+
expand_limits(x = fifty_states $ long,y = fifty_states $ lat)+
coord_map()+


scale_x_continuous(breaks = NULL)+ scale_y_continuous(breaks = NULL)+
labs(x =,y =)+ theme(legend.position =底部,
panel.background = element_blank())

请有人可以帮我吗?

解决方案

要向文本添加文本(本例中为地图),需要文本标签和文本坐标。这里有一个你的数据的方法:

pre $ library $($ $ $ $ $ $ $ $ $ $ $ $ $ b $ library $ tidyverse)

data(fifty_states)

ggplot(data = crime,aes(map_id = state))+
geom_map(aes(fill = Assault) ,color =black,map = fifty_states)+
expand_limits(x = fifty_states $ long,y = fifty_states $ lat)+
coord_map()+
geom_text(data = fifty_states%> ;%
group_by(id)%>%
summary(lat = mean(c(max(lat),min(lat))),
long = mean(c(max long),min(long))))%>%
mutate(state = id)%>%
left_join(犯罪,by =state),aes(x = long,y = lat,label = Assault))+
scale_x_continuous(breaks = NULL)+ scale_y_continuous(breaks = NULL)+
labs(x =,y =)+ theme(legend.position = bottom,
panel.background = element_blank())



在这里,我使用Assault号作为标签,并将每个状态的经纬度坐标的最大值和最小值的均值作为文本坐标。对于某些州来说,坐标可能会更好,可以手动添加它们或者使用选定的城市坐标。

编辑:带更新后的问题:

首先选择年份和犯罪类型并汇总数据

 凶杀案%>%
filter(Year == 1980& Crime.Type ==Murder or Manslaughter)%>%
group_by(State)%>%
summary(n = n()) %>%
mutate(state = tolower(State)) - >凶杀案_1980

然后绘制:

  ggplot(data = homicide_1980,aes(map_id = state))+ 
geom_map(aes(fill = n),color =black,map = fifty_states)+
expand_limits(x = fifty_states $ long,y = fifty_states $ lat)+
coord_map()+
geom_text(data = fifty_states%>%
group_by(id)%>%
总结(lat =平均值(c(max(lat),min(lat))),
long =平均值(c(max(long),min(long))))%>%
mutate(state = id)%>%
left_join(homicide_1980,by =state),aes(x = long,y = lat,label = n))+
scale_x_continuous = NULL)+ scale_y_continuous(breaks = NULL)+
labs(x =,y =)+ theme(legend.position =bottom,
panel.background = element_blank())



如果您想比较所有年,我建议这样做,因为它会非常混乱:

 杀人%>%
过滤(州,年)%>%
汇总(n = n())%>%
mutate( state = tolower(State))%>%
ggplot(aes(map_id = state))+
geom_map(aes(fill = n),color =black,map = fifty_states)+
expand_limits(x = fifty_states $ long,y = fifty_states $ lat)+
coord_map()+
scale_x_continuous(breaks = NULL)+ scale_y_continuous(breaks = NULL)+
labs( x =,y =)+ theme(legend.position =bottom,
panel.background = element_blank())+
facet_wrap(〜Year,ncol = 5)



可以看到这些年来没有太多变化。



我相信一个更丰富的情节是:

  homocide%>%
过滤器(Crime.Type ==Murder or Manslaughter)%>%
group_by(州,年)%>%
汇总(n = n())%>%
mutate(state = tolower(State))%>%
ggplot()+
geom_line(aes(x = Year,y = n))+
facet_wrap(〜state,ncol = 6,scales =free_y)+
theme_bw()


I am still learning about R and I want to map the States of US with the labels of number of crimes occurred in each state. I want to create the below image.

I used the below code which was available online but I could not label the No of crimes.

    library(ggplot2)
    library(fiftystater)

    data("fifty_states") 
    crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
    p <- ggplot(crimes, aes(map_id = state)) + 
      # map points to the fifty_states shape data
      geom_map(aes(fill = Assault), map = fifty_states) + 
      expand_limits(x = fifty_states$long, y = fifty_states$lat) +
      coord_map() +


scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") + theme(legend.position = "bottom", 
        panel.background = element_blank())

Please can someone help me ?

解决方案

To add text to a plot (map in this case) one needs the text label and the coordinates of the text. Here is an approach with your data:

library(ggplot2)
library(fiftystater)
library(tidyverse)

data("fifty_states") 

ggplot(data= crimes, aes(map_id = state)) + 
  geom_map(aes(fill = Assault),  color= "black", map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  geom_text(data = fifty_states %>%
              group_by(id) %>%
              summarise(lat = mean(c(max(lat), min(lat))),
                        long = mean(c(max(long), min(long)))) %>%
              mutate(state = id) %>%
              left_join(crimes, by = "state"), aes(x = long, y = lat, label = Assault ))+
  scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") + theme(legend.position = "bottom", 
                               panel.background = element_blank())

Here I used the Assault number as label and mean of the maximum and minimum of lat and long coordinates of each state as text coordinates. The coordinates could be better for some states, one can add them by hand or use chosen city coordinates.

EDIT: with the updated question:

First select the year and type of crime and aggregate the data

homicide %>%
  filter(Year  == 1980 & Crime.Type == "Murder or Manslaughter") %>%
  group_by(State) %>%
  summarise(n = n()) %>%
  mutate(state = tolower(State)) -> homicide_1980

and then plot:

ggplot(data = homicide_1980, aes(map_id = state)) + 
  geom_map(aes(fill = n),  color= "black", map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  geom_text(data = fifty_states %>%
              group_by(id) %>%
              summarise(lat = mean(c(max(lat), min(lat))),
                        long = mean(c(max(long), min(long)))) %>%
              mutate(state = id) %>%
              left_join(homicide_1980, by = "state"), aes(x = long, y = lat, label = n))+
  scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") + theme(legend.position = "bottom", 
                               panel.background = element_blank())

If one wants to compare all years I suggest doing it without text since it will be very cluttered:

homicide %>%
  filter(Crime.Type == "Murder or Manslaughter") %>%
  group_by(State, Year) %>%
  summarise(n = n()) %>%
  mutate(state = tolower(State)) %>%
  ggplot(aes(map_id = state)) + 
  geom_map(aes(fill = n),  color= "black", map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") + theme(legend.position = "bottom", 
                               panel.background = element_blank())+
  facet_wrap(~Year, ncol = 5)

One can see not much has changed during the years.

I trust a more informative plot is:

homocide %>%
  filter(Crime.Type == "Murder or Manslaughter") %>%
  group_by(State, Year) %>%
  summarise(n = n()) %>%
  mutate(state = tolower(State)) %>%
  ggplot()+
  geom_line(aes(x = Year, y = n))+
  facet_wrap(~state, ncol = 6, scales= "free_y")+
  theme_bw()

这篇关于如何用R来映射美国所有州与每个州发生的犯罪数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆