簡體   English   中英

子集數據幀並使用循環繪制所有子集[R]

[英]subset dataframe and plot all the subsets with a loop [R]

我正在使用具有8個有用變量的數據框,其代碼思想是繪制4個變量(y軸上為3個,x軸上為公共)。 數據框如下所示:

它有6500行

我想從文件列中子集化data.frame,然后將LogP繪制為a軸,並將Temperature,RH和臭氧繪制為y軸。 我嘗試在plot函數中使用子集,但操作不順利。 我將此代碼用於原始文件之一的繪圖,但不知道如何包括子集

> plot(DataOzono$LogP, DataOzono$Temperature, axes= F,type="l",col="red", ylab = NULL, xlab = 'LogP',xaxt="n",yaxt="n" )
axis(2,ylim(c(min(DataOzono$Temperature),max(DataOzono$Temperature)), layout.widths(2)))
mtext(text = 'T',line = 2,side = 2)
par(new=TRUE)
plot(DataOzono$LogP, DataOzono$RH,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("RH",side=4,line=2)
par(new=TRUE)
plot(DataOzono$LogP, DataOzono$Ozone,type="l",col="green",xaxt="n",yaxt="n",xlab="",ylab="")
mtext("O3",side=5,line=3)
axis(2, line = 4)

任何建議都會非常有幫助。

這是loop繪制圖表的方法。 在您提供的示例中,我們只有一個文件號。 但是,它應該為文件列中的每個數字創建一個圖表。 在Windows上,可以使用savePlot保存到驅動器。 我簡化了您的示例,因為我遇到了錯誤。

DataOzono <- read.table(text="pressure    height  Temperature RH  Ozone   file    LogP
753.6   2541    16.8    76  0   80131   0.3475673
748.0   2604    17.7    32  0   80131   0.347959
743.5   2656    15.9    38  0   80131   0.3482766
739.8   2697    15.4    39  0   80131   0.3485396
736.6   2734    15.0    41  0   80131   0.3487685
731.8   2790    14.5    42  0   80131   0.3491142", header=TRUE, stringsAsFactors=FALSE)

original_par <- par()
par(mar=c(5.1, 8.1, 4.1, 3.1))

for (i in unique(DataOzono$file)){
DataOzono_subset <- DataOzono[DataOzono$file==i,] #keep only rows for that file number

plot(DataOzono_subset$LogP, DataOzono_subset$Temperature, axes= F,type="l",col="red", ylab = "", xlab = 'LogP',xaxt="n",yaxt="n" )
axis(2,col="red",col.axis="red")
mtext(text = 'T',line = 2,side = 2,col="red",col.lab="red")
par(new=TRUE)
plot(DataOzono_subset$LogP, DataOzono_subset$RH,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4,col="blue",col.axis="blue")
mtext("RH",side=4,line=2,col="blue",col.lab="blue" )
par(new=TRUE)
plot(DataOzono_subset$LogP, DataOzono_subset$Ozone,type="l",col="darkgreen",xaxt="n",yaxt="n",xlab="",ylab="")
mtext("O3",side=2,line=6,,col="darkgreen",col.lab="darkgreen")
axis(2, line = 4,col="darkgreen",col.axis="darkgreen")

savePlot(filename=paste0("c:/temp/",i,".png"),type="png")
}

par()  <- original_par #restore par to initial value.

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM