我正在尝试绘制一张组织感兴趣的国家的精美地图。我制作了下面的地图,但想通过删除国家/地区边界(除两个感兴趣的国家/地区共用边界的地方)来使事情变得更加优雅。在南非和津巴布韦之间。我已经看过许多教程,但对此一无所获。
最后,我想为城市数据添加一个图例。
代码如下:
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数据,产生了下面的图像。我尝试通过多种方式添加它,但是无法使其正常工作。为了弄清楚我的意思,这是当我取消选中该注释时代码生成的内容:
任何一个问题的帮助将不胜感激。
答案 0 :(得分:1)
分层创建图。这也应解决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"))