Gggplot grid.arrange all in one row to look like a seamless plot

余生长醉 提交于 2021-01-29 20:31:28

问题


I am attempting to implement a solution from here. Unfortunately I cant get nested facets package uploaded and attempting to format a the facet titles by using the solution to use gridextra package.

However, what I cant seem to figure out how to do is put all of the graphs on one row, as in it would look like one single bar plot. Need to modify this line somehow: grid.arrange(., ncol = 1). In my data all plots will be of equal size with two data points each.

Code borrowed is:

library(ggplot2)
library(gridExtra)
library(dplyr)

dat <-
  structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
  "Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068, 
  -27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
  ), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
  1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464, 
  0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
  ), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
  "Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
  "Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept", 
  "B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")

dat

# A ggplot object with NO data.  Omit the order from the facet_grid call
g <- 
  ggplot() +
  aes(Species, B) +
  geom_point() +
  facet_grid(. ~ Family,
             scales = "free", space = "free") +
  ylim(range(dat$B)) +
  xlab("")

# Build a seperate graphic for each Order and title
plots <-
  lapply(unique(dat$Order), function(o) {
           g %+% dplyr::filter_(dat, ~ Order == o) + ggtitle(o)
             })

# build as Grobs and plot via gridExtra::grid.arrange
plots %>%
  lapply(ggplotGrob) %>%
  arrangeGrob(grobs = .) %>%
  grid.arrange(., ncol = 1)


回答1:


I would suggest a patchwork package approach. You can define the number of rows and columns you want in a practical way. Next the code with our data being dat:

We create a list for our data:

library(ggplot2)
library(dplyr)
library(patchwork)
#Create list
List <- split(dat,dat$Order)

We build a function for plots:

#Now function to plot
myplotfun <- function(x)
{
  G <- ggplot(x,aes(x=Family,y=B))+
    geom_point() +
    facet_grid(. ~ Family,
               scales = "free", space = "free") +
    ylim(range(x$B)) +
    xlab("")+ggtitle(unique(x$Order))
  return(G)
}

Finally we apply the function and plot using patchwork functions:

#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)

Output:

You can add other details according to what you want.



来源:https://stackoverflow.com/questions/63578544/gggplot-grid-arrange-all-in-one-row-to-look-like-a-seamless-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!