2016-06-08 49 views
9

我想知道平滑情節的可能性,或者讓它變得更好,因爲現在像素太大了。平滑的2D表面

library(ggplot2) 
    library(reshape2) 

    # plot2d = melt(c) 
    plot2d = melt(matrix(rnorm(20), 5)) # fake data 

    names(plot2d) <- c("x", "y", "z") 

    v <- ggplot(plot2d, aes(x, y, z = z)) 
      v + geom_tile(aes(fill = z)) + 
       scale_alpha_continuous(limits=c(start.point, end.point)) + 
       scale_fill_gradient2('TYYYT',low="green", mid = "white", high="red") 

enter image description here

+0

幾乎沒有可重複的數據。在這種情況下,一些假數據也會非常簡單。 –

+0

顯而易見的方法是內插數據。如果您提供了一些假數據,我想有人會解決這個問題。 –

+0

我添加了一個假數據 –

回答

7
library(ggplot2) 
library(reshape2) 

set.seed(101) 
## set dimnames so that melt() picks them up 
m <- matrix(rnorm(20),5,dimnames=list(x=1:5,y=1:4)) 

plot2d_1 <- melt(m,value.name="z") 

gg0 <- ggplot(plot2d_1, aes(x,y,z=z,fill=z)) 

enter image description here

來平滑該曲線的最簡單方法是使用geom_raster()interpolate=TRUE(見?geom_tile其他好處)。

gg0 + geom_raster(interpolate=TRUE) 

enter image description here

你也可以做(雙線)插手工,使用fields包(有很多的選擇:如library(sos); findFn("{bilinear interpolation}")

library(fields) 
m2 <- interp.surface.grid(list(x=1:5,y=1:4,z=m), 
       grid.list=list(x=seq(1,5,length=101), 
          y=seq(1,4,length=101))) 
dimnames(m2$z) <- list(x=m2$x,y=m2$y) 

現在熔化和重繪:

plot2d_2 <- melt(m2,value.name="z") 
gg0 %+% plot2d_2 + geom_tile()  

enter image description here

嗯,插值似乎已經改變了Z-規模 - 你應該小心與...