Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo x-axis scales on one R plot
    text
    copied!<p>I am trying to create a plot that shows two different time scales on the x-axis. The problem is that the two time scales have a complicated relationship. </p> <p>I would like to show weather data by the day of year and by the thermal units. Thermal units are the accumulation of the mean temperatures of each day. Some days we get a lot of thermal units, some days not so many. I fit a spline to the relationship between day of year and thermal units and used that to predict thermal unit values for each day. So I do have a nice dataset with the following headers: day of year (day), thermal units (gdd), temperature (temp), precipitation (precip). </p> <p>I created the following figure (may have to open in new window): <img src="https://www.dropbox.com/s/m8rt42hyit8odhn/Climate%202010.pdf" alt="Wrong Scale"> </p> <p>with this code:</p> <pre><code>pdf(file="Climate 2010.pdf", family="Times") par(mar = c(5,4,4,4) + 0.3) plot(cobs10$day, cobs10$precip, col="white", type="h", yaxt="n", xaxt="n", ylab="", xlab="") axis(side=3, col="black", labels=FALSE) at = axTicks(3) mtext(side = 3, text = at, at = at, col="black", line = 1, las=0) mtext("Day of Year", side=3, las=0, line = 3) par(new=TRUE) plot(cobs10$gdd, cobs10$temp, type="l", col="red", yaxt="n", ylab="", xlab="Thermal Units") axis(side=2, col='red', labels=FALSE) at= axTicks(2) mtext(side=2, text= at, at = at, col = "red", line = 1, las=0) mtext("Temperature (C)", side=2, las=0, line=3) par(new=TRUE) plot(cobs10$gdd, cobs10$precip, type="h", col="blue", yaxt="n", xaxt="n", ylab="", xlab="") axis(side=4, col='blue', labels=FALSE) at = axTicks(4) mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0) mtext("Precipitation (cm)", side=4, las=0, line = 3) dev.off() </code></pre> <p>This is exactly what I want, but I realized the x-axis scales are linear here, and they should not be. I put the top x-axis in by making my precipitation data white and writing over it. See what happens when I make it green:</p> <p><img src="https://www.dropbox.com/s/7bhp0s14isi8qco/Climate%202010%20v2.pdf" alt="Obviously wrong scales"> </p> <p>It's obvious things don't match up. <strong>So how can I make the two axes in scale with eachother?</strong></p> <p>Here's the little dataframe I have been using where the time units are matched up by predicting: <a href="https://www.dropbox.com/s/ysoiut7ma5y8q30/cobs10.txt" rel="nofollow">cobs10.txt</a>. "gdd" is thermal units</p> <p>EDIT: Here is some new code that doesn't use par(new=TRUE):</p> <pre><code>par(mar = c(5,4,4,4) + 0.3) plot(cobs10$gdd, cobs10$temp, type="l", col="red", yaxt="n", xlab="", ylab="", ylim=c(-25, 30)) lines(cobs10$gdd, cobs10$precip, type="h", col="blue", yaxt="n", xlab="", ylab="") axis(side=3, col="black", at=cobs10$gdd, labels=cobs10$day) want&lt;-(c(1, 130, 150, 170, 190, 210, 230, 250, 270, 360)) mtext(side = 3, text = want, at = want, col="black", line = 1, las=0) mtext("Day of Year", side=3, las=0, line = 3) axis(side=2, col='red', labels=FALSE) at= axTicks(2) mtext(side=2, text= at, at = at, col = "red", line = 1, las=0) mtext("Temperature (C)", side=2, las=0, line=3) axis(side=4, col='blue', labels=FALSE) at = axTicks(4) mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0) mtext("Precipitation (cm)", side=4, las=0, line = 3) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload