这是一个例子:
生成坐标:
b = c(0, 5, 10, 15, 20)
a = (1:30)/10
生成所有坐标组合
df <- expand.grid(a, b)
通过 a 和 b+1 的 tcrossprod 生成 c (这完全是任意的,但会生成一个很好的模式)
df$c <- as.vector(a %o% (b+1))
ggplot(df, aes(x = Var1, y = Var2, z = c, fill = c)) +
geom_raster(interpolate = T) + #interpolate for success
scale_fill_gradientn(colours = rainbow(10))
一般来说,如果您有一个要在 ggplot 中绘制的值矩阵(z 值),您需要通过以下方式将其转换为长格式melt
in reshape2
或聚集在tidyr
然后用于绘图。
您的数据非常稀疏,解决此问题的一种方法是生成丢失的数据。我将展示如何使用 loess 函数来完成:
model <- loess(c ~ a + b, data = DF) #make a loess model based on the data provided (data in OP)
z <- predict(model, newdata = expand.grid(a = (10:30)/10, b = (0:200)/10)) #predict on the grid data
df <- data.frame(expand.grid(a = (10:30)/10, b = (0:200)/10), c = as.vector(z)) #append z to grid data
ggplot(df, aes(x = a, y = b, z = c, fill = c)) +
geom_raster(interpolate = T)+
scale_fill_gradientn(colours = rainbow(10))