2012-06-24 67 views
15

第一篇文章在這裏,我希望我觀察網站禮儀預期光柵繪圖不起作用。我在網站上找不到和回答,之前我曾將其發佈到ggplot2特定組,但目前還沒有解決方案。GGPLOT2:當設置alpha值

基本上我試圖覆蓋使用GGPLOT2兩個柵格和要求最上面的一個是半透明的。我有一個從高程數據柵格計算出來的HillShade柵格,並且我希望將高程柵格覆蓋到山體陰影柵格上,因此生成的繪圖看起來不是「平坦的」。您可以在下面的可重複R代碼中查看我的意思。

使用基本圖形我能達到預期的結果,並且我已經包含在下面的代碼示例要清楚我的意思,但我需要做的這GGPLOT2。

我不能讓它在GGPLOT2工作。結合光柵使顏色變得有趣(我可以自己繪製每個顏色)。任何人都可以幫助或指向正確的方向。自包含,可重現的代碼示例如下。 (對不起,但我認爲最好清楚)。

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

我的問題如下:

Q1:我怎樣才能使P3的外觀的層像他們當在上面的例子基礎圖形繪製做?

Q2:我怎樣才能更理智指定的色階,所以我沒有在RHS一個荒謬的傳說?

+0

我敢肯定會有某種方式使用'annotation_raster' - 但我的努力都石沉大海迄今。 (因爲我看到它)你的組合情節失敗的原因是兩個調用'scale_fill_gradientn' – mnel

+0

這就是它的關鍵。看起來'ggplot'只能有一個'fill'審美和一個'colour'審美。你可以通過使用color = DEM將DEM圖層設爲'geom_point',然後使用'scale_colour_gradientn'而不是'scale_fill_gradientn'來獲得某種工作,但它看起來不如基本圖形方式好。 –

+0

謝謝你看我的問題,你們倆! @mplourde - 我認爲ggplot2每層可以有一個填充美學,但我認爲在alpha透明的地方,它將使用addidtive顏色混合的顏色組合在一起,其中層重疊。不知何故,這種混合在ggplot2中與基礎圖形不同。我一直在想它,我認爲我需要做的是手動計算hillShade柵格中相關灰度值的顏色(我認爲這是hcl顏色中的色度模型)和DEM柵格的色調(我認爲這將是色調)。 –

回答

11

Q1:你不能在不同的層不同的填充尺度。一種解決方法是使用DEM的填充美學和山體陰影的alpha美學。不幸的是,geom_raster似乎並沒有像我期望的那樣使用alpha審美。你可以得到相同的效果與geom_tile,它只是需要更長的時間:

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:退房?guide_colorbar。它不能很好地處理你的100個色彩分離,但少了很多。

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

DEM plus hill shading and colorbar legend

+0

非常感謝您的時間和精力來解決我的編碼問題。這對我的目的很有用。非常感謝! :-) –

3

光柵阿爾法將在下一版本的支持,這樣你就可以得出:

ggplot(NULL, aes(X, Y)) + 
    geom_raster(data = ddf, aes(fill = DEM)) + 
    geom_raster(data = hdf, aes(alpha = Hill)) + 
    scale_fill_gradientn(name="Altitude",colours = rainbow(20))+ 
    guides(fill = guide_colorbar()) + 
    scale_alpha(range = c(0, 0.5), 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() 

反正很漂亮的情節。

如果您想立即使用,請嘗試從github上安裝:

library(devtools) 
install_github("ggplot2", "kohske", "fix/geom-raster-alpha") 

注意geom_tilegeom_raster看在一些設備上的不同。 也許柵格更適合您的目的。

enter image description here

+0

謝謝!有了這個修復,我現在可以完全按照我的需要進行繪製。非常感謝。乾杯,西蒙。 –

+0

任何人都知道如何調整圖例中的顏色以匹配地圖中的顏色,現在它被灰色蒙上了一層? – Dominik

+0

當使用geom_raster()而不是geom_tile()時,出現文件輸出(PDF)與「模糊」柵格的問題。後者解決了這個問題。 Thx的評論。 – Shadow