2016-06-23 39 views
1

为了说明这个问题,我知道不希望在单个图上绘制2个独立的y轴,并且可能会造成混淆和误导。但是,在这种情况下,我必须这样做,并且遇到一些麻烦事情。这个领域通常被问到,但是当x指数稍微偏离时,我还没有看到有关如何做到这一点的任何问题。部分原因可能是由于我处理了POSIX &日期对象,所以如果修复它,请让我知道。当使用2个y轴时,匹配X值

我想绘制一个时间序列的土壤湿度数据,并将其与降水和其他参数进行比较。我试过使用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) 

将会产生以下情节: VWC plot

降水标签被切断了一下​​(不知道为什么R的这样做,但如果线变得更小,它只是出现在标签上) ,降水点与点图上的刻度线不匹配。我认为这是因为第一个绘图是用POSIX x轴绘制的,另一个不是,但是我怎么才能让这两个绘图组合?

+0

你能提供可重复的格式或数据使用内置的数据为你的榜样? –

+1

我放了完整的数据集。 – Heymans

回答

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. 

enter image description here