r - sf map 使用 inter_join 添加缺失值
问题描述
我有一个技术问题请教你。
read_sf("map.shp") %>% mutate(Groups = as.factor(Groups)) %>%
mutate(Groups = factor(Groups, levels = c(paste0(1:23)))) %>%
left_join(data, by = "cities_code") %>%
# Show map with cities border
ggplot() +
geom_sf(aes(fill = Groups), size = 0.4) +
# Color the different Groups, here 23 colors
stat_sf_coordinates(aes(size = observation)) +
# Put point with the size of my number of observations
scale_radius(range = c(1, 6)) +
geom_sf(fill = "transparent", color = "gray20", size = 1, data = . %>% group_by(Groups) %>% summarise()) +
# Show the border of my Groups
theme_bw()
这张地图正是我想要的。它代表按地区(“组”)细分的一个州的城市。但是我map.shp
和我之间data
有50个城市的差异,因为这些城市没有观察(所以没有“ stat_sf_coordinates(aes(size = observation))
”)。
我可以找到不同之处anti_join(data, by = "cities_code")
。我想要相同的地图,但请用红色标出缺失的城市。
谢谢
解决方案
这很简单:
read_sf("map.shp") %>% mutate(Groups = as.factor(Groups)) %>%
mutate(Groups = factor(Groups, levels = c(paste0(1:23)))) %>%
left_join(data, by = "cities_code") %>%
ggplot() +
geom_sf(aes(fill = Groups), size = 0.4) +
stat_sf_coordinates(aes(size = observation)) +
scale_radius(range = c(1, 6)) +
##
geom_sf(fill = "red", color = "gray40", size = 0.4, data = . %>% anti_join(data, by = "cities_code")) +
##
geom_sf(fill = "transparent", color = "gray20", size = 1, data = . %>% group_by(Groups) %>% summarise()) +
theme_bw()
推荐阅读
- javascript - 如何使用正则表达式找到后面没有单词字符的数字?
- c# - 无法修改“Transform.rotation”的返回值,因为它不是变量
- excel - 在excel中汇总文本字符串和数字
- kotlin - 如何检查用户是否已将照片上传到我在 Kotlin android 中的图像视图
- java - 在 android studio 的 2 个活动中携带意图数据
- swift - 如何使用 SwiftUI 将值数组写入 Firestore
- excel - 遍历行不遵守排序顺序
- laravel - 无法访问该站点,本地服务器在 Visual Studio 代码上说
- java - 使用 Cumulocity 微服务 JavaSDK 获取应用程序托管对象
- reactjs - 从 localForage getItem 返回一个布尔值