2015-12-03 62 views
0

我正在使用facet_grid繪製我的數據。我有三個網格圖,我想修復兩個圖的ymax(或ylimit)。目前,我使用下面的代碼在ggplot2中自定義facet_grid中的ymax

f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")+ 
    theme(axis.title.x=element_blank(),axis.title.y=element_blank()) 
f1 <- f + geom_linerange(subset=.(variable=="hpanom")) # require(plyr) for dot function 
f2 <- f1 + geom_linerange(subset=.(variable=="lofanom")) 
f3 <- f2 + geom_line(subset=.(variable=="power")) 
f3 

輸出情節是:

enter image description here

我想從0-1解決前兩個(hpanom和lofanom)範圍內的情節和我做不關心第三個。這是因爲前兩個代表概率,因此範圍總是固定的,而第三個代表的是我不知道範圍的值。

在這裏,我附上我的整個數據集,從而使之成爲容易複製的情況下

structure(list(timestamp = structure(c(1438450200, 1438536600, 
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000, 
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400, 
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800, 
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200, 
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600, 
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000, 
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400, 
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800, 
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200, 
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600, 
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000, 
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400, 
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800, 
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200, 
1440696600, 1440783000, 1440869400, 1440955800), tzone = "Asia/Kolkata", tclass = c("POSIXct", 
"POSIXt"), class = c("POSIXct", "POSIXt")), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("power", "hpanom", 
"lofanom"), class = "factor"), value = c(713.818544290426, 1.60000010638915e-16, 
1588.93456060134, 1080.34496835479, 1602.88616015399, 1325.85664208325, 
1487.27242035101, 647.925289288333, 1.60000010638915e-16, 1280.71707200196, 
1558.88686231823, 1349.32481298453, 1386.23617766603, 995.841940511863, 
961.865845980269, 1153.33076383446, 1601.85509748887, 1346.15843354498, 
1209.13461354976, 1060.91957740428, 1963.3986755642, 1995.90120364349, 
1.60000010638915e-16, 1391.2765167008, 1198.11633766185, 1202.33001076712, 
1508.21685464222, 1299.09592097037, 1.60000010638915e-16, 1.60000010638915e-16, 
0.718682639785718, 0.835994256774337, 0.323313580710195, 0.323826587878846, 
0.252277897807753, 0.427692277575098, 0, 0.737219412602466, 0.835994256774337, 
0.165199776286282, 0.0893388057418736, 0.20964070918484, 0.13145481071022, 
0.408292163070401, 0.824540692174425, 0.71239540630304, 0.323313580710195, 
0.485828441524218, 0.188258288292337, 0.231165659725455, 0.907512281748878, 
0.951462340841186, 0.835994256774337, 0.29824669841755, 0.245223685520087, 
0.108813825489535, 0.387339161244294, 0.359556716396615, 0.835994256774337, 
0.835994256774337, 0.4, 0.43, 0.13, 0.28, 0.11, 0.1, 0.13, 0.43, 
0.43, 0.13, 0.11, 0.01, 0.12, 0.3, 0.6, 0.4, 0.2, 0.11, 0.1, 
0.15, 0.67, 1, 0.43, 0.08, 0.08, 0.12, 0.07, 0.08, 0.43, 0.43 
)), row.names = c(NA, -90L), .Names = c("timestamp", "variable", 
"value"), class = "data.frame") 

回答

1

這是一個黑客位,但你可以通過使用size=0y=1繪製一個「看不見」點強制兩個linerange圖上的限制爲1,如下所示。我也刪除了需要使用plyr

f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y") 
    f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank()) 
    f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom"))) 
# plot invisible point (size=0) to set upper limit of y axis to 1 
    f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")), 
         aes(x=min(timestamp), y=1), size=0) 
    f <- f + geom_line(data=subset(newmel, variable=="power")) 
    f 

更新ggplot 2.0

ggplot 2.0,設置size=0不會再使點不可見。相反,使用colour = NA使其透明。新的解決方案是

library(ggplot2) 
    f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y") 
    f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank()) 
    f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom"))) 
# plot transparent point (colour = NA) to set upper limit of y axis at 1 
    f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")), 
         aes(x=timestamp[1], y=1), colour=NA ) 
    f <- f + geom_line(data=subset(newmel, variable=="power")) 
    f 
相關問題