我正在努力制作一个优雅的国家地图,其中一个组织有兴趣工作.我已经制作了下面的地图,但是除了两个感兴趣的国家共享边界之外,除了南非和津巴布韦之外,还希望通过移除国界来使事情变得更加优雅.我已经通过了一些教程,但在这方面找不到任何东西.
最后,我想为城市数据添加一个图例.
这是代码:
world <- map_data("world")
countries <- read_excel("country_table.xlsx", sheet = 3) #table of coutries with interest
world3 <- merge(world, countries, all.x = TRUE)
world4 <- arrange(world4, order)
city <- read_excel("country_table.xlsx", sheet = 4) #city data
city$long <- as.numeric(city$long)
city$lat <- as.numeric(city$lat)
ggplot(world4, aes(x = long, y = lat, group = group, fill = interest)) +
geom_polygon(col = "white") +
#scale_fill_manual(breaks = c("interest", "past", "current"), values = c("#4dc11d","#26660b","#9def7a")) +
theme_map() +
coord_fixed(xlim = c(-130, 160), ylim = c(-50, 75), ratio = 1.3) +
geom_point(data = city, aes(x= long, y = lat), shape = 21, inherit.aes = F, size = 2, col = "black", fill = "yellow")
Run Code Online (Sandbox Code Playgroud)
生产:
此外,我想使用绿色标度来代表国家,顺序为传奇:"兴趣","过去","当前"; 从浅绿色到深绿色.在我取消选中注释行"scale_fill_manual"的那一刻,我丢失了生成下面图像的所有NA数据.我尝试通过多种方式添加它,但无法使其工作.为了清楚我的意思,这是我取消选中该注释时代码产生的内容:
任何问题的任何帮助将不胜感激.
分层形成你的情节。这也应该可以解决这个问题,因为您的数据框中scale_fill_manual不会有值。NA
除了您所做的之外:
# subset for all countries that are filled by a color
library(dplyr)
world5 <- world4 %>% dplyr::filter(!is.na(interest))
ggplot() +
# first layer: all countries, no fill, no white outlines
geom_polygon(data = world4,
aes(x = long, y = lat, group = group)) +
# second layer: only countries with a fill
geom_polygon(data = world5,
aes(x = long, y = lat, group = group, fill = interest),
color = "white") +
# apply the color scale
scale_fill_manual(breaks = c("interest", "past", "current"),
values = c("#4dc11d","#26660b","#9def7a"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1203 次 |
| 最近记录: |