ggplot2中的双y轴

时间:2013-08-16 09:11:15

标签: r ggplot2

这是我的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")

我想在同一个图上绘制变量P和T作为Date.time的函数:每个变量应该有自己的轴。使用plot函数非常简单:

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

是否可以使用ggplot2做同样的事情?我在某处读到可能无法用gglot2绘制双y轴图,但对于像ggplot这样强大的包来说这很奇怪。

0 个答案:

没有答案
相关问题