州人口等值线图
Choropleth Map of State Population
正在尝试创建一个显示各州人口的等值线图,同时标注首府城市。我最初有两个数据框,但无法将 ggplot 1 添加到 ggplot 2,所以我将两个数据框组合在一起,table 的一部分如下所示:
基本上是尝试将这两张图片组合在一起:
和
我写了
ggplot(spr, aes(long, lat)) + borders("state") + geom_point() +
coord_quickmap() +geom_label_repel(aes(label = city), size = 2) +
geom_polygon(aes(long, lat, group = capital, fill = pcls),color = "grey") +
coord_map("bonne", parameters=45) +ggthemes::theme_map() +
scale_fill_brewer(palette = "Reds")
但地图看起来不对:
我认为是多边形部分让我失望,但不确定该怎么做。
您将需要 shapefile,或者至少要知道将数据映射到的边界。
根据你前几天的问题,你仍然可以使用 state
。 scale_fill_brewer
设计用于离散变量。使用 scale_fill_gradientn
,指定 brewer.pal
。根据需要在其中添加 capitals
层。
library(ggplot2)
library(usmap)
library(maps)
library(ggrepel)
library(ggthemes)
us <- map_data("state") # get the data to plot and map data to
data(statepop)
pops <- statepop
pops$full <- tolower(pops$full)
ggplot() + geom_map(data = us, map = us, aes(long, lat, map_id = region), fill = "#ffffff", color = "#ffffff", size = 0.15) +
geom_map(data = pops, map = us, aes(fill = pop_2015, map_id = full), size = 0.15) +
coord_map("bonne", parameters=45) +
scale_fill_gradientn(colors = brewer.pal(9, "Reds")) + #adjust the number as necessary
borders("state") +
ggthemes::theme_map()
正在尝试创建一个显示各州人口的等值线图,同时标注首府城市。我最初有两个数据框,但无法将 ggplot 1 添加到 ggplot 2,所以我将两个数据框组合在一起,table 的一部分如下所示:
基本上是尝试将这两张图片组合在一起:
和
我写了
ggplot(spr, aes(long, lat)) + borders("state") + geom_point() +
coord_quickmap() +geom_label_repel(aes(label = city), size = 2) +
geom_polygon(aes(long, lat, group = capital, fill = pcls),color = "grey") +
coord_map("bonne", parameters=45) +ggthemes::theme_map() +
scale_fill_brewer(palette = "Reds")
但地图看起来不对:
我认为是多边形部分让我失望,但不确定该怎么做。
您将需要 shapefile,或者至少要知道将数据映射到的边界。
根据你前几天的问题,你仍然可以使用 state
。 scale_fill_brewer
设计用于离散变量。使用 scale_fill_gradientn
,指定 brewer.pal
。根据需要在其中添加 capitals
层。
library(ggplot2)
library(usmap)
library(maps)
library(ggrepel)
library(ggthemes)
us <- map_data("state") # get the data to plot and map data to
data(statepop)
pops <- statepop
pops$full <- tolower(pops$full)
ggplot() + geom_map(data = us, map = us, aes(long, lat, map_id = region), fill = "#ffffff", color = "#ffffff", size = 0.15) +
geom_map(data = pops, map = us, aes(fill = pop_2015, map_id = full), size = 0.15) +
coord_map("bonne", parameters=45) +
scale_fill_gradientn(colors = brewer.pal(9, "Reds")) + #adjust the number as necessary
borders("state") +
ggthemes::theme_map()