【问题标题】:R raster plot non-continuous color scaleR光栅图非连续色标
【发布时间】:2014-09-24 03:18:16
【问题描述】:

好的,所以我对此感到非常头疼,因此我们将不胜感激。我正在使用 raster 和 ggplot 绘制数据。下面是一个生成我创建的图形的示例:

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

我遇到的问题是它使用的连续色标。我到处寻找,但找不到任何特定于我的问题的东西(或者至少在我花了 6 个多小时的时间里找不到它)。我想要的色标是,比如说使用上面的例子,-3 和 -2.5 之间的数据值为红色,-2.5 和 -2.0 之间的数据值为橙色,-2.0 和 -1.5 之间的数据值为黄色,依此类推.我对使用 scale_colour_brewer 并没有死心,所以任何建议都会非常感激,让我摆脱痛苦。谢谢!

【问题讨论】:

    标签: r ggplot2 raster


    【解决方案1】:

    有一些错误,我不知道map是哪个包函数。所以我不确定这是否是您正在寻找的答案。

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

    您可以手动选择颜色,但您需要为它们指定适当的名称。

    【讨论】:

      猜你喜欢
      • 2015-07-06
      • 2015-03-30
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多