同一图中的多个等值线图

时间:2015-12-08 08:50:35

标签: r ggplot2 contour

我是R的新手并尝试在同一个绘图中绘制多个2D等高线图。我尝试使用以下代码。

dat1 <- structure(list(x1 = c(1:10), y1 = c(10:20)), .Names = c("x1", "y1"), class = "data.frame", row.names = c(NA,-21L))
dat2 <- structure(list(x2 = c(10:20), y2 = c(20:30)), .Names = c("x2", "y2"), class = "data.frame", row.names = c(NA,-21L))
Contourcolors <- colorRampPalette(c('yellow',"red"))(20)
PointColors <- c(rep('green',5),rep('blue',5))
filled.contour(z=dat1,col=Contourcolors,
               plot.axes=points( x=seq(0, 1, length.out = nrow(dat1)),
                                 y=rep(.5,20), 
                                 col=PointColors,cex=5,pch=19)
)
Error in as.matrix.data.frame(x) : 
  length of 'dimnames' [2] not equal to array extent

如何使用R轻松完成此操作?

1 个答案:

答案 0 :(得分:1)

我不认为你的数据在这里有意义,但也许你正在寻找这样的东西?

dat1 <- data.frame(x = c(0:10), y = c(10:20))
dat2 <- data.frame(x = c(10:20), y = c(20:30))

dat3 <- cbind(rbind(dat1, dat2), df = rep(c('1', '2'), each = 11))

library(ggplot2)
ggplot(dat3, mapping = aes(x, y, col = df)) + 
  geom_density2d()

enter image description here