我已经生成了一个带有三个图的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
制作此图表......
我没有只有一个y轴标题标签,“值”,而是想要两个标题。一个用于顶部图形,一个用于底部两个图形。
由于
-cherrytree
答案 0 :(得分:2)
参考上述评论中所述的方向,你可以做类似的事情。
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