简体   繁体   English

同一图中有多个等高线图

[英]Multiple contour plots in the same plot

I am new to R and trying to draw multiple 2D contour plots in the same plot. 我是R的新手,正在尝试在同一图中绘制多个2D等高线图。 I tried with the following code. 我尝试了以下代码。

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

How can I do this easily with R? 如何使用R轻松做到这一点?

I don't think your data is making much sense here, but perhaps you are looking for something like this? 我认为您的数据在这里没有多大意义,但也许您正在寻找类似的东西?

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM