光滑的2D表面

Ale*_*sky 10 r smooth ggplot2

我想知道是否有可能平滑情节或使其更好,因为现在像素太大了.

  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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ben*_*ker 9

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

平滑该地块的最简单方法是使用geom_raster()interpolate=TRUE(见?geom_tile其他好处).

gg0 + geom_raster(interpolate=TRUE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你也可以手工做(双线性)插值,使用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)
Run Code Online (Sandbox Code Playgroud)

现在融化并重新绘制:

plot2d_2 <- melt(m2,value.name="z")
gg0 %+% plot2d_2 + geom_tile()    
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

嗯,插值似乎改变了z尺度 - 你应该小心......