【问题标题】:Region missing parts of map in ggplot2 in RR中ggplot2中地图的区域缺失部分
【发布时间】:2016-03-16 00:52:35
【问题描述】:

我正在尝试使用 R 中的 ggplot2 为美国各州制作热图。这样做时,我遇到了与 thisthis 帖子中详述的相同的问题,但是解决方案 -使用merge 指定左连接或使用dplyr::left_joindplyr::inner_join 似乎不起作用。

以下是我使用my_data 中的数据创建以下绘图的操作,其中华盛顿、新泽西、纽约的部分地区缺失或颜色错误:

library(ggplot2)
library(ggthemes)

str(my_data)

> str(data)
'data.frame':   50 obs. of  2 variables:
 $ region          : chr  "alaska" "alabama" "arkansas" "arizona" ...
 $ number_of_tweets: int  0 10967 5130 462 26489 267 1464 2431 9777 7359 ...

states <- map_data("state")

choro <- dplyr::left_join(states, my_data, by = "region")

ggplot() +
      geom_polygon(data = choro, 
                   aes(x = long, y = lat, group = region, fill = number_of_tweets)) +
      scale_fill_gradient(low = "#deebf7", high = "#3182bd") +
      theme_map()

这是my_data 的内容,以防有助于重现性:

region,number_of_tweets
alabama,10929
alaska,0
arkansas,5107
arizona,452
california,26299
colorado,265
connecticut,1459
delaware,2418
florida,9666
georgia,7306
hawaii,2486
iowa,29229
idaho,7607
illinois,10221
indiana,20700
kansas,32252
kentucky,11098
louisiana,938
massachusetts,10764
maryland,4091
maine,5770
michigan,47335
minnesota,1079
missouri,1079
mississippi,1273
montana,11606
north carolina,22354
north dakota,6294
nebraska,7319
new hampshire,7185
new jersey,26850
new mexico,0
nevada,7918
new york,16007
ohio,8284
oklahoma,63551
oregon,1120
pennsylvania,908
rhode island,10240
south carolina,6296
south dakota,3559
tennessee,4765
texas,30235
utah,15019
virginia,5541
vermont,16444
washington,7506
wisconsin,7817
wyoming,10496
west virginia,0

【问题讨论】:

  • 您是否忘记粘贴整个代码或最后的+ 是错字?另外,您使用了choro_data 而不是choro
  • @TejaK 感谢那些是复制代码时的拼写错误。
  • 你是什么意思:“华盛顿、新泽西、纽约的部分地区丢失或颜色错误”?根据您提供的数据和地图图例,它们看起来颜色正确。
  • geom_polygon,你想要group = group。这将解决华盛顿问题。
  • group = region 更改为 group = group

标签: r ggplot2 maps


【解决方案1】:

不需要数据连接或可怕的投影。请注意,您的数据确实不是连续的,您应该弄清楚应该如何对其进行规范化(可能找到每个州的推特用户估计数并以此规范化,或每 100,000 人使用)。

library(ggplot2)
library(ggthemes)
library(viridis)

my_data <- structure(list(region = c("alabama", "alaska", "arkansas", "arizona", 
"california", "colorado", "connecticut", "delaware", "florida", 
"georgia", "hawaii", "iowa", "idaho", "illinois", "indiana", 
"kansas", "kentucky", "louisiana", "massachusetts", "maryland", 
"maine", "michigan", "minnesota", "missouri", "mississippi", 
"montana", "north carolina", "north dakota", "nebraska", "new hampshire", 
"new jersey", "new mexico", "nevada", "new york", "ohio", "oklahoma", 
"oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", 
"tennessee", "texas", "utah", "virginia", "vermont", "washington", 
"wisconsin", "wyoming", "west virginia"), number_of_tweets = c(10929L, 
0L, 5107L, 452L, 26299L, 265L, 1459L, 2418L, 9666L, 7306L, 2486L, 
29229L, 7607L, 10221L, 20700L, 32252L, 11098L, 938L, 10764L, 
4091L, 5770L, 47335L, 1079L, 1079L, 1273L, 11606L, 22354L, 6294L, 
7319L, 7185L, 26850L, 0L, 7918L, 16007L, 8284L, 63551L, 1120L, 
908L, 10240L, 6296L, 3559L, 4765L, 30235L, 15019L, 5541L, 16444L, 
7506L, 7817L, 10496L, 0L)), .Names = c("region", "number_of_tweets"
), class = "data.frame", row.names = c(NA, -50L))

states <- map_data("state")

my_data$cut <- as.character(cut(my_data$number_of_tweets, 
                                breaks=pretty(x=my_data$number_of_tweets, n=7),
                                labels=pretty(x=my_data$number_of_tweets, n=7)[-1]))

my_data$cut <- ifelse(is.na(my_data$cut), 0, my_data$cut)

gg <- ggplot()
gg <- gg + geom_map(data=states, map=states,
                    aes(x=long, y=lat, map_id=region),
                    color="white", size=0.1, fill=NA)
gg <- gg + geom_map(data=my_data, map=states,
                    aes(fill=cut, map_id=region),
                    color="white", size=0.1)
gg <- gg + scale_fill_viridis(name="# Tweets", discrete=TRUE, begin=0.1, end=0.9)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 2012-09-09
    • 2018-04-12
    • 2020-02-17
    • 2017-06-23
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多