2016-03-08 86 views
3

我有點卡住柵格與對數刻度。考慮此情節,例如:geom_raster插值與對數刻度

ggplot(faithfuld, aes(waiting, eruptions)) + 
geom_raster(aes(fill = density)) 

enter image description here

但如何使用數比例與此GEOM?通常的方法都不是非常令人滿意:

ggplot(faithfuld, aes(waiting, log10(eruptions))) + 
    geom_raster(aes(fill = density)) 

enter image description here

ggplot(faithfuld, aes(waiting, (eruptions))) + 
    geom_raster(aes(fill = density)) + 
    scale_y_log10() 

enter image description here

而這並不在所有的工作:

ggplot(faithfuld, aes(waiting, (eruptions))) + 
    geom_raster(aes(fill = density)) + 
    coord_trans(x="log10") 

Error: geom_raster only works with Cartesian coordinates

是否有任何選項可用於使用光柵日誌比例?

準確地說,我有三列數據。 z值是我想用來爲柵格着色的值,它不是從x和y值中計算出來的。所以我需要提供所有三列到ggplot函數。例如:

dat <- data.frame(x = rep(1:10, 10), 
        y = unlist(lapply(1:10, function(i) rep(i, 10))), 
        z = faithfuld$density[1:100]) 

ggplot(dat, aes(x = log(x), y = y, fill = z)) + 
    geom_raster() 

enter image description here

我能做些什麼來擺脫柵格這些差距呢?

注意,這個問題是關係到這兩個:

我一直保持的R代碼裏面的更新要點,結合細節從這些問題的答案(包含在要點中的示例輸出)。該要點在於:https://gist.github.com/benmarwick/9a54cbd325149a8ff405

回答

3

數據集faithfuld已經有一個密度列,它是等待和爆發的二維密度的估計值。你可以發現數據集中的爆發和等待是網格中的點。當您使用geom_raster時,它不會爲您計算密度。相反,它根據x,y座標繪製密度,在這種情況下,是網格。因此,如果你只是在y上應用對數變換,它會扭曲y之間的差異(原來它們是等距的),這就是爲什麼你看到你的陰謀的空間。我點用可視化效果:

library(ggplot2) 
library(gridExtra) 

# Use point to visualize the effect of log on the dataset 
g1 <- ggplot(faithfuld, aes(x=waiting, y=eruptions)) + 
    geom_point(size=0.5)  

g2 <- ggplot(faithfuld, aes(x=waiting, y=log(eruptions))) + 
    geom_point(size=0.5)  

grid.arrange(g1, g2, ncol=2)  

enter image description here

如果你確實要改變y以對數刻度併產生密度圖,您必須使用faithful數據集geom_density_2d

# Use geom_density_2d 
ggplot(faithful, aes(x=waiting, y=log(eruptions))) + 
    geom_density_2d() + 
    stat_density_2d(geom="raster", aes(fill=..density..), 
        contour=FALSE) 

enter image description here

更新:使用geom_rect和供應定製XMIN,XMAX,YMIN,YMAX值以滿足日誌規模的空間。

由於geom_raster使用相同大小的圖塊,因此您可能必須使用geom_tilegeom_rect來創建圖。我的想法是計算每個圖塊應該有多大(寬度),並調整每個圖塊的xminxmax以填補空白。

dat <- data.frame(x = rep(1:10, 10), 
        y = unlist(lapply(1:10, function(i) rep(i, 10))), 
        z = faithfuld$density[1:100]) 
library(ggplot2) 
library(gridExtra) 

g <- ggplot(dat, aes(x = log(x), y = y, fill = z)) + 
    geom_raster() 

# Replace the ymin and ymax 
distance <- diff((unique(dat$x)))/2 
upper <- (unique(dat$x)) + c(distance, distance[length(distance)]) 
lower <- (unique(dat$x)) - c(distance[1], distance) 

# Create xmin, xmax, ymin, ymax 
dat$xmin <- dat$x - 0.5 # default of geom_raster is 0.5 
dat$xmax <- dat$x + 0.5 
dat$ymin <- unlist(lapply(lower, function(i) rep(i, rle(dat$y)$lengths[1]))) 
dat$ymax <- unlist(lapply(upper, function(i) rep(i, rle(dat$y)$lengths[1])))   

# You can also use geom_tile with the width argument 
g2 <- ggplot(dat, aes(x=log(x), y=y, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=z)) + 
    geom_rect() 

# show the plots  
grid.arrange(g, g2, ncol=2) 

enter image description here

+0

感謝,在我的實際使用情況我真的需要使用第三個變量的密度,如'faithfuld'數據集,所以你的建議並沒有解決我的問題。你還有其他建議嗎? – Ben

+0

我不確定你的意思是「真的需要使用第三個變量作爲密度」。你有沒有辦法計算密度?另一種方法是檢索'ggplot'用來構建圖的數據。您可以將圖像保存爲'g'這樣的對象,並通過'ggplot_build(g)$ data'檢索數據。數據包含密度。 – JasonWang

+0

或者你可以編輯問題並提供實際使用案例的子集? – JasonWang