How can I obtain an 'unbalanced' grid of ggplots?
grid.arrange
draws directly on the device; if you want to combine it with other grid objects you need arrangeGrob
, as in
p = rectGrob()
grid.arrange(p, arrangeGrob(p,p,p, heights=c(3/4, 1/4, 1/4), ncol=1),
ncol=2)
Edit (07/2015): with v>2.0.0 you can use the layout_matrix
argument,
grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))
Grid Arrange mutliple ggplots from evaluated text
Making use of lapply
this could be achieved like so:
Note: To make geom_histogram
and geom_dotplot
work I made y = wt
a local aes for both the geom_point
and geom_smooth
as otherwise your code resulted in an error.
library(ggplot2)
library(gridExtra)
p <- ggplot(data = mtcars, aes(x = mpg, color = cyl))
p1 <- p + geom_point(aes(y = wt))
p2 <- p + geom_histogram()
p3 <- p + geom_dotplot()
p4 <- p + geom_smooth(aes(y = wt), method='lm')
tx <- paste0("p", 1:4)
grid.arrange(grobs = lapply(tx, function(x) eval(parse(text = x))), nrow = 2)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
#> `geom_smooth()` using formula 'y ~ x'
How to set heigth of rows grid in graph lines on ggplots (R)?
The lines are far apart because the values of the variable plotted on the y-axis are far apart. If you need them closer together, you fundamentally have 3 options:
- change the scale (e.g. convert the plot to a log scale), although this can make it harder for people to interpret the numbers. This can also change the behavior of each line, not just change the space between the lines. I'm guessing this isn't what you will want, ultimately.
- normalize the data. If the actual value of the variable on the y-axis isn't important, just standardize the data (separately for each value of
leyenda
). - As stated above, you can graph each line separately. The main drawback here is that you need 3 graphs where 1 might do.
Not recommended:
- I know that some graphs will have the a "squiggle" to change scales or skip space. Generally, this is considered poor practice (and I doubt it's an option in ggplot2 because it masks the true separation between the data points. If you really do want a gap, I would look at this post: axis.break and ggplot2 or gap.plot? plot may be too complexe
In a nutshell, the answer here depends on what your numbers mean. What is the story you are trying to tell? Is the important feature of your plots the change between them (in which case, normalizing might be your best option), or the actual numbers themselves (in which case, the space is relevant).
Display a list of plots in a grid with specified columns and rows based on grouping column values
It's not very programmatic in nature, but at least it works. I was inspired by Allan Camerons's answer here.
We can use patchwork::wrap_plots
to get the ggplot graphs into a grid.
Then we can use grid::grid.draw
to draw some textGrobs
manually. You can define the position on the final graph with the x=
and y=
arguments to textGrob
. Since the position is different for each label, we can use purrr:walk2
to combine sets of parameters. (walk2
is like map2
, but doen't return anything.) The rot=
argument can turn the text depending on the axis.
library(patchwork); library(purrr); library(grid)
wrap_plots(plotlist = exdata$plots, ncol = 3, nrow = 5) +
plot_annotation(title = " ")
walk2(seq(0.125,0.875,length.out = 5),unique(exdata$cut),
~grid.draw(textGrob(.y, x = 0.01, y = .x, rot = 90)))
walk2(seq(0.165,0.835,length.out = 3),unique(exdata$dummy),
~grid.draw(textGrob(.y, x = .x, y = 0.98, rot = 0)))
ggplot2: Define plot layout with grid.arrange() as argument of do.call()
You can now do,
grid.arrange(p1,p2,p3,p4, layout_matrix = rbind(c(1,1,1),c(2,3,4)))
How to combine two ggplots with one rotated?
Some adjustments might be necessary for the width
and height
of the second plot, but this seems to work:
p <- qplot(1:10)
library(grid)
grid.newpage()
print(p, vp=viewport(0, 0, width = unit(0.5, "npc"), just = c('left', 'bottom')))
print(p, vp=viewport(0.5, 0, angle = 90, height = unit(0.8, "npc"), width = 0.55, just = c('left', 'top')))
Three ggplots in a 2x2 grid
Yes you can do this using arrangeGrob / grid.arrange from gridExtra.
See here
To do this for your example, you'd do
library(ggplot2)
library(gridExtra)
g1 <- qplot(rnorm(10), rnorm(10))
g2 <- qplot(rpois(100, 10), geom = "histogram")
g3 <- qplot(rnorm(10))
my_plots <- list(g1, g2, g3)
my_layout <- rbind(c(1, 1), c(2, 3))
grid.arrange(grobs = my_plots, layout_matrix = my_layout)
Change whitespace around R ggplots from grid layout engine
One option to achieve your desired result would be to draw a rectGrob
with the desired fill color and drawing the ggplot on top of it:
set.seed(42)
animals <- as.data.frame(
table(
Species =
c(
rep("Moose", sample(1:100, 1)),
rep("Frog", sample(1:100, 1)),
rep("Dragonfly", sample(1:100, 1))
)
)
)
library(ggplot2)
library(grid)
g <- ggplot(animals, aes(x = "", y = Freq, fill = Species)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
theme(plot.background = element_rect(fill = "lightblue", color = "lightblue"))
grob <- rectGrob(gp = gpar(fill = "lightblue", col = "lightblue"))
grid.newpage()
grid.draw(grob)
grid.draw(ggplotGrob(g))
qplot does not work with grid.arrange function in R
It looks like the data frame needs to be generated when storing the plots in a loop. The following works as expected. Please see this link for a more detailed discussion: Storing plot objects in a list
library(grid)
library(gridExtra)
library(ggplot2)
plot_freq <- function(arrs){
g <- list()
for(i in c(1:length(arrs))){
y_val <- get(arrs[i])
g[[i]] <- qplot(data = data.frame(y = y_val, x = c(1,2,3,4,5)), x , y)
#g[[i]] <- qplot(y = y_val, x = c(1,2,3,4,5))
}
grid.arrange(grobs = g, ncol= 3)
}
a <- c(4,4,4,4,4)
b <- c(5,5,5,5,5)
c <- c(6,6,6,6,6)
plot_freq(c("a", "b", "c"))
generating this plot (please note Y-axis is different) in each sub-plot.
Related Topics
Remove Multiple Objects with Rm()
Select Every Other Element from a Vector
Concatenate Unique Strings After Groupby in R
How to Display All X Labels in R Barplot
Cumulative Sum That Resets When 0 Is Encountered
How to Generate All Possible Combinations of Vectors Without Caring for Order
How to Convert R Markdown to HTML? I.E., What Does "Knit HTML" Do in Rstudio 0.96
How to Put a Geom_Sf Produced Map on Top of a Ggmap Produced Raster
How to Get Name of Variable in R (Substitute)
Hosting and Setting Up Own Shiny Apps Without Shiny Server
Extract Matrix Column Values by Matrix Column Name
Reduce PDF File Size of Plots by Filtering Hidden Objects
Re-Ordering Factor Levels in Data Frame
For Loop Over Dygraph Does Not Work in R
How to Work with Large Numbers in R
Extract Every Nth Element of a Vector
Difference Between Passing Options in Aes() and Outside of It in Ggplot2