gpt4 book ai didi

r - 在 R 中制作突出显示特定县和点的州 map (ggmaps、maps、ggplot2)

转载 作者:行者123 更新时间:2023-12-04 03:03:13 24 4
gpt4 key购买 nike

我正在尝试制作一张州 map ,其中勾勒出各县的轮廓,一个县用蓝色着色,并带有一个特定度假胜地的点。 las,我无法为县上色或添加特定点。我的代码基于 http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html感谢您提供任何见解!

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)

states <- map_data("state")
dim(states)
ut_df <- subset(states, region == "utah")
head(ut_df)

counties <- map_data("county")
ut_county <- subset(counties, region == "utah")
head(ut_county)

ut_base <- ggplot(data = ut_df, mapping = aes(x = long, y = lat, group =
group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")

ut_base + theme_nothing() +
geom_polygon(data = ut_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA) # get the state border back on top

最佳答案

# Select a subregion
single_county <- subset(ut_county, subregion=="utah")

# Fill the selected subregion with a predefined color and
# plot a colored point with a specified long. and lat.
ut_base + theme_void() +
geom_polygon(data = ut_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA) +
geom_polygon(data = single_county, fill = "red", color = "white") +
geom_point(x=-111.8, y=40.2, col="blue", size=3)

enter image description here

关于r - 在 R 中制作突出显示特定县和点的州 map (ggmaps、maps、ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47112414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com