【问题标题】:Automatically scale x-axis by date range within a factor using xyplot()使用 xyplot() 在一个因子内按日期范围自动缩放 x 轴
【发布时间】:2014-02-28 23:54:54
【问题描述】:

我一直在尝试编写一个 R 脚本,该脚本将为一组由 Deployment_ID 标识的位置绘制日期-临时序列。

理想情况下,输出 pdf 的每一页都应包含 Deployment_ID 的名称(检查)、带有适当轴(检查)和正确缩放 x 轴的图表,以最好地显示该特定 Deployment_ID 的日期-临时系列(不检查)。

目前,该脚本会生成一个 pdf,显示日期列中整个日期范围内的每个 ID(即 1988-2010),而不是仅显示相关日期(即仅 2005 年),这会压缩散点图陷入无用。

我很确定这与您定义 xlim 的方式有关,但我不知道如何让 R 在绘制绘图时访问每个因素的日期最小值和日期最大值。

到目前为止我的脚本:

#Get CSV to read data from, change the file path and name
data <- read.csv(file.path("C:\Users\Person\Desktop\", "SampleData.csv"))

#Make Date real date - must be in yyyy/mm/dd format from the csv to do so
data$Date <- as.Date(data$Date)

#Call lattice to library, note to install.packages(lattice) if you don't have it
library(lattice)

#Make the plots with lattice, this takes a while.
dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
   data=data, 
   stack = TRUE, 
   auto.key = list(space = "right"), 
   layout = c(1,1),
   ylim = c(-10,40)
   )

#make the pdf
pdf("Dataplots_SampleData.pdf", onefile = TRUE)

#print to the pdf? Not really sure how this works. Takes a while.
print(dataplot)
dev.off()

【问题讨论】:

    标签: r date pdf-generation axis lattice


    【解决方案1】:

    使用 scales 参数。试试这个

    dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
    data=data, 
    stack = TRUE, 
    auto.key = list(space = "right"), 
    layout = c(1,1),
    scales= list( relation ="free")
      )
    

    【讨论】:

    • 我认为这只是调用整个日期集的最小值和最大值,而我正在寻找的是该特定因素内日期的最小值和最大值。我不确定如何更改 min(data$Date) 以引用所引用的特定因素。这有意义吗?
    • 太棒了!正是我想要做的!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多