gpt4 book ai didi

r - 如何根据列条目向 ggplot2 中的 map 图添加额外的图例?

转载 作者:行者123 更新时间:2023-12-04 10:11:06 25 4
gpt4 key购买 nike

数据:Data

代码:

palette = brewer.pal(11,"RdYlGn")    # ColorBrewewr.org spectral palette, 11 colors
ggmap_byscen = ggplot(wmap_byscen.df[wmap_byscen.df$variable !=c("AVG") &
wmap_byscen.df$ID_1 !=c("0"),], aes(x=long, y=lat, group=group))
ggmap_byscen = ggmap_byscen + geom_polygon(aes(fill=value)) + facet_wrap(~ variable)
ggmap_byscen = ggmap_byscen + geom_path(colour="grey50", size=.1)
ggmap_byscen = ggmap_byscen + geom_text(aes(x=c.long, y=c.lat, label=ID_1),size=5)
ggmap_byscen = ggmap_byscen + scale_fill_gradientn(name="% Change",colours=palette)
ggmap_byscen = ggmap_byscen + coord_fixed(xlim = longlimits, ylim = latlimits)
ggmap_byscen = ggmap_byscen + scale_y_continuous(breaks=seq(-60,90,30), labels=c("60ºS","30ºS","0º","30ºN","60ºN","90ºN"))
ggmap_byscen = ggmap_byscen + scale_x_continuous(breaks=seq(-180,180,45), labels=c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE"))
ggmap_byscen = ggmap_byscen + labs(x="",y="",title="Average yield impacts across all crops across\nby climate scenarios (% change)")
ggmap_byscen = ggmap_byscen + theme(plot.title=element_text(size=rel(2), hjust=0.5, vjust=1.5, face="bold"),
legend.text=element_text(size=17),
legend.position="left",legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.4), hjust=0.5, vjust=1),
panel.background = element_rect(fill = "white", colour = "gray95"),
strip.text = element_text(size=18),
axis.text.x = element_text(size=16),
axis.text.y = element_text(size=16))
ggmap_byscen

结果: Map

问题:我希望添加由数据框中“标签”列定义的附加图例,以标识 map 上的区域。最好,我希望图例位于多面 map 下方。我见过一些示例,其中可以将表条目添加为单独的图,然后将两者合并。我不知道如何为我的案例制作它。

任何帮助都会很棒,谢谢。

最佳答案

正如@jlhoward 提到的,longlimitslatlimits没有定义。因此,我决定离开 coord_fixed(xlim = longlimits, ylim = latlimits)从这个答案的一部分。我的解决方法有效,但我相信有更好的方法来解决这个问题。挑战在于以一种可以很好地呈现数据的方式创建另一个图例。如果您使用 colourgeom_text ,您可以创建另一个图例,但您最终会看到字母表,即图例中灰色框中的 a。所以,我决定使用 geom_pointalpha = 0以及 colouraes .这样,您就有了一个带有 ID 名称的新图例,但您在 map 上看不到任何点。然后,我用了 annotate分配 map 上的数字。感谢@jlhoward,我创建了一个小数据框,这是annotate() 所必需的。 .如果使用原始数据框,R 会尝试写入文本 4000 次左右。在主题部分,我添加了legend.key = element_rect(fill = NA)为了删除图例中的灰色方块。我把图的高度和宽度做得很小,这样我就可以把它贴在这里。所以它看起来不是那么好。但是如果你指定大的数字,这个数字会更好看。

library(dplyr)
library(ggplot2)

wmap_byscen.df <- read.csv("mydata.csv", header = T)

mydf <- wmap_byscen.df[wmap_byscen.df$variable != c("AVG") &
wmap_byscen.df$ID_1 != c("0"),]

### This is for annotate()

mydf2 <- select(mydf, c.long, c.lat, ID_1, ID_name) %>%
distinct()

### Color setting

palette = brewer.pal(11,"RdYlGn")


ggplot(mydf, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = value)) +
facet_wrap(~ variable) +
geom_path(colour = "grey50", size = .1) +
geom_point(aes(x = c.long, y = c.lat, color=factor(ID_name, levels=unique(ID_name)), label = ID_1), size = 1, alpha = 0) +
annotate("text", x = mydf2$c.long, y = mydf2$c.lat, label = mydf2$ID_1) +
scale_fill_gradientn(name = "% Change",colours = palette) +
scale_color_discrete(name = "Regions") +
#coord_fixed(xlim = longlimits, ylim = latlimits) +
scale_y_continuous(breaks = seq(-60,90,30), labels = c("60ºS","30ºS","0º","30ºN","60ºN","90ºN")) +
scale_x_continuous(breaks = seq(-180,180,45), labels = c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE")) +
labs(x = "",y = "",title = "Average yield impacts across all crops across\nby climate scenarios (% change)") +
theme(plot.title = element_text(size = rel(2), hjust = 0.5, vjust = 1.5, face = "bold"),
legend.text = element_text(size = 8),
legend.position = "bottom",
legend.text = element_text(size = rel(1.3)),
legend.title = element_text(size = rel(1.4), hjust = 0.5, vjust = 1),
panel.background = element_rect(fill = "white", colour = "gray95"),
strip.text = element_text(size = 18),
axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16),
legend.key = element_rect(fill = NA)) +
guides(col = guide_legend(nrow = 3, byrow = TRUE))

enter image description here

关于r - 如何根据列条目向 ggplot2 中的 map 图添加额外的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496965/

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