【问题标题】:Choropleth Map of State Population州人口等值线图
【发布时间】:2018-11-01 20:13:11
【问题描述】:

试图创建一个显示州人口的等值线图,同时标记州府城市。我最初有两个数据框,但无法将 ggplot 1 添加到 ggplot 2,所以我将两个数据框组合在一起,表格的一部分如下所示:

基本上试图将这两个图像组合在一起:

我写了

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")

但地图看起来不太好:

我认为是多边形部分让我失望,但不知道该怎么办。

【问题讨论】:

  • 在没有数据可以立即复制它的情况下,我只能说我认为您的group 需要是您所在州的任何分组,而不是资本。它使用大写字母作为多边形的点。

标签: r ggplot2 choropleth


【解决方案1】:

您将需要 shapefile,或者至少需要知道将数据映射到的边界。

根据您前几天提出的问题,您仍然可以使用statescale_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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 2020-07-28
    • 2012-10-17
    • 2019-10-08
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多