如何使用R将美国所有州与每个州发生的犯罪数量对应起来?

kav*_*esh 4 r ggplot2

我仍在学习R,我想用每个州发生的犯罪数量的标签映射美国。我要创建下面的图像。

我使用了下面的代码,这些代码可在线获得,但无法标记为“犯罪无”。

    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())
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?

mis*_*use 5

要将文本添加到绘图(在这种情况下为地图),需要文本标签和文本坐标。这是处理您的数据的一种方法:

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())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

编辑:与更新的问题:

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

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

然后绘制:

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())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果要比较所有年份,我建议不要使用文本,因为这样会很混乱:

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

人们可以看到这些年来变化不大。

我相信更多有用的情节是:

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明