在 R 中按北卡罗来纳州的县创建一个等值区来衡量暴力犯罪
Creating a choropleth in R measuring violent crimes by county in North Carolina
我想可视化北卡罗来纳州各县的暴力犯罪
我的数据集看起来有点像这样:
次区域violent_crime
alamance 396.39
alexander 130.38
alleghany 137.48
anson 513.65
ashe 78.32
avery 138.51
beaufort 328.74
...
这是我的代码 - 到目前为止,只有北卡罗来纳州的地图及其县界线是可视化的。
我试图只使用 ggplot 和地图,但我 运行 陷入了死胡同
...
library(plotly)
library(ggplot2)
library(maps)
library(dplyr)
crime.df <- read.csv(file="B:/Data/visualization/violent_crimes.csv", header=TRUE, sep=",")
vcdExtra::datasets
nc <- subset(states, region == "north carolina")
head(nc)
counties <- map_data("county")
nc_county <- subset(counties, region == "north carolina")
head(nc_county)
choropleth <- inner_join(nc_county, crime.df, by = "subregion")
choropleth <- chloropleth[!duplicated(chloropleth$order),]
ggplot(data = nc, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.2) +
geom_polygon(color = "black", fill = "gray") +
geom_polygon(data = nc_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA)
...
谢谢!
library(ggplot2)
library(dplyr)
# Get NC counties
nc_map <- tbl_df(map_data("county", region = "north carolina"))
# Simulate data since you didn't use dput() as the R section of SO instructs you to do
set.seed(1492)
data_frame(
subregion = unique(nc_map$subregion),
crime = sample(50:500, length(unique(nc_map$subregion)))
) -> crime_df
# Join the values to the map
nc_map <- left_join(nc_map, crime_df)
# Plot it
ggplot() +
geom_polygon(data=nc_map, color="black",
aes(x=long, y=lat, group=subregion, fill=crime)) +
viridis::scale_fill_viridis(name="Crime ", direction=-1) +
coord_map("polyconic") +
ggthemes::theme_map() +
theme(legend.position="bottom")
考虑:
- 将犯罪数据分为 ~5 组
- 确保您使用的是人均信息(基于县人口,因为您关注的是 NC)
- 按照 SO R 部分的说明使用
dput()
,以便您可以包含数据
- 尝试隔离错误并提供更好的错误描述
- 阅读预测并尝试将
ggalt::coord_proj()
与此 PROJ.4 字符串一起使用:+proj=aea +lat_1=34.0207760236743 +lat_2=36.37811477607033 +lon_0=-80.716552734375
对比我的懒惰出路示例。
我想可视化北卡罗来纳州各县的暴力犯罪
我的数据集看起来有点像这样:
次区域violent_crime
alamance 396.39
alexander 130.38
alleghany 137.48
anson 513.65
ashe 78.32
avery 138.51
beaufort 328.74
...
这是我的代码 - 到目前为止,只有北卡罗来纳州的地图及其县界线是可视化的。
我试图只使用 ggplot 和地图,但我 运行 陷入了死胡同
...
library(plotly)
library(ggplot2)
library(maps)
library(dplyr)
crime.df <- read.csv(file="B:/Data/visualization/violent_crimes.csv", header=TRUE, sep=",")
vcdExtra::datasets
nc <- subset(states, region == "north carolina")
head(nc)
counties <- map_data("county")
nc_county <- subset(counties, region == "north carolina")
head(nc_county)
choropleth <- inner_join(nc_county, crime.df, by = "subregion")
choropleth <- chloropleth[!duplicated(chloropleth$order),]
ggplot(data = nc, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.2) +
geom_polygon(color = "black", fill = "gray") +
geom_polygon(data = nc_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA)
...
谢谢!
library(ggplot2)
library(dplyr)
# Get NC counties
nc_map <- tbl_df(map_data("county", region = "north carolina"))
# Simulate data since you didn't use dput() as the R section of SO instructs you to do
set.seed(1492)
data_frame(
subregion = unique(nc_map$subregion),
crime = sample(50:500, length(unique(nc_map$subregion)))
) -> crime_df
# Join the values to the map
nc_map <- left_join(nc_map, crime_df)
# Plot it
ggplot() +
geom_polygon(data=nc_map, color="black",
aes(x=long, y=lat, group=subregion, fill=crime)) +
viridis::scale_fill_viridis(name="Crime ", direction=-1) +
coord_map("polyconic") +
ggthemes::theme_map() +
theme(legend.position="bottom")
考虑:
- 将犯罪数据分为 ~5 组
- 确保您使用的是人均信息(基于县人口,因为您关注的是 NC)
- 按照 SO R 部分的说明使用
dput()
,以便您可以包含数据 - 尝试隔离错误并提供更好的错误描述
- 阅读预测并尝试将
ggalt::coord_proj()
与此 PROJ.4 字符串一起使用:+proj=aea +lat_1=34.0207760236743 +lat_2=36.37811477607033 +lon_0=-80.716552734375
对比我的懒惰出路示例。