To preface the question, I am aware that it is discouraged to plot 2 separate y-axes on a single plot, and that it can be confusing and misleading. However, I have to do it in this case, and am having some trouble having things line up. This area is commonly asked about, but I haven't seen any questions about how to do this when the x-indices are slightly off. Part of this may be due to my handling of POSIX & date objects, so if that fixes it please let me know.
I am trying to plot a time-series of soil moisture data, and compare it with precipitation and other parameters. I've tried to use ggplot2, but it doesn't appear very friendly to doing 2 y-axis plots. The data comes in 2 minute intervals, but processes on annual or larger time-scales are being used. An example of the dataset is below. There is some duplication, since the data comes with Year, Julian Day, and Minute but I combine them into a POSIXlt object for plotting and manipulation.
>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 EDIT: full data found here: http://dropcanvas.com/#Bl4hJG9Y4yfg5j (to large to paste)
The Year.Month value is a factor with n values, where n are the number of unique month/year combinations selected. I'm using this to get boxplots of monthly groups. I want to plot the raw data for soil moisture at a depth, then the boxplots of the monthly data on top of it, and then add on monthly precipitation bins. Monthly bins for precipitation come from:
short.sumRain <- aggregate(Precip_.mm. ~ Year.Month, to.analyze, sum) The issue can be seen in the following plot.
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) Which produces the following plot: 
The precipitation label gets cut off a bit (not sure why R is doing that, but if line gets smaller it just appears on top of the labels), and the precipitation points don't match up with the tick-marks from the points plot. I think it is because the first plot is being drawn with a POSIX x-axis, and the other is not, but how can I get those 2 to line-up?
