gpt4 book ai didi

r - 如何使用 R 映射美国所有州和每个州发生的犯罪数量?

转载 作者:行者123 更新时间:2023-12-03 14:18:38 28 4
gpt4 key购买 nike

我仍在学习 R,我想用每个州发生的犯罪数量的标签来绘制美国各州的 map 。我想创建下面的图像。

我使用了以下在线代码,但我无法标记犯罪数量。

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

请问有人可以帮我吗?

最佳答案

要将文本添加到绘图(在本例中为 map ),需要文本标签和文本坐标。这是处理您的数据的方法:

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

enter image description here

在这里,我使用突击编号作为标签,将每个州的纬度和经度坐标的最大值和最小值的平均值用作文本坐标。某些州的坐标可能更好,可以手动添加或使用选定的城市坐标。

编辑:更新的问题:

首先选择犯罪的年份和类型并汇总数据
homicide %>%
filter(Year == 1980 & Crime.Type == "Murder or Manslaughter") %>%
group_by(State) %>%
summarise(n = n()) %>%
mutate(state = tolower(State)) -> homicide_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) %>%
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())

enter image description here

如果有人想比较所有年份,我建议在没有文字的情况下进行,因为它会非常困惑:
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)

enter image description here

人们可以看到这些年来没有太大变化。

我相信一个更翔实的情节是:
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()

enter image description here

关于r - 如何使用 R 映射美国所有州和每个州发生的犯罪数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46859779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com