【问题标题】:Multiple log-normal probability plots in 1 plot using R使用 R 的 1 个图中的多个对数正态概率图
【发布时间】:2016-08-10 00:40:42
【问题描述】:

阿罗哈。我试图在一个图中绘制多条流动持续时间曲线。流量持续时间曲线显示流量的概率,并具有对数正态 y 轴和概率标度 x 轴。我使用示例here 创建曲线。这是我的代码和曲线截图:

library(waterData)
library(lattice)
library(latticeExtra)

site.no <- "16068000" # streamflow station number
site.file <- importDVs(site.no, code = "00060", stat = "00003") # reads data from NWIS
PrbGrd <- qnorm(c(.001, .01, .10, .30, .50, .70, .90, .99, .999)) # create probablity scale lines
PrbGrdL<-c("99.9", "99", "90", "70", "50", "30", "10", "1", "0.1") # exceedence probability scale labels
ValGrd<-c(seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10)) # create value grid lines
ValGrd<-log10(ValGrd) # convert value grid lines to log
qqmath(~ val, # val = data column with streamflow data
       data = site.file,
       distribution = function(p) qnorm(p),
       main = "Flow-duration curves for streamgages on Kauai",
       pch = 20,
       cex = 0.5,
       xlab = "Percentage of time discharge was equaled or exceeded (%)",
       ylab = "Daily mean discharge, in cubic feet per second",
       xlim = c(max(PrbGrd),min(PrbGrd)),
       scales = list(y=list(log=10,alternating=1),x = list(at = PrbGrd, labels = PrbGrdL, cex = 0.8)),
       yscale.components=yscale.components.log10ticks,
       panel = function(x,...){
         panel.abline(v=PrbGrd ,col="grey",lty=3)
         panel.abline(h=ValGrd,col="grey",lty=3)
         panel.qqmath(x,distribution=qnorm)
       }
)

Click to see my flow-duration curve

我无法在同一图上绘制额外的流动持续时间曲线。我按照其他帖子中的建议尝试了lines()points(),但它们不起作用。理想情况下,我想要一个包含流量站编号列表的文本文件和一个带有文本文件中列出的所有流量站的流量持续时间曲线的图。非常感谢任何输入。谢谢!

针对 Hack-R 的评论,这里有一个例子。我有一个名为“Input_FDC.txt”的文本文件,其中包含站号列表。为简单起见,我有 4 个站号(但实际上,我有 30 多个站)。以下是文本文件的内容:

车站
16071500
16097500
16017000
16068000

带有importDVs() 的代码部分是我不知道如何合并到qqmath() 中的部分,以便我可以在一个图中绘制所有4 条曲线。如果我要绘制几条曲线,Hack-R 建议使用qqmath(~ val + val2 ...) 将起作用。例如,我可以执行以下操作:

input.file <- "Input_FDC.txt" # text file with list of station numbers
path.R <- "C:/Users/ccheng/Documents/R/" # enter directory of text file
stn.file <- read.delim(paste(path.R, input.file, sep = ""), header = TRUE)
site.no <- as.character(stn.file$Stations) # for importDVs to work, station no. needs to be in quotes

for(i in 1:length(site.no)) { 
  x.1 <- importDVs(site.no[i], code = "00060", stat = "00003")
  assign(paste("flow",i,sep=""), x.1$val)
}

PrbGrd <- qnorm(c(.001, .01, .10, .30, .50, .70, .90, .99, .999)) # create probablity scale lines
PrbGrdL<-c("99.9", "99", "90", "70", "50", "30", "10", "1", "0.1") # exceedence probability scale labels
ValGrd<-c(seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10)) # create value grid lines
ValGrd<-log10(ValGrd) # convert value grid lines to log
qqmath(~ flow1 + flow2 + flow3 + flow4,
       distribution = function(p) qnorm(p),
       main = "Flow-duration curves for streamgages on Kauai",
       auto.key = list(points = F, lines = T, text = site.no, columns = 4),
       pch = 20,
       cex = 0.5,
       xlab = "Percentage of time discharge was equaled or exceeded (%)",
       ylab = "Daily mean discharge, in cubic feet per second",
       xlim = c(max(PrbGrd),min(PrbGrd)),
       scales = list(y=list(log=10,alternating=1),x = list(at = PrbGrd, labels = PrbGrdL, cex = 0.8)),
       yscale.components=yscale.components.log10ticks
)

但是对于 30 多条曲线,我正在寻找一种解决方案,该解决方案允许我循环浏览“Input_FDC.txt”文件中的站点列表,而不必在代码中包含 30 多个站点的 ~ flow1 + flow2 + ... flow30。非常感谢任何输入。谢谢!

【问题讨论】:

  • 对不起,“site.no.1”应该是“site.no”。我会更正代码。
  • 仅供参考:lines()points()(甚至add=T)不起作用的原因是qqmath() 使用lattice 包进行绘图,该包不允许像这样的新层.
  • @Vincent 感谢您的澄清。

标签: r plot


【解决方案1】:

假设我的假val2 数据是您要绘制的另一条曲线:

site.file$val2 <- site.file$val*.5
qqmath(~ val + val2, # val = data column with streamflow data
       data = site.file,
       distribution = function(p) qnorm(p),
       main = "Flow-duration curves for streamgages on Kauai",
       pch = 20,
       cex = 0.5,
       xlab = "Percentage of time discharge was equaled or exceeded (%)",
       ylab = "Daily mean discharge, in cubic feet per second",
       xlim = c(max(PrbGrd),min(PrbGrd)),
       scales = list(y=list(log=10,alternating=1),x = list(at = PrbGrd, labels = PrbGrdL, cex = 0.8)),
       yscale.components=yscale.components.log10ticks
)

【讨论】:

  • 嗯,好的,这意味着要使您的解决方案正常工作,我必须将所有流数据合并到一个数据帧中,即 site.file。如果我需要在同一个图中绘制 30 多条曲线,是否可以使用循环或类似函数来循环遍历 val、val2、val3 等...?
  • @ccheng 您不一定必须将它们全部放在同一个 data.frame 中。那部分只是因为您指定了data = 选项。否则,您可以为所有感兴趣的 data.frames 和列提供 dataframe$columname 样式引用。至于将它们组合起来的循环应该很容易,但我需要一个示例来使用。
  • 非常感谢您的意见。我在原始帖子中包含了一个示例。希望您可以使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 2016-01-10
  • 2016-02-03
  • 2016-06-07
  • 2011-09-13
  • 1970-01-01
相关资源
最近更新 更多