R中ggplot2中地图的区域缺失部分
Region missing parts of map in ggplot2 in R
我正在尝试使用 R 中的 ggplot2
为美国各州制作热图。当我这样做时,我 运行 遇到了同样的问题,详见 this and this post,但是解决方案 - 使用 merge
指定左连接或使用 dplyr::left_join
或 dplyr::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
不需要数据连接或可怕的投影。请注意,您的数据实际上不是连续的,您应该弄清楚应该如何对其进行标准化(可能找到每个州的估计推特用户数量并以此对其进行标准化,或者每 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
我正在尝试使用 R 中的 ggplot2
为美国各州制作热图。当我这样做时,我 运行 遇到了同样的问题,详见 this and this post,但是解决方案 - 使用 merge
指定左连接或使用 dplyr::left_join
或 dplyr::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
不需要数据连接或可怕的投影。请注意,您的数据实际上不是连续的,您应该弄清楚应该如何对其进行标准化(可能找到每个州的估计推特用户数量并以此对其进行标准化,或者每 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