我使用 Hadley 提供的代码复制了 Choropleth Map。我的数据是一个包含国家名称的csv,没有。谋杀未遂,不。Assaultand 号 强奸。我需要绘制一个国家的地理热图,其中最深的颜色代表该州的犯罪数量较多,依此类推。
代码:(我尝试复制)
library(ggplot2)
library(maps)
unemp2 <- read.csv("USA_State.csv", header = T, stringsAsFactors = F)
county_df1 <- map_data("state")
names(county_df1) <- c("long", "lat", "group", "order", "state", "state1")
county_df1$state1 <- NULL
state_df <- map_data("state")
# Combine together
choropleth <- merge(county_df1, unemp2, by = c("state"))
choropleth <- choropleth[order(choropleth$order), ]
# Discretise rate to use with Brewer colour scheme - many options here
# choropleth$rate_d <- cut_number(choropleth$rate, 5)
# choropleth$rate_d <- cut_interval(choropleth$rate, 5)
# Nathan's choice is a little odd:
choropleth$rate_d <- cut(choropleth$Assault, breaks = c(seq(0, 10, by = 2), 35))
# Once you have the data in the right format, recreating the plot is straight
# forward.
library(scales)
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
scale_fill_brewer(palette = "PuRd")
# Takes a while to draw because ggplot2 not very efficient with large numbers
# of polygons :(
#
正在创建“choropleth$rate_d”的部分,我不确定如何在我的数据中使用它。我对此没有太多想法。任何人都可以向我解释原始代码或可以帮助我的代码。如果我不清楚,请告诉我