2014-10-30 33 views
1

我已经制作了一个带有三个图的facet_grid,但是,我希望其中一个绘图具有不同的y轴标题。在facet_grid上放置两个不同的y轴标题

这里是我的数据:

df <- structure(list(year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("2000", 
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
"2009", "2010"), class = "factor"), variable = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L 
), .Label = c("seasonlengths_mean", "largeintegrals_mean", "amplitudes_mean" 
), class = "factor"), value = c(1850.944464, 2027.706638, 1574.997154, 
1734.959513, 1722.652719, 1723.666096, 1621.52355, 1874.67606, 
1558.877827, 1588.589131, 1731.504726, 3150.864739, 3246.418993, 
3021.413284, 3422.282686, 3307.734952, 3320.185729, 3505.83784, 
3254.344441, 3288.9163, 3454.903224, 3183.981956, 212.84742, 
243.530836, 243.530836, 201.9268598, 208.061906, 208.0701673, 
197.0074404, 224.4825376, 170.9265051, 176.7918446, 198.7339252 
)), row.names = c(NA, -33L), .Names = c("year", "variable", "value" 
), class = "data.frame") 

然后,我让我的图...

library (ggplot2) 
library (scales) 

p <- ggplot (data=df, aes(x=year,y=value,group=variable)) + geom_line() + facet_grid(variable~.,scales='free') 
p 

生成此图...

enter image description here

而不必只是一个Y轴标题标签,'值',我想要两个标题..一个顶部图和一个e为底部的两张图。

感谢

-cherrytree

+2

不认为你可以在一个'facet_grid'做到这一点。也许两块地块,从顶部一个,然后'grid.arrange'删除轴 – 2014-10-30 17:24:20

回答

2

参照作为opined在评论上面,你可以做一些类似的方向。

library(ggplot2) 
library(scales) 
library(dplyr) 
p1 <- ggplot (data <- df %>% filter(variable %in% c("seasonlengths_mean")), 
       aes(x=year,y=value,group=variable)) + 
    geom_line() + 
    facet_grid(variable~.,scales='free') + 
    labs(y="value_label_1") + 
    theme(axis.title.x=element_blank()) 

p2 <- ggplot (data <- df %>% 
       filter(variable %in% c("largeintegrals_mean", "amplitudes_mean")), 
       aes(x=year,y=value,group=variable)) + 
    geom_line() + 
    facet_grid(variable~.,scales='free') + 
    labs(y="value_label_2") 

# http://stackoverflow.com/questions/13294952/left-align-two-graph-edges-ggplot 
gA <- ggplotGrob(p1) 
gB <- ggplotGrob(p2) 
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5]) 
gA$widths[2:5] <- as.list(maxWidth) 
gB$widths[2:5] <- as.list(maxWidth) 

p3 <- arrangeGrob(
    gA, gB, nrow = 2, heights = c(0.35, 0.65), 
    main = textGrob(
    "Title of the Graph", 
    just = "top", vjust = 0.75, gp = gpar(fontsize = 14, lineheight = 1, 
              fontface = "bold"))) 
p3 

enter image description here