gpt4 book ai didi

r - facet_wrap(ggplot2)中的多个标题

转载 作者:行者123 更新时间:2023-12-04 03:50:28 25 4
gpt4 key购买 nike

我正在尝试使用facet_wrap和ggplot2将多个标题添加到绘图中。说您例如拥有过去两年的季度数据,并且希望将季度数据与两个主要标题进行图形化比较; 2014年和2015年,以及相应季度的标题。

我走了这么远:

data <- rnorm(10)

A1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2014)
A2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2014)
A3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2014)
A4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2014)

N1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2015)
N2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2015)
N3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2015)
N4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2015)

A <- rbind(A1, A2, A3, A4)
N <- rbind(N1, N2, N3, N4)
tmp <- data.frame(rbind(A, N))

ggplot(data=tmp, aes(x=X, y=Y)) + geom_line() + facet_wrap(~year + Q, scales="free", ncol=4)

这给了我这张图:
enter image description here

相反,我希望“2014”和“2015”位于每个子图上方的两个单独的灰色框中。那可能吗?

谢谢!

最佳答案

使用Sandy Muspratt建议的代码here,我可以得出:

library(ggplot2)
library(gtable)

data <- rnorm(10)

A1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2014)
A2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2014)
A3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2014)
A4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2014)

N1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2015)
N2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2015)
N3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2015)
N4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2015)

A <- rbind(A1, A2, A3, A4)
N <- rbind(N1, N2, N3, N4)
tmp <- data.frame(rbind(A, N))

p <- ggplot(data=tmp, aes(x=X, y=Y)) + geom_line() + facet_wrap(~year + Q, scales="free", ncol=4)


z <- ggplotGrob(p)

# New title strip at the top
z <- gtable_add_rows(z, unit(1, "lines"), pos = 0) # New row added to top

# Display the layout to select the place for the new strip
gtable_show_layout(z) # New strip goes into row 2
# New strip spans columns 4 to 13

z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = grey(0.8), size = .5)),
textGrob("2014", vjust = .27,
gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 2, 4, 2, 13, name = c("a", "b"))

# Add small gap between strips - below row 2
z <- gtable_add_rows(z, unit(2/10, "line"), 2)


# New title strip in the middle
z <- gtable_add_rows(z, unit(1, "lines"), pos = 8) # New row added to top
# Display the layout to select the place for the new strip
gtable_show_layout(z)
# New strip goes into row 9
# New strip spans columns 4 to 13

z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = grey(0.8), size = .5)),
textGrob("2015", vjust = .27,
gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 9, 4, 9, 13, name = c("a", "b"))

# Add small gap between strips - below row 2
z <- gtable_add_rows(z, unit(2/10, "line"), 9)


# Draw it
grid.newpage()
grid.draw(z)

哪个会给你这张图: enter image description here

关于r - facet_wrap(ggplot2)中的多个标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com