【问题标题】:Smooth 2D surface光滑的二维表面
【发布时间】:2016-10-08 17:28:24
【问题描述】:

我想知道是否可以平滑绘图或使其更好,因为现在像素太大了。

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

【问题讨论】:

  • 没有数据很难重现。在这种情况下,一些虚假数据也会非常简单。
  • 显而易见的方法是插入数据。如果你提供一些虚假数据,我想有人会解决这个问题。
  • 我添加了一个假数据

标签: r ggplot2 smooth


【解决方案1】:
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))

平滑此图的最简单方法是将geom_raster()interpolate=TRUE 结合使用(有关其他优点,请参阅?geom_tile)。

gg0 + geom_raster(interpolate=TRUE)

您也可以使用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()    

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多