gpt4 book ai didi

r - 世界地图ggplot2上的边框和颜色

转载 作者:行者123 更新时间:2023-12-04 21:03:59 26 4
gpt4 key购买 nike

我正在尝试制作一个组织有兴趣在其中工作的国家的精美 map 。我制作了下面的 map ,但希望通过删除国家边界来使事情变得更加优雅,除了两个感兴趣的国家共享边界之外的所有地方 - 例如。在南非和津巴布韦之间。我已经阅读了许多教程,但找不到任何关于此的内容。

最后我想为城市数据添加一个图例。

这是代码:

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")

产生:

enter image description here

此外,我想用绿色的比例来代表国家,图例的顺序是:“兴趣”、“过去”、“当前”;从浅绿色变为深绿色。在我取消选中注释行“scale_fill_manual”时,我丢失了所有 NA 数据,生成了下面的图像。我尝试通过多种方式添加它,但无法使其正常工作。为了清楚我的意思,这是我取消选中该评论时代码产生的内容:

enter image description here

对于这两个问题的任何帮助将不胜感激。

最佳答案

分层形成你的情节。 这也应该注意 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"))

关于r - 世界地图ggplot2上的边框和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com