简体   繁体   English

在facet_grid上放置两个不同的y轴标题

[英]place two different y axis titles on facet_grid

I have produced a facet_grid with three plots, however, I want one of the plots to have a different y-axis title. 我生成了带有三个图的facet_grid,但是,我希望其中一个图具有不同的y轴标题。

Here is my data: 这是我的数据:

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

Then I make my graph... 然后我画图...

library (ggplot2)
library (scales)

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

Produces this graph... 产生此图...

在此处输入图片说明

Instead of having just one y axis title label, 'values', I want two titles..one for the top graph and one for the bottom two graphs. 我不仅要有一个y轴标题标签“值”,还需要两个标题..一个用于顶部图形,一个用于底部两个图形。

Thanks 谢谢

-cherrytree -cherrytree

With reference to the direction as opined in the comments above, you could do something similar to this. 参考上面评论中指定的方向,您可以执行类似的操作。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM