R:How 在地图上显示城市级别的数据以及如何在地图的一个特定区域上缩放它
R:How to display city level data on map and how to ZOOM it on one specific region of the map
我正在使用 usmap 和 ggplot 在地图上绘制人口图。我的数据有两列 - 人口和邮政编码。
问题:如何使用相同的图书馆显示城市级别的数据,或者如果您知道其他图书馆可以完成这项工作。
问题:我正在绘制加州地图,我想放大洛杉矶县和附近的县。
下面的代码给了我一张漂亮的加利福尼亚地图和人口作为颜色。
library(usmap)
library(ggplot2)
usmap::plot_usmap("counties",
include = ("CA") )
plot_usmap(data = data, values = "pop_2015", include = c("CA"), color = "grey") +
theme(legend.position = "right")+scale_fill_gradient(trans = "log10")
tigris
软件包使下载邮政编码列表区域变得相当简单。您可以下载一个简单的要素数据框,因此使用 dplyr
函数按邮政编码连接数据非常容易。这是一个简单的例子:
library(tigris)
library(dplyr)
library(ggplot2)
df <- zctas(cb = TRUE,
starts_with = c("778"),
class = "sf")
## generate some sample data that
## can be joined to the downloaded data
sample_data <- tibble(zips = df$ZCTA5CE10,
values = rnorm(n = df$ZCTA5CE10))
## left join the sample data to the downloaded data
df <- df %>%
left_join(sample_data,
by = c("ZCTA5CE10" = "zips"))
## plot something
ggplot(df) +
geom_sf(aes(fill = values))
我正在使用 usmap 和 ggplot 在地图上绘制人口图。我的数据有两列 - 人口和邮政编码。
问题:如何使用相同的图书馆显示城市级别的数据,或者如果您知道其他图书馆可以完成这项工作。
问题:我正在绘制加州地图,我想放大洛杉矶县和附近的县。
下面的代码给了我一张漂亮的加利福尼亚地图和人口作为颜色。
library(usmap)
library(ggplot2)
usmap::plot_usmap("counties",
include = ("CA") )
plot_usmap(data = data, values = "pop_2015", include = c("CA"), color = "grey") +
theme(legend.position = "right")+scale_fill_gradient(trans = "log10")
tigris
软件包使下载邮政编码列表区域变得相当简单。您可以下载一个简单的要素数据框,因此使用 dplyr
函数按邮政编码连接数据非常容易。这是一个简单的例子:
library(tigris)
library(dplyr)
library(ggplot2)
df <- zctas(cb = TRUE,
starts_with = c("778"),
class = "sf")
## generate some sample data that
## can be joined to the downloaded data
sample_data <- tibble(zips = df$ZCTA5CE10,
values = rnorm(n = df$ZCTA5CE10))
## left join the sample data to the downloaded data
df <- df %>%
left_join(sample_data,
by = c("ZCTA5CE10" = "zips"))
## plot something
ggplot(df) +
geom_sf(aes(fill = values))