平滑的2D表面 [英] Smooth 2D surface

查看:161
本文介绍了平滑的2D表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(ggplot2)
library(reshape2)

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

名称(plot2d)< -c(x,y,z)

v< - ggplot(plot2d,aes(x,y,z )=
v + geom_tile(aes(fill = z))+
scale_alpha_continuous(limits = c(start.point,end.point))+
scale_fill_gradient2('TYYYT',low =绿色,中等=白色,高=红色)

解决方案

  library(ggplot2)
library (reshape2)

set.seed(101)
##设置dimnames,以便melt()选择它们
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))



您也可以手动执行(双线性)插值,使用字段包(有很多选项:例如库(SOS);
$ b

  library(fields)
m2({bilinear interpolation})

< --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尺度 - 你应该小心......


I am wondering about the possibility to smooth the plot or make it somehow better, since now the pixels are too big.

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

解决方案

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

The easiest way to smooth this plot is to use geom_raster() with interpolate=TRUE (see ?geom_tile for other advantages).

gg0 + geom_raster(interpolate=TRUE)

You can also do (bilinear) interpolation by hand, using the fields package (there are lots of options: e.g. 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)

Now melt it and replot:

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

Hmm, the interpolation seems to have changed the z-scale - you should be careful with that ...

这篇关于平滑的2D表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆