我正在尝试绘制一张组织感兴趣的国家的精美地图。我制作了下面的地图,但想通过删除国家边界来使事情变得更加优雅,除了两个感兴趣的国家共享边界的地方(例如南非和津巴布韦之间)。我已经看过许多教程,但找不到任何东西。
最后,我想为城市数据添加一个图例。
这是代码:
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")
产生:
此外,我想用绿色的比例代表国家,并按以下顺序显示图例:“兴趣”,“过去”,“当前”;从浅绿色变为深绿色。当我取消选中注释行“ 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"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句