【问题标题】:When using 2 y-axes, match X values使用 2 个 y 轴时,匹配 X 值
【发布时间】:2016-10-26 04:42:47
【问题描述】:

作为问题的序言,我知道不鼓励在一个图上绘制 2 个单独的 y 轴,这可能会造成混淆和误导。但是,在这种情况下我必须这样做,并且在安排事情时遇到了一些麻烦。这个区域经常被问到,但我没有看到任何关于当 x-indices 稍微偏离时如何做到这一点的问题。部分原因可能是由于我处理了 POSIX 和日期对象,所以如果解决了问题,请告诉我。

我正在尝试绘制时间序列的土壤水分数据,并将其与降水量和其他参数进行比较。我尝试使用 ggplot2,但它对 2 个 y 轴绘图似乎不太友好。数据以 2 分钟的间隔出现,但正在使用年度或更大时间尺度的过程。下面是数据集的一个示例。有一些重复,因为数据带有 Year、Julian Day 和 Minute,但我将它们组合成一个 POSIXlt 对象以进行绘图和操作。

>to.analyze[1:10,]
Rowname Year    Julian_Day  Minute  T_.C.   Rel_Humid   Precip_.mm. T_5cm   T_10cm  T_20cm  T_50cm  T_100cm Moist_5cm   Moist_10cm  Moist_20cm  Moist_50cm  Moist_100cm TOD Year.Month  cumRain
2015.137.1  2015    137 0   18.85   19.83   0   31.88   30.08   25.66   21.36   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:00:00 AM   2015.May    0
2015.137.2  2015    137 2   18.99   19.15   0   31.8    30.06   25.66   21.37   20.6    0.047   0.054   0.07    0.125   0.134   5/17/2015 12:02:00 AM   2015.May    0
2015.137.3  2015    137 4   19.12   20.3    0   31.72   30.03   25.69   21.37   20.6    0.047   0.054   0.07    0.125   0.134   5/17/2015 12:04:00 AM   2015.May    0
2015.137.4  2015    137 6   18.99   21.65   0   31.64   30.01   25.7    21.37   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:06:00 AM   2015.May    0
2015.137.5  2015    137 8   18.68   22.59   0   31.55   29.98   25.72   21.37   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:08:00 AM   2015.May    0
2015.137.6  2015    137 10  18.16   23.69   0   31.47   29.96   25.72   21.37   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:10:00 AM   2015.May    0
2015.137.7  2015    137 12  17.69   24.8    0   31.38   29.93   25.75   21.37   20.6    0.047   0.054   0.07    0.125   0.134   5/17/2015 12:12:00 AM   2015.May    0
2015.137.8  2015    137 14  18.06   23.73   0   31.3    29.9    25.75   21.37   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:14:00 AM   2015.May    0
2015.137.9  2015    137 16  18.39   22.97   0   31.2    29.88   25.77   21.37   20.6    0.046   0.054   0.07    0.125   0.134   5/17/2015 12:16:00 AM   2015.May    0
2015.137.10 2015    137 18  18.47   22.96   0   31.11   29.84   25.77   21.37   20.6    0.047   0.054   0.07    0.125   0.134   5/17/2015 12:18:00 AM   2015.May    0

编辑:在此处找到完整数据:http://dropcanvas.com/#Bl4hJG9Y4yfg5j(要大到粘贴)

Year.Month 值是具有 n 个值的因子,其中 n 是所选的唯一月/年组合的数量。我正在使用它来获取每月组的箱线图。我想绘制深度土壤水分的原始数据,然后在其上绘制每月数据的箱线图,然后添加每月降水箱。每月的降水分类来自:

short.sumRain <- aggregate(Precip_.mm. ~ Year.Month, to.analyze, sum)

问题可以在下图中看到。

plot(to.analyze$TOD,to.analyze[, "Moist_10cm"], xlab = "Time",
     ylab = "Volumetric Water Content", type = "l", xaxt = "n",
     main = main, ylim = c(0, .3))
r <- as.POSIXct(range(to.analyze$TOD))
axis.POSIXct(1, at = seq(r[1],r[2], by = "months"), labels = TRUE, format = "%Y-%m", 
         las = 1, cex = 0.1)

# Add boxplot
par(new = TRUE)
boxplot(to.analyze$Moist_10cm ~ to.analyze$Year.Month, range = 0, type = "l", 
    xaxt = "n", yaxt = "n", ylim = c(0, .3), boxwex = 0.3)

# Add monthly rainfall sums
par(new = TRUE)
plot(short.sumRain$Precip_.mm., type = "o", col = "blue", xaxt = "n", yaxt = "n",
 xlab = "", ylab = "", pch = 1)
axis(4)
mtext("Precip (mm)", side = 4, line = 1.5)

这会产生以下情节:

降水标签被切断了一点(不知道为什么 R 这样做,但如果线变小它只会出现在标签的顶部),并且降水点与来自的刻度线不匹配点图。我认为这是因为第一个图是用 POSIX x 轴绘制的,而另一个不是,但是我怎样才能让这 2 个图排列起来呢?

【问题讨论】:

  • 您能否以可重复的格式提供该数据或使用内置数据作为您的示例?
  • 我放了完整的数据集。

标签: r plot


【解决方案1】:

我认为你的问题的背景是POSIXct classas.numeric( as.POSIXct("2011-01-01", "GMT") ) 是 1293840000,这是真正的坐标值。
默认情况下,boxplot() 更改 xy 坐标并使用 1、2、~ 作为它们的 x 位置。参数add=T 使boxplot() 使用当前坐标,而参数at 给出位置。
当您使用2 个单独的y 轴时,使用相同的xlim 会更安全。

to.analyze <- read.csv("FMS.to.analyze.csv") # read the data

to.analyze$r <- as.POSIXct(to.analyze$TOD) # do class change first and add it as a new column
levels(to.analyze$Year.Month)       # I didn't use Year.Month because of alphabetical levels.
to.analyze$Year.Month2 <- substr(to.analyze$r, 1, 7)
# make a new column, Year.Month2 by extracting POSIXct class data
short.sumRain <- aggregate(Precip_.mm. ~ Year.Month2, to.analyze, sum)

x.range <- as.POSIXct(c("2013-05-01", "2013-09-01"))   # decide the x-range on plot
x.month <- as.numeric( as.POSIXct(paste0( unique(to.analyze$Year.Month2), "-15")) )
# decide the day where boxplot and short.sumRain's points are. (I used 15th of every month)
   # (paste0() joins year-month to -day. as.numeric(as.POSIXct()) calculate the real value.)

par(mar=c(4, 4.2, 2.5, 4.2))                           # set margin (down, left, up, right)
plot(Moist_10cm ~ r, to.analyze, xlab = "Time", xaxt = "n", ylab = "Volumetric Water Content", 
     main = "FMS", type = "l", ylim = c(0, .3), xlim = x.range)      # set xlim = x.range
axis.POSIXct(1, at = seq(x.range[1], x.range[2], by = "months"), labels = TRUE, 
             format = "%Y-%m", las = 1, cex = 0.1)
# you can draw additional other depth data by succeeding code.
# lines(Moist_100cm ~ r, to.analyze, col = "gray")

# use boxplot()'s arguments add=T and at. (If you don't want transparency, please delete col = "#00000020")
boxplot(Moist_10cm ~ Year.Month2, to.analyze, boxwex = 900000, 
        at = x.month, axes = F, col = "#00000020", add = T)

par(new = TRUE)   ### HERE, the first xy-coordinates are reset.
plot(short.sumRain$Precip_.mm. ~ x.month, type = "o", col = "blue", 
     axes=F, ann=F, xlim = x.range)           # use the same xlim
axis(4)
mtext("Precip (mm)", side = 4, line = 2)

# If you needn't keep TOD and/or Year.Mont, you can overwrite it 
# (e.g., to.analyze$TOD <- as.POSIXct(to.analyze$TOD) ) instead of making a new column.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-27
    • 2021-11-20
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多