简体   繁体   English

光栅图非连续色标

[英]R raster plot non-continuous color scale

Ok, so I'm having an epic brain cramp over this, so any help would be appreciated. 好吧,所以我对此有史诗般的抽筋,因此,我们将不胜感激。 I am plotting data using raster and ggplot. 我正在使用栅格和ggplot绘制数据。 Below is an example to produce the figure that I create: 以下是产生我创建的图形的示例:

r = raster(ncol=18, nrow=18, xmn=-120, xmx=-80, ymn=10, ymx=45)
outlines = as.data.frame(map("world", xlim=c(-120, -80), ylim=c(10, 45), plot=FALSE)     [c("x","y")])
map = geom_path(aes(x,y), inherit.aes = FALSE, data = outlines, alpha = 0.8, show_guide = FALSE, color = "gray")

lat = runif(10, 10,45)
lon = runif(10, -120,-80)
data = runif(10, -3,2)

xy = cbind(x = lon, y = lat)

dummy = rasterize(xy, r, field=data, fun=mean)
dummy.df = as.data.frame(dummy, row.names=NULL, optional = TRUE, xy = TRUE, centroids = TRUE)
colnames(dummy.df) <- c("lon", "lat", "data")


ggplot(data = dummy.df, aes(x=lon,y=lat)) + geom_tile(aes(fill = data)) + scale_colour_brewer(palette="Set1")

What I'm having an issue with is the continuous color scale that it uses. 我遇到的问题是它使用的连续色标。 I have looked everywhere, but can't find anything specific to my issue (or at least couldn't find it in the 6+ hours I spent looking). 我到处都看过,但是找不到与我的问题有关的任何东西(或者至少在我花了六个多小时找不到的东西)。 What I want the color scale to be is, say using the example above, have values of data between -3 and -2.5 be red, between -2.5 and -2.0 to be orange, -2.0 and -1.5 to be yellow and so on. 我想要的色阶是,例如使用上述示例,数据值介于-3和-2.5之间为红色,介于-2.5和-2.0之间为橙色,-2.0和-1.5之间为黄色,依此类推。 。 I am not dead set on using scale_colour_brewer, so any suggestions would be so much appreciated and put me out of my misery. 我对使用scale_colour_brewer并没有犹豫,因此任何建议将不胜感激,这使我摆脱了痛苦。 Thanks! 谢谢!

There were some errors, I didn't know from which package function map was. 有一些错误,我不知道来自哪个包函数map So I am not shure if this the answer you are looking for. 因此,对于这是否是您要找的答案,我不确定。

library(ggplot2)
library(raster)
library(RColorBrewer)
r = raster(ncol=18, nrow=18, xmn=-120, xmx=-80, ymn=10, ymx=45)
outlines = as.data.frame(map("world", xlim=c(-120, -80), ylim=c(10, 45), plot=FALSE)     [c("x","y")])
map = geom_path(aes(x,y), inherit.aes = FALSE, data = outlines, alpha = 0.8, show_guide = FALSE, color = "gray")

lat = runif(10, 10,45)
lon = runif(10, -120,-80)
data = runif(10, -3,2)

xy = cbind(x = lon, y = lat)

dummy = rasterize(xy, r, field=data, fun=mean)
dummy.df = as.data.frame(dummy, row.names=NULL, optional = TRUE, xy = TRUE, centroids = TRUE)
colnames(dummy.df) <- c("lon", "lat", "data")

breaks2 <-seq(-2.5,2,0.5)
dummy.df$Col<-as.character(cut(dummy.df$data,breaks = breaks2))
dummy.df$Col[is.na(dummy.df$Col)] <- "Empty"
#there not enouth colours in this palette so I added green
Colors <- c(brewer.pal(9, "Set1"),"green")
names(Colors) <- unique(dummy.df$Col)
Colors["Empty"]="grey40"
ggplot(data = dummy.df, aes(x=lon,y=lat)) + geom_tile(aes(fill = Col))+
  scale_fill_manual(values=c(Colors))

您可以手动选择颜色

You can choose colors manually, but you need to give them appropriate names. 您可以手动选择颜色,但是需要给它们指定适当的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM