1

I have a plot with Celsius on y axis:

plot(y=0:100,x=0:100, main="temperature",xlab="time",ylab="Celsius",type="l")

How can I plot an identical secondary Y scale, but with the units that are shown in Celsius on the y axis as Fahrenheit on second y axis. T(°F) = T(°C) × 9/5 + 32 I need the label positions of the two y axis to correspond exactly so that the secondary y label shows the converted value that is on the primary y label.

Thank you for your help.

adam.888
  • 7,686
  • 17
  • 70
  • 105
  • 1
    Note: we don't use centigrade anymore, but Celsius. –  Aug 19 '15 at 09:39
  • Possible duplicate of http://stackoverflow.com/questions/21375505/r-creating-graphs-with-two-y-axes – maj Aug 19 '15 at 09:44
  • 4
    possible duplicate of [How can I plot with 2 different y-axes?](http://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes) –  Aug 19 '15 at 09:46

2 Answers2

2

In it's roughest form you can use axis():

plot(y=0:100,x=0:100, main="temperature",xlab="time",ylab="Centigrate",type="l")

axis(4, at=0:100, labels=0:100 * 9/5 + 32)

You can get fewer labels by using seq(0, 100, by=10). You'll also want to set par(mar=) to fit your axis label.

MikeRSpencer
  • 1,276
  • 10
  • 24
2

Here's an example with pretty ticks on left and right sides.

set.seed(1)
temp <- rnorm(10) * 10 + 10  # random temperatures

par(mar=c(5.1, 5.1, 5.1, 5.1))  # extra margin space
plot(temp, ylab="degrees C")
ylim <- par("yaxp")[1:2]  # get range of original y-axis

# slope and offset coefficients to convert between degrees C and F
slope <- 9/5
offset <- 32
alt.ax <- pretty(slope * ylim + offset)
alt.at <- (alt.ax - offset) / slope

axis(side=4, at=alt.at, labels=alt.ax, srt=90)
mtext("degrees F", side=4, line=par("mgp")[1])

output

Mike T
  • 41,085
  • 18
  • 152
  • 203