问题描述
大家好! 我真的需要一些帮助来在连续地图上创建离散图例。
但是,我想要的是一个分类图例,该图例具有根据图例中每个类别的连续值,目的是清楚地表明算法预测之间的一致性,而没有连续标度。
图2中您可以看到完美的地图,但是我想使用ggplot2制作它:Here I can clearly indicate in the discrete legend the colors accordingly to the 4 statistical algos.
对于第二张地图,我使用了以下代码:
breakpoints <- c(-125,125,375,625,875,1125)
colors <- c(grey(seq(0.9,0.7,-0.2)),gcolors(3))
a.arg <- list(at=seq(0,1000,length.out=5),labels=c("0","1","2","3","4"),cex.axis=1.5)
l.arg <- list(text="Vote",side=2,line=0.5,cex=1.5)
par(mar=c(0,r.mar),cex=1.4)
plot(mymap,col=colors,ext=study_area,breaks=breakpoints,legend.width=1.5,legend.shrink=0.6,legend.mar=7,axis.args=a.arg,legend.arg=l.arg,axes=FALSE,box=FALSE,zlim=c(0,1000))
而且效果很好!
但是,在ggplot2下,我正在使用这段代码:
#Color scales pres
col_scale_var_test_pres <- scale_fill_gradientn(
colours = c(grey(c(0.90,seq(0.9,-0.2))),gcolors(3)),# colour palete
na.value="transparent",values=rescale(seq(0,1000),limits=c(0,breaks= seq(0,by=250),# if one algo predict species occurrence it counts 250,two algos
#(500),### three (750),four (1000)
labels= c(0,4)
)
b1 <- plot_anomaly_2(r= species_map,label="(b)",title="") + col_scale_var_test_pres
我已经尝试使用#scale_fill_manual,#scale_fill_gradientn等,并且也忽略了这个问题(非常相似): R ggplot2 discrete colour palette for gradient map。 但是我仍然无法解决这个问题。
任何帮助和建议将不胜感激!
解决方法
可复制的代码下次可能会更有用。假设您想从data
数据框中绘制mymap
,那么以下代码可能会有所帮助:
library(tidyverse)
library(ggthemes)
mymap %>%
mutate(value = cut(data,breaks = c(-125,125,375,625,875,1125),right = FALSE,labels = c("0","1","2","3","4","5"))) %>%
ggplot(.,aes(x = long,y = lat,group = group,fill = value)) +
geom_polygon(col = "grey") + scale_fill_brewer(palette = "YlGnBu",direction = 1) +
coord_fixed(xlim = c(-130,1200)) + theme_map() +
theme(legend.position = "bottom")