0

this is a piece of my df:

    structure(list(Date.time = structure(c(1372846200, 1372846800, 
1372847400, 1372848000, 1372848600, 1372849200, 1372849800, 1372850400, 
1372851000, 1372851600, 1372852200, 1372852800, 1372853400, 1372854000, 
1372854600, 1372855200, 1372855800, 1372856400, 1372857000, 1372857600, 
1372858200, 1372858800, 1372859400, 1372860000, 1372860600, 1372861200, 
1372861800, 1372862400, 1372863000, 1372863600), class = c("POSIXct", 
"POSIXt"), tzone = ""), P = c(1114.3, 1114.5, 1114.3, 1114.1, 
1114.1, 1113.9, 1114.1, 1113.9, 1113.9, 1113.9, 1114.1, 1113.9, 
1114.1, 1113.9, 1113.9, 1113.9, 1113.9, 1113.9, 1113.7, 1114.3, 
1114.1, 1114.3, 1114.3, 1114.3, 1114.3, 1114.3, 1114.3, 1114.3, 
1114.3, 1114.5), T = c(11.92, 11.92, 11.92, 11.92, 11.92, 11.93, 
11.93, 11.93, 11.93, 11.93, 11.93, 11.93, 11.94, 11.94, 11.94, 
11.94, 11.94, 11.94, 11.94, 11.94, 11.94, 11.94, 11.94, 11.94, 
11.95, 11.94, 11.94, 11.95, 11.94, 11.94), ID = c("J9335", "J9335", 
"J9335", "J9335", "J9335", "J9335", "J9335", "J9335", "J9335", 
"J9335", "J9335", "J9335", "J9335", "J9335", "J9335", "J9335", 
"J9335", "J9335", "J9335", "J9335", "J9335", "J9335", "J9335", 
"J9335", "J9335", "J9335", "J9335", "J9335", "J9335", "J9335"
)), .Names = c("Date.time", "P", "T", "ID"), row.names = c(NA, 
30L), class = "data.frame")

I want to plot on the same plot the variable P and T as function of Date.time: each variable should have its own axis. It's quite simple with the plot function:

plot(df$Date.time, df$P, type="l")
par(new=T)
plot(df$Date.time, df$T, type="l", col="blue", yaxt="n", xaxt="n", xlab="n", ylab="n")
axis(4)

enter image description here

Is it possible to do the same thing with ggplot2? I read somewhere that it might be impossible to plot a double y axis graph with gglot2, but that's weird for a powerful package as ggplot.

matteo
  • 4,683
  • 9
  • 41
  • 77
  • I suppose you have read the first 5-10 hits when googling "double y axis in ggplot2"? – Henrik Aug 16 '13 at 09:24
  • yeah I read them. But I didn't understand if it's not possible or just not advisable to have the second y axis on the right side of the graph – matteo Aug 16 '13 at 09:29
  • 1
    As you'll see his answer at the link I just added, Hadley is opposed to this at a relatively fundamental level so it will *never* be supported in ggplot2. You can do it in base R graphics (better than in your example code) but I think Hadley is right that for many applications it produces visually deceptive results. – Thomas Aug 16 '13 at 09:52
  • @Thomas So it's not possible at all. I agree that sometimes having a double y axis can lead to confusion, but in some cases a double plotting is very useful. Thanks – matteo Aug 16 '13 at 09:57

0 Answers0