在 r 中使用 ggmap 和 ggplot 绘制伊利诺伊州
Plotting Illinois with ggmap and ggplot in r
我正在尝试按县绘制伊利诺伊州底图。
我加载的库:
library(ggplot2)
library(maps)
library(ggmap)
library(mapdata)
这是我的代码:
states <- map_data("state")
IL <- subset(states, region %in% c("illinois"))
counties <- map_data("county")
IL_county <- subset(counties, region == "illinois")
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
il_base
il_base +
geom_polygon(data = IL_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA)
il_base
剧情不错,基本勾勒出国家的轮廓。但是,只要我向其中添加 geom_polygon
,它就会像这样映射县:
这不是伊利诺伊州各县的样子。我这里做错了什么?
我通过修改底图解决了这个问题:
# Add group
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
我正在尝试按县绘制伊利诺伊州底图。 我加载的库:
library(ggplot2)
library(maps)
library(ggmap)
library(mapdata)
这是我的代码:
states <- map_data("state")
IL <- subset(states, region %in% c("illinois"))
counties <- map_data("county")
IL_county <- subset(counties, region == "illinois")
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
il_base
il_base +
geom_polygon(data = IL_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA)
il_base
剧情不错,基本勾勒出国家的轮廓。但是,只要我向其中添加 geom_polygon
,它就会像这样映射县:
这不是伊利诺伊州各县的样子。我这里做错了什么?
我通过修改底图解决了这个问题:
# Add group
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()