我正在尝试创建一个虚拟数据示例,以便我可以更好地向论坛提问。基本上我试图在一页纸上创建一个带有6个图形的PDF。下面是我一直在尝试构建的示例代码。
#Dummy data
Data1 <- data.frame(flow = c(8,8.5,6,7.1,9), SP_elev = c(20,11,5,25,50))
Data2 <- data.frame(flow = c(7,7.2,6.5,8.2,8.5), SP_elev = c(13,15,18,25,19))
Data3 <- data.frame(flow = c(2,3,5,7,9), SP_elev = c(20,25,28,30,35))
Data4 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data5 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data6 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(22,23,25,27,29))
#Create Vector list (in place of list.files)
dataframes = list("Data1" = data1,
"Data2" = data2,
"Data3" = data3,
"Data4" = data4,
"Data5" = data5,
"Data6" = data6)
# open the PDF device
pdf(file="Dummy_Example.pdf", paper="letter", height=10, width=8)
#Create array of plots
par(mfrow=c(3,2))
#plot a with regression model
for (i in dataframes) {
plot (i[,c('flow', 'SP_elev')], xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")), ylab= "Elevation (m)", tck=0.02, adj = 0.5)
}
# close the PDF device
dev.off()
我一直收到错误
Error in i[, c("flow", "SP_elev")] : incorrect number of dimensions
答案 0 :(得分:3)
您应该将data.frames放在列表中,
dataframes = list("Data1" = Data1,
"Data2" = Data2,
"Data3" = Data3) # I gave up here
其余代码应该有效。