简体   繁体   English

facet_wrap中的多个标题(ggplot2)

[英]Multiple titles in facet_wrap (ggplot2)

I am trying to add multiple titles to a plot using facet_wrap and ggplot2. 我正在尝试使用facet_wrap和ggplot2为绘图添加多个标题。 Say that you eg have quarterly data over two years, and wants a graphical comparison of the quarterly data with two major titles; 假设你有两年的季度数据,并想要将季度数据与两个主要标题进行图形比较; 2014 and 2015, as well as titles for the respective quarter. 2014年和2015年,以及各季度的标题。

I have come this far: 我走到这一步:

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)

which gives me this graph: 这给了我这个图: 在此输入图像描述

Instead I would like "2014" and "2015" to be in two separate grey boxes above each sub graph. 相反,我希望“2014”和“2015”在每个子图上方的两个单独的灰色框中。 Is that possible? 那可能吗?

Thanks! 谢谢!

Using the code here , as suggested by Sandy Muspratt, i can come up with: 根据Sandy Muspratt的建议,使用此处的代码,我可以提出:

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)

Which will give you this graph: 这将给你这个图: 在此输入图像描述

You beat me to it RHA, I wrote almost an identical code to yours before I saw your post. 你把我打败了RHA,在看到你的帖子之前,我给你写了几乎相同的代码。 Anyhow, thanks! 无论如何,谢谢!

I also wanted to remove "2014" and "2015" from the grey boxes (which I didn't specified in my first post), so I had to make some more modifications. 我还想从灰色框中删除“2014”和“2015”(我在第一篇文章中没有说明),所以我不得不做一些修改。

With some inspiration from here , here , and here , I came up with the following (really ugly) code: 这里得到一些灵感, 在这里 这里 ,我提出了以下(非常难看)的代码:

data14 <- rnorm(10)
data15 <- rnorm(10, mean = 500)

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

N1 <- data.frame("Y"=data15, "X"=1:10, "Q"=1, "year"=2015)
N2 <- data.frame("Y"=data15, "X"=1:10, "Q"=2, "year"=2015)
N3 <- data.frame("Y"=data15, "X"=1:10, "Q"=3, "year"=2015)
N4 <- data.frame("Y"=data15, "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))

Then I made a simple function naming the variables correctly 然后我做了一个简单的函数来正确命名变量

labFunc <- function(data, var1, var2, names) {
  data$id <- NA
  loop <- length(levels(factor(data[[var2]])))

  for (i in 1:loop) {
    data[data[[var1]] == 2014 & data[[var2]] == levels(factor(data[[var2]]))[i], "id"] <- names[i]
    data[data[[var1]] == 2015 & data[[var2]] == levels(factor(data[[var2]]))[i], "id"] <- paste(names[i], "")
  }

  first <- levels(factor(data$id))[seq(from=1, to = length(levels(factor(data$id))), by = 2)]
  second <- levels(factor(data$id))[seq(from=2, to = length(levels(factor(data$id))), by = 2)]

  data$id <- factor(data$id, levels=paste(c(first, second)))
  return(data)
}

names <- c("Q1", "Q2", "Q3", "Q4")
data <- labFunc(tmp, "year", "Q", names)

Make the plot: 制作情节:

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

And then finally add the major labels 然后最后添加主要标签

z <- ggplotGrob(p)

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

#z <- gtable_add_rows(z, unit(9, "lines"), pos = 0)  # New row added to top


# Check the layout
gtable_show_layout(z)   # New strip goes into row 2 
# New strip spans columns 4 to 8

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, 14, name = c("a", "b"))

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"))), 7, 4, 7, 14, name = c("a", "b"))



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

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

在此输入图像描述

This was a bit more trickier than I thought, but thank you all for the help! 这比我想象的要复杂一些,但谢谢大家的帮助!

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

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