0

我使用 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”的部分,我不确定如何在我的数据中使用它。我对此没有太多想法。任何人都可以向我解释原始代码或可以帮助我的代码。如果我不清楚,请告诉我

4

1 回答 1

2

从字面上看,关于如何在 ggplot2 中生成choropleths的代码遍布 SO 和互联网。由于您没有提供任何数据,因此以下代码会生成一些数据,并为您提供具有正确投影的基本模板:

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绘制底图多边形,第二个覆盖填充。这个特殊的需要一些更美观的工作(边框、字体、清理图例标签等),但我不得不留下一些工作让你去做。

我强烈建议您为要呈现的数据使用有序的水平条形图。我怀疑地理是否会提供任何洞察力。

于 2015-03-19T20:42:30.827 回答