我用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”的部分,我不知道如何在我的数据中使用它。我对此并不太了解。 任何人都可以向我解释原始代码或可以帮助我的代码。 如果我不清楚,请告诉我
答案 0 :(得分:2)
关于如何在整个SO和互联网上的ggplot2中生成等值区的代码,实际上有一个 plethora 代码。以下代码生成一些数据,因为您没有提供任何数据,并为您提供了具有正确投影的基本模板:
library(ggplot2)
library(scales)
# Cleans the canvas well for a map ----------------------------------------
theme_map <- function(base_size=9, base_family="") {
require(grid)
theme_bw(base_size=base_size, base_family=base_family) %+replace%
theme(axis.line=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
legend.justification = c(0,0),
legend.position = c(0,0)
)
}
# Generate some data ------------------------------------------------------
set.seed(100)
choro_data <- data.frame(region=tolower(state.name),
Murder=sample(1:20, length(state.name), replace=TRUE),
Assault=sample(c(1:10, 35), length(state.name), replace=TRUE),
Rape=sample(1:40, length(state.name), replace=TRUE),
stringsAsFactors=FALSE)
choro_data$rate_d <- cut(choro_data$Assault, breaks = c(seq(0, 10, by = 2), 35))
# The core choropleth code ------------------------------------------------
us <- map_data("state")
us <- fortify(us, region="region")
# plot --------------------------------------------------------------------
gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
aes(x=long, y=lat, map_id=region, group=group),
fill="white", color="white", size=0.25)
gg <- gg + geom_map(data=choro_data, map=us,
aes(fill=rate_d, map_id=region),
color="#7f7f7f", size=0.25)
gg <- gg + coord_map("albers", lat0=39, lat1=45)
gg <- gg + scale_fill_brewer(name="Rate")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg
两个geom_map
调用可以使数据框的值可能缺少区域ID(并且不必将一堆数据连接到地图本身)。第一个geom_map
绘制基本地图多边形,第二个叠加填充。这个特殊的工作需要一些更美学的工作(边框,字体,清理图例标签等),但我不得不离开一些工作。
我强烈建议您尝试呈现的数据的有序水平条形图。我怀疑地理位置会给它带来任何见解。