ggplot2:设置alpha值时,栅格绘图不能按预期工作 [英] ggplot2: raster plotting does not work as expected when setting alpha values

查看:164
本文介绍了ggplot2:设置alpha值时,栅格绘图不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先在这里发帖,希望我能观察网站的礼仪。我在网站上找不到和回答,之前我曾将其发布到ggplot2特定组,但目前还没有解决方案。

基本上我试图使用ggplot2覆盖两个栅格,并要求最上面的一个是半透明的。我有一个从高程数据栅格计算出来的HillShade栅格,并且我希望将高程栅格覆盖到山体阴影栅格上,所以得到的曲线看起来并不平坦。你可以在下面的可复制的R代码中看到我的意思。



使用基础图形我可以达到理想的结果,并且我在下面的代码中包含了一个例子来说明我的意思是,但我需要在ggplot2中做到这一点。



我无法让它在ggplot2中工作。结合光栅使颜色变得有趣(我可以自己绘制每个颜色)。任何人都可以帮助或指向正确的方向。自包含,可重现的代码示例如下。 (对不起,但我认为最好清楚)。

 #加载相关库
库(ggplot2 )
库(光栅)


#从我的Dropbox下载加纳样本栅格数据
oldwd< - getwd()
tmp< - tempdir()
setwd(tmp)
url1< - http://dl.dropbox.com/s/xp4xsrjn3vb5mn5/GHA_HS.asc
url2< - http:/ /dl.dropbox.com/s/gh7gzou9711n5q7/GHA_DEM.asc
f1< - file.path(tmp,GHA_HS.asc)
f2< - file.path(tmp, GHA_DEM.asc)
download.file(url1,f1)#File是〜5,655Kb
download.file(url2,f2)#File是〜2,645Kb


#从下载的文件创建栅格
hs< - 栅格(f1)
dem< - 栅格(f2)


# (hs,col = gray(1:100/100),legend = F)
plot(dem,col = rainbow(100),alpha = 0.4,add = T, legend = F)


#将栅格转换为数据框用于绘制ggplot
hdf < - rasterToPoints( HS); hdf < - data.frame(hdf)
colnames(hdf)< -c(X,Y,Hill)
ddf< - rasterToPoints(dem); (ddf)< - c(X,Y,DEM)


#创建向量对于颜色中断
b.hs < - seq(min(hdf $ Hill),max(hdf $ Hill),length.out = 100)
b.dem < - seq(min(ddf $ DEM),max(ddf $ DEM),length.out = 100)


#使用ggplot绘制DEM图层()
p1 < - ggplot()+
layer(geom =raster,data = ddf,mapping = aes(X,Y,fill = DEM))+
scale_fill_gradientn(name =Altitude,colors = rainbow(100) b.dem)+
scale_x_continuous(name = expression(paste(Longitude(,degree,))),limits = c(-4,2),expand = c(0,0))+
scale_y_continuous(name = expression(paste(Latitude(,degree,))),limits = c(4,12),expand = c(0,0))+
coord_equal )
print(p1)


#使用ggplot绘制hillShade图层()
p2 < - ggplot()+
图层(geom =栅格,data = hdf,mapping = aes(X,Y,fill = Hill))+
scale_fill_gradientn(colors = gray(1:100/100),breaks = b.hs,guide =none) +
scale_x_continuous(name = expression(pa ste(Longitude(,degree,))),limits = c(-4,2),expand = c(0,0))+
scale_y_continuous(name = expression( ,degree)))),limits = c(4,12),expand = c(0,0))+
coord_equal()
print(p2)


#尝试在DEM图层上将透明度和透明度都放在一起绘制出来
p3 < - ggplot(hdf)+
geom_raster(aes(X,Y,fill = Hill))+
scale_fill_gradientn(colors = gray(1:100/100),breaks = b.hs,guide =none)+
scale_x_continuous(name = expression(paste(Longitude(,degree,)) )),limits = c(-4,2),expand = c(0,0))+
scale_y_continuous(name = expression(paste(Latitude(,degree,))),limits = c(4,12),expand = c(0,0))+
geom_raster(data = ddf,aes(X,Y,fill = DEM),alpha = I(0.4))+
scale_fill_gradientn(name =Altitude,colors = rainbow(100),breaks = b.dem)+
coord_equal()
print(p3)


#清理下载的文件并返回到以前的wd
unlink(tmp,recursive = T)
setwd(oldwd)

我的问题tions如下:

Q1:在上面的例子中,如何使用基础图形绘制p3的图层看起来就像它们一样?



Q2:我怎样才能更明智地指定色阶,所以我在RHS上没有一个荒谬的传说?

解决方案Q1:你不能在不同的图层上使用不同的填充比例。一种解决方法是使用DEM的填充美学和山体阴影的alpha美学。不幸的是, geom_raster 似乎没有像我期望的那样使用alpha审美。你可以用 geom_tile 得到同样的效果,它只需要更长时间:

  ggplot(hdf)+ 
geom_raster(data = ddf,aes(X,Y,fill = DEM))+
scale_fill_gradientn(name =Altitude,colors = rainbow(100),breaks = b .dem)+
geom_tile(aes(X,Y,alpha = Hill),fill =grey20)+
scale_alpha(range = c(0,0.5))+
scale_x_continuous name = expression(paste(Longitude(,degree,))),
limits = c(-4,2),expand = c(0,0))+
scale_y_continuous = paste(Latitude(,degree,))),
limits = c(4,12),expand = c(0,0))+
coord_equal()

Q2:签出?guide_colorbar 。它不能很好地处理你的100个颜色中断,但少用它很好。

  ggplot(hdf)+ 
geom_raster(data = ddf,aes(X,Y,fill = DEM))+
scale_fill_gradientn(name =Altitude,colors = rainbow(20))+
guides(fill = guide_colorbar ())+
geom_tile(aes(X,Y,alpha = Hill),fill =grey20)+
scale_alpha(range = c(0,0.5))+
scale_x_continuous name = expression(paste(Longitude(,degree,))),
limits = c(-4,2),expand = c(0,0))+
scale_y_continuous = paste(Latitude(,degree,))),
limits = c(4,12),expand = c(0,0))+
coord_equal()


First post here, I hope I'm observing website etiquette. I couldn't find and answer on the site and I previously posted this to a ggplot2 specific group, but no solutions as yet.

Basically I am trying to overlay two rasters using ggplot2 and require the top one to be semi-transparent. I have a hillShade raster which is computed from an elevation data raster, and I wish to overlay the elevation raster onto the hillshade raster so the resulting plot doesn't look 'flat'. You can see what I mean in the reproducible R code below.

Using base graphics I can achieve the desired result and I have included an example in code below to make it clear what I mean, but I need to do this in ggplot2.

I can't get it to work in ggplot2. Combining the rasters makes the colours go funny (I can plot each one ok by itself). Can anyone help or point me in the right direction. Self contained, reproducible code example included below. (Sorry for the length, but I thought better to be clear).

#   Load relevant libraries
library(ggplot2)
library(raster)


#   Download sample raster data of Ghana from my Dropbox
oldwd <- getwd()
tmp <- tempdir()
setwd(tmp)
url1 <- "http://dl.dropbox.com/s/xp4xsrjn3vb5mn5/GHA_HS.asc"
url2 <- "http://dl.dropbox.com/s/gh7gzou9711n5q7/GHA_DEM.asc"
f1 <- file.path(tmp,"GHA_HS.asc")
f2 <- file.path(tmp,"GHA_DEM.asc")
download.file(url1,f1)  #File is ~ 5,655Kb
download.file(url2,f2)  #File is ~ 2,645Kb


#   Create rasters from downloaded files
hs <-  raster(f1)
dem <- raster(f2)


#   Plot with base graphics to show desired output
plot(hs,col=grey(1:100/100),legend=F)
plot(dem,col=rainbow(100),alpha=0.4,add=T,legend=F)


#   Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(hs); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")
ddf <- rasterToPoints(dem); ddf <- data.frame(ddf)
colnames(ddf) <- c("X","Y","DEM")


#   Create vectors for colour breaks
b.hs <- seq(min(hdf$Hill),max(hdf$Hill),length.out=100)
b.dem <- seq(min(ddf$DEM),max(ddf$DEM),length.out=100)


#   Plot DEM layer with ggplot()
p1 <- ggplot()+
    layer(geom="raster",data=ddf,mapping=aes(X,Y,fill=DEM))+
    scale_fill_gradientn(name="Altitude",colours = rainbow(100),breaks=b.dem)+
    scale_x_continuous(name=expression(paste("Longitude (",degree,")")),limits=c(-4,2),expand=c(0,0))+
    scale_y_continuous(name=expression(paste("Latitude (",degree,")")),limits=c(4,12),expand=c(0,0))+
    coord_equal()
print(p1)


#   Plot hillShade layer with ggplot()
p2 <- ggplot()+
    layer(geom="raster",data=hdf,mapping=aes(X,Y,fill=Hill))+
    scale_fill_gradientn(colours=grey(1:100/100),breaks=b.hs,guide="none")+
    scale_x_continuous(name=expression(paste("Longitude (",degree,")")),limits=c(-4,2),expand=c(0,0))+
    scale_y_continuous(name=expression(paste("Latitude (",degree,")")),limits=c(4,12),expand=c(0,0))+
    coord_equal()
print(p2)


#   Try to plot both together with transparency on the DEM layer
p3 <- ggplot(hdf)+
    geom_raster(aes(X,Y,fill=Hill))+
    scale_fill_gradientn(colours=grey(1:100/100),breaks=b.hs,guide="none")+
    scale_x_continuous(name=expression(paste("Longitude (",degree,")")),limits=c(-4,2),expand=c(0,0))+
    scale_y_continuous(name=expression(paste("Latitude (",degree,")")),limits=c(4,12),expand=c(0,0))+
    geom_raster(data=ddf,aes(X,Y,fill=DEM),alpha=I(0.4))+
    scale_fill_gradientn(name="Altitude",colours = rainbow(100),breaks=b.dem)+
    coord_equal()
 print(p3)


#   Cleanup downloaded files and return to previous wd
unlink(tmp,recursive=T)
setwd(oldwd)

My questions are as follows:

Q1: How can I make the layers of p3 look like they do when plotted with base graphics in the example above?

Q2: How can I more sensibly specify colour scales so I don't have a ridiculous legend on the RHS?

解决方案

Q1: You can't have different fill scales on different layers. One workaround is to use the fill aesthetic for the DEM and the alpha aesthetic for hillshade. Unfortunately, geom_raster doesn't seem to use the alpha aesthetic the way I expected. You can get the same effect with geom_tile, it just takes longer:

ggplot(hdf) +
  geom_raster(data=ddf,aes(X,Y,fill=DEM)) +
  scale_fill_gradientn(name="Altitude",colours = rainbow(100),breaks=b.dem) +
  geom_tile(aes(X,Y,alpha=Hill), fill = "grey20") +
  scale_alpha(range = c(0, 0.5)) +
  scale_x_continuous(name=expression(paste("Longitude (",degree,")")),
    limits=c(-4,2),expand=c(0,0)) +
  scale_y_continuous(name=expression(paste("Latitude (",degree,")")),
    limits=c(4,12),expand=c(0,0)) +
  coord_equal() 

Q2: Check out ?guide_colorbar. It doesn't work very nicely with your 100 colour breaks, but with fewer it's pretty good.

ggplot(hdf)+
  geom_raster(data=ddf,aes(X,Y,fill=DEM))+
  scale_fill_gradientn(name="Altitude",colours = rainbow(20))+
  guides(fill = guide_colorbar()) +
  geom_tile(aes(X,Y,alpha=Hill), fill = "grey20") +
  scale_alpha(range = c(0, 0.5)) +
  scale_x_continuous(name=expression(paste("Longitude (",degree,")")),
    limits=c(-4,2),expand=c(0,0)) +
  scale_y_continuous(name=expression(paste("Latitude (",degree,")")),
    limits=c(4,12),expand=c(0,0)) +
  coord_equal() 

这篇关于ggplot2:设置alpha值时,栅格绘图不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆