如何在ggplot2中的y轴右侧显示数据

时间:2013-05-23 13:39:54

标签: r ggplot2

我有这个数据框,叫做test。它有3列,DATEUSED_CORESRELATIVE_PERCENT。我想创建一个ggplot图,我希望在x轴上显示DATE,在y轴上显示RELATIVE_PERCENT,在y轴的右侧显示USED_CORES

这是我的数据框:

dput(head(test,30))  

structure(list(DATE = structure(c(1364359560, 1364359590, 1364359620, 
1364359650, 1364359680, 1364359710, 1364359740, 1364359770, 1364359800, 
1364359830, 1364359860, 1364359890, 1364359920, 1364359950, 1364359980, 
1364360010, 1364360040, 1364360070, 1364360100, 1364360130, 1364360160, 
1364360190, 1364360220, 1364360250, 1364360280, 1364360310, 1364360340, 
1364360370, 1364360400, 1364360430), class = c("POSIXct", "POSIXt"
), tzone = ""), USED_CORES = c(2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 
2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.24, 2.4, 2.4, 2.4, 2.4, 2.4, 
2.24, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.56), 
    RELATIVE_PERCENT = c(15, 15, 15, 15, 15, 15, 15, 15, 15, 
    15, 15, 15, 14, 15, 15, 15, 15, 15, 14, 15, 15, 15, 15, 15, 
    15, 15, 15, 15, 15, 16)), .Names = c("DATE", "USED_CORES", 
"RELATIVE_PERCENT"), row.names = c(NA, 30L), class = "data.frame")

我可以这样做以在x轴上显示DATE并在y轴上显示RELATIVE_PERCENT

ggplot(test, aes(DATE, RELATIVE_PERCENT)) + 
      geom_point(colour="blue", size=1) + theme_bw()

如何在y轴的右侧显示USED_CORES

1 个答案:

答案 0 :(得分:1)

显然这在ggplot2中是不可能的,这是有充分理由的。看看这个问题:

Plot with 2 y axes, one y axis on the left, and another y axis on the right

特别是在接受答案的维基链接中:

http://rwiki.sciviews.org/doku.php?id=tips:graphics-ggplot2:aligntwoplots