我正在尝试将3D绘制为具有离散数据的轮廓曲线或平铺。我设法使用akima
包中的使用插值来完成它。但是,我还希望能够使用原始数据生成一个图。最初我认为它就像跳过插值阶段一样简单,但我认为不规则间隔数据可能存在一些问题。
以下是geom_raster
的尝试,您可以看到我拥有的数据类型(我需要转换轴数据):
library(ggplot2)
library(akima)
x <- seq(from = 0.01,to = 1, by = 0.01)
y <- seq(from = 0.01,to = 1, by = 0.01)
xAxis <- x/(1+x*y)
yAxis <- x/(1+x*y)
df <- expand.grid(x,y)
xAxis <- df$Var1/(1+df$Var1*df$Var2)
yAxis <- df$Var2/(1+df$Var1*df$Var2)
z <- -1*((0.5-xAxis)^2 + (0.5-yAxis)^2)
df$x <- xAxis
df$y <- yAxis
df$z <- z
ggplot(df, aes(x, y, fill=z)) +
geom_raster() +
viridis::scale_fill_viridis()
错误消息:
Error in matrix(NA_character_, nrow = nrow, ncol = ncol) :
invalid 'nrow' value (too large or NA)
In addition: Warning messages:
1: In f(...) : NAs introduced by coercion
2: In f(...) : NAs introduced by coercion
我也试过geom_tile()
,它没有产生任何错误信息,但给了我一个空图:
library(ggplot2)
library(akima)
x <- seq(from = 0.01,to = 1, by = 0.01)
y <- seq(from = 0.01,to = 1, by = 0.01)
xAxis <- x/(1+x*y)
yAxis <- x/(1+x*y)
df <- expand.grid(x,y)
xAxis <- df$Var1/(1+df$Var1*df$Var2)
yAxis <- df$Var2/(1+df$Var1*df$Var2)
z <- -1*((0.5-xAxis)^2 + (0.5-yAxis)^2)
df$x <- xAxis
df$y <- yAxis
df$z <- z
ggplot(df, aes(x, y, fill=z)) +
geom_tile() +
viridis::scale_fill_viridis()
最后,这是使用akima
'包的插值的工作示例。
library(ggplot2)
library(akima)
x <- seq(from = 0.01,to = 1, by = 0.01)
y <- seq(from = 0.01,to = 1, by = 0.01)
xAxis <- x/(1+x*y)
yAxis <- x/(1+x*y)
df <- expand.grid(x,y)
xAxis <- df$Var1/(1+df$Var1*df$Var2)
yAxis <- df$Var2/(1+df$Var1*df$Var2)
z <- -1*((0.5-xAxis)^2 + (0.5-yAxis)^2)
df$x <- xAxis
df$y <- yAxis
df$z <- z
im <- with(df, akima::interp(x, y, z, nx = 100, ny = 100))
df2 <- data.frame(expand.grid(x = im$x, y = im$y), z = c(im$z))
ggplot(df2, aes(x, y, fill=z)) +
geom_raster() +
viridis::scale_fill_viridis()
akima
的主要问题是,对于我的原始数据,它似乎会抛出一些点,奇怪的是这里的伪造数据不会出现。
谢谢!