如何为R中地图中的不同区域选择不同的主题
How to choose different theme for separate regions in map in R
我想使用 ggplot 和 geom_polygons 在地图上绘制区域,但我希望特定区域只有边界,其他区域被填充(我只分析 3 个区域,但想显示周围环境的地图国家)
我正在使用 map_data 函数并选择我对 c("Syria","Israel","Greece") 感兴趣的区域,但也想显示像这样的区域埃及和利比亚,但只是边界,所以它们在视觉上并不突出。
这是我的代码:
mapdata = map_data("world", regions =c("Syria","Israel","Greece"))
plot2 = ggplot() + geom_polygon(data = mapdata, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3) + theme_classic()
稍后我将地理点绘制到它。
我试过类似的方法:
dummymap = map_data("world", regions = c("Egypt","Libya"))
[...] + geom_polygon(data = dummymap, aes(x=long, y = lat, group = group)) + theme_bw()
*我正在将主题更改为 bw 但它也输出经典的埃及和利比亚
所以我希望希腊、以色列和叙利亚被填满,而埃及和利比亚只接壤
这是一个 tidyverse
+ sf
解决方案
library( tidyverse )
library( sf )
#load countries
mapdata = map_data("world", regions =c("Syria","Israel","Greece","Egypt","Libya"))
#read in points
sf <- st_as_sf( mapdata, coords = c("long", "lat"), crs = 4326 ) %>%
#group geometries by region + subregion, convert to POLYGON
group_by( region, subregion ) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON") %>%
#repeat, but now group by region (=country), and convert to MULTIPOLYGON
group_by( region ) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast( "MULTIPOLYGON" )
#now plot the different countries, use `fill` to determine fill-color
#and use `aplha` for country-transparancy.
ggplot( data = sf) +
geom_sf( data = sf[ sf$region %in% c("Egypt","Libya"), ], alpha = 0 ) +
geom_sf( data = sf[ sf$region %in% c("Syria","Israel","Greece"), ], fill = "blue" )
结果
我不确定是否有必要将您的数据转换为 sf
-object,但它允许以不同的方式轻松(而且漂亮!)绘图,包括 leaflet
等强大的选项.
我想使用 ggplot 和 geom_polygons 在地图上绘制区域,但我希望特定区域只有边界,其他区域被填充(我只分析 3 个区域,但想显示周围环境的地图国家)
我正在使用 map_data 函数并选择我对 c("Syria","Israel","Greece") 感兴趣的区域,但也想显示像这样的区域埃及和利比亚,但只是边界,所以它们在视觉上并不突出。
这是我的代码:
mapdata = map_data("world", regions =c("Syria","Israel","Greece"))
plot2 = ggplot() + geom_polygon(data = mapdata, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3) + theme_classic()
稍后我将地理点绘制到它。
我试过类似的方法:
dummymap = map_data("world", regions = c("Egypt","Libya"))
[...] + geom_polygon(data = dummymap, aes(x=long, y = lat, group = group)) + theme_bw()
*我正在将主题更改为 bw 但它也输出经典的埃及和利比亚
所以我希望希腊、以色列和叙利亚被填满,而埃及和利比亚只接壤
这是一个 tidyverse
+ sf
解决方案
library( tidyverse )
library( sf )
#load countries
mapdata = map_data("world", regions =c("Syria","Israel","Greece","Egypt","Libya"))
#read in points
sf <- st_as_sf( mapdata, coords = c("long", "lat"), crs = 4326 ) %>%
#group geometries by region + subregion, convert to POLYGON
group_by( region, subregion ) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON") %>%
#repeat, but now group by region (=country), and convert to MULTIPOLYGON
group_by( region ) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast( "MULTIPOLYGON" )
#now plot the different countries, use `fill` to determine fill-color
#and use `aplha` for country-transparancy.
ggplot( data = sf) +
geom_sf( data = sf[ sf$region %in% c("Egypt","Libya"), ], alpha = 0 ) +
geom_sf( data = sf[ sf$region %in% c("Syria","Israel","Greece"), ], fill = "blue" )
结果
我不确定是否有必要将您的数据转换为 sf
-object,但它允许以不同的方式轻松(而且漂亮!)绘图,包括 leaflet
等强大的选项.