简体   繁体   中英

ggplot place facet between two rows of facets

I have 9 plots with 3 time series in each plot, one of these plots contains only one curve and it's the reference plot which I would like to place in between the two rows that contain the other 8 plots. Is there an easy way to do so? I use facet_wrap(~density,nrow=2) but I get one row with 5 and another with 4 plots. I am sure other people had this problem, is there an easy way around to organize the position of this reference plot, or do I have to create two separate plots and overlay them? Otherwise I might have to move this reference plot in all the other plots but it seems redundant information.

This is my current result, but as you can see it's not very well laid out. 在此处输入图片说明

The graphic you are looking for can be generated with gridArrange from the gridExtra package. Here is an example using the storms data set from the dplyr .

library(ggplot2)
library(gridExtra)
library(dplyr)
data(storms, package = 'dplyr')
str(storms)

## Classes 'tbl_df', 'tbl' and 'data.frame':    10010 obs. of  13 variables:
##  $ name       : chr  "Amy" "Amy" "Amy" "Amy" ...
##  $ year       : num  1975 1975 1975 1975 1975 ...
##  $ month      : num  6 6 6 6 6 6 6 6 6 6 ...
##  $ day        : int  27 27 27 27 28 28 28 28 29 29 ...
##  $ hour       : num  0 6 12 18 0 6 12 18 0 6 ...
##  $ lat        : num  27.5 28.5 29.5 30.5 31.5 32.4 33.3 34 34.4 34 ...
##  $ long       : num  -79 -79 -79 -79 -78.8 -78.7 -78 -77 -75.8 -74.8 ...
##  $ status     : chr  "tropical depression" "tropical depression" "tropical depression" "tropical depression" ...
##  $ category   : Ord.factor w/ 7 levels "-1"<"0"<"1"<"2"<..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ wind       : int  25 25 25 25 25 25 25 30 35 40 ...
##  $ pressure   : int  1013 1013 1013 1013 1012 1012 1011 1006 1004 1002 ...
##  $ ts_diameter: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ hu_diameter: num  NA NA NA NA NA NA NA NA NA NA ...

Let's create two graphics. The first graphic will be only form category == -1 storms (this would be the control group in your question). The second graphic will be a facteted graphic for the category > -1 storm

First, we'll build a generic ggplot object for the graphics.

graphic <-
  ggplot() + 
  aes(x = long, y = lat, color = category) +
  geom_point() +
  facet_wrap( ~ category) +
  scale_color_hue(breaks = levels(storms$category),
                  labels = levels(storms$category),
                  drop = FALSE)

Next we build the two graphics as needed.

g1 <- graphic %+% dplyr::filter(storms, category == -1) + theme(legend.position = "none")
g2 <- graphic %+% dplyr::filter(storms, category != -1)

gridExtra::grid.arrange can take a layout matrix where the numbers 1 and 2 denote the first and second graphics passed to the function. (This works for a lot more than just two graphics, by the way.) By repeating the values of 1 and 2 in the matrix we can control the relative size of the two graphics in the graphics device.

gridExtra::grid.arrange(g1, g2,
                        layout_matrix = 
                          matrix(c(1, 1, 1, 2, 2, 2, 2, 2,
                                   1, 1, 1, 2, 2, 2, 2, 2,
                                   1, 1, 1, 2, 2, 2, 2, 2),
                                 byrow = TRUE, nrow = 3) 
                        )

在此处输入图片说明

If I understand the question correctly you could reformat your data with appropriate facetting variables to introduce a new row of reference panels

library(ggplot2)

d <- data.frame(x=rep(1:10, 8), y = rnorm(80), 
                f=gl(8,10, ordered = TRUE))
d$f1 <- factor(d$f <= 4, labels=c(1,3))
d$f2 <- as.numeric(d$f) %% 4

d2 <- data.frame(x=1:10, y=0, f1 = 2)

ggplot(d, aes(x,y)) + 
  geom_point(aes(colour=f)) + 
  geom_point(data=d2, colour="black") + 
  facet_grid(f1~f2)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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