在 ggplot2 中自定义 facet_grid 中的 ymax

Customize ymax in facet_grid in ggplot2

我正在使用 facet_grid 来绘制我的数据。我在网格中有三个图,我想修复其中两个图的 ymax(或 ylimit)。目前,我正在使用以下代码

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

输出图为:

我想将前两个(hpanom 和 lofanom)图的范围固定为 0-1,我不关心第三个。这是因为前两个代表概率,因此范围总是固定的,而第三个代表我不知道限制的值。

这里我附上了我的整个数据集,这样复制案例就变得容易了

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")

有点乱,但是您可以通过在 y=1 处使用 size=0 来绘制一个 "invisible" 点,将两个 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