How to center plots in layout plot Julia? - plot

I have the following reproducible odd layout plot:
using Plots
plot(rand(100, 5), layout = 5)
Output:
As you can see plot number 4 and 5 are not centered, but the are left aligned. I would like these to be more centered without changing their height and width so they stay the same as other plots. So I was wondering if anyone knows how to center these plots in a layout plot in Julia?

UPDATE: As requested, the layout has to keep all the plots of same size:
p1 = plot(rand(10));
p2 = plot(rand(10));
p3 = plot(rand(10));
p4 = plot(rand(10));
p5 = plot(rand(10));
pb1 = plot(legend=false,grid=false,foreground_color_subplot=:white);
pb2 = plot(legend=false,grid=false,foreground_color_subplot=:white);
l = #layout [grid(1,3); grid(1,4, widths=[1/6,1/3,1/3,1/6])]
plot(p1,p2,p3,pb1,p4,p5,pb2; layout = l)
Giving:
Try:
julia> l = #layout [grid(1,3); grid(1,2)]
2×1 Matrix{Any}:
Plots.GridLayout(1, 3)
Plots.GridLayout(1, 2)
julia> plot(rand(10,5); layout = l)
Mostly self explanatory, but there is also more material on layouts at:
https://docs.juliaplots.org/latest/layouts/

Related

Left-aligning ggplot when saved while using a fixed aspect ratio

I'm building a custom ggplot theme to standardize the look & feel of graphs I produce. The goal is more complex than this minimal example, so I'm looking for a general solution. I have a few key goals:
I want all graphs to export at the same size (3000 pixels wide, 1500 pixels high).
I want to control the aspect ratio of the plot panel itself.
I want to use textGrobs to include figure numbers.
I want the image to be left-aligned
The challenge I'm facing is that when combining these two constraints, the image that gets saved centers the ggplot graph within the window, which makes sense as a default, but looks bad in this case.
I'm hoping there's a general solution to left-align the ggplot panel when I export. Ideally, this will also work similarly for faceted graphs.
It seems that something should be possible using one of or some combination of the gridExtra, gtable, cowplot, and egg packages, but after experimenting for a few hours I'm at a bit of a loss. Does anybody know how I can accomplish this? My code is included below.
This is the image that gets produced. As you can see, the caption is left-aligned at the bottom, but the ggplot itself is horizontally centered. I want the ggplot graph left-aligned as well.
Graph output: https://i.stack.imgur.com/5EM2c.png
library(ggplot2)
# Generate dummy data
x <- paste0("var", seq(1,10))
y <- LETTERS[1:10]
data <- expand.grid(X=x, Y=y)
data$Z <- runif(100, -2, 2)
# Generate heatmap with fixed aspect ratio
p1 <- ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
labs(title = 'A Heatmap Graph') +
theme(aspect.ratio = 1)
# A text grob for the footer
figure_number_grob <- grid::textGrob('Figure 10',
x = 0.004,
hjust = 0,
gp = grid::gpar(fontsize = 10,
col = '#01A184'))
plot_grid <- ggpubr::ggarrange(p1,
figure_number_grob,
ncol = 1,
nrow = 2,
heights = c(1,
0.05))
# save it
png(filename = '~/test.png', width = 3000, height = 1500, res = 300, type = 'cairo')
print(plot_grid)
dev.off()
I was able to find a solution to this that works for my needs, though it does feel a bit hacky.
Here's the core idea:
Generate the plot without a fixed aspect ratio.
Split the legend from the plot as its own component
Use GridExtra's arrangeGrob to combine the plot, a spacer, the legend, and another spacer horizontally
Set the width of the plot to some fraction of npc (normal parent coordinates), in this case 0.5. This means that the plot will take up 50% of the horizontal space of the output file.
Note that this is not exactly the same as setting a fixed aspect ratio for the plot. If you know the size of the output file, it's close to the same thing, but the size of axis text & axis titles will affect the output aspect ratio for the panel itself, so while it gets you close, it's not ideal if you need a truly fixed aspect ratio
Set the width of the spacers to the remaining portion of the npc (in this case, 0.5 again), minus the width of the legend to horizontally center the legend in the remaining space.
Here's my code:
library(ggplot2)
# Generate dummy data
x <- paste0("var", seq(1,10))
y <- LETTERS[1:10]
data <- expand.grid(X=x, Y=y)
data$Z <- runif(100, -2, 2)
# Generate heatmap WITHOUT fixed aspect ratio. I address this below
p1 <- ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
labs(title = 'A Heatmap Graph')
# Extract the legend from our plot
legend = gtable::gtable_filter(ggplotGrob(p1), "guide-box")
plot_output <- gridExtra::arrangeGrob(
p1 + theme(legend.position="none"), # Remove legend from base plot
grid::rectGrob(gp=grid::gpar(col=NA)), # Add a spacer
legend, # Add the legend back
grid::rectGrob(gp=grid::gpar(col=NA)), # Add a spacer
nrow=1, # Format plots in 1 row
widths=grid::unit.c(unit(0.5, "npc"), # Plot takes up half of width
(unit(0.5, "npc") - legend$width) * 0.5, # Spacer width
legend$width, # Legend width
(unit(0.5, "npc") - legend$width) * 0.5)) # Spacer width
# A text grob for the footer
figure_number_grob <- grid::textGrob('Figure 10',
x = 0.004,
hjust = 0,
gp = grid::gpar(fontsize = 10,
col = '#01A184'))
plot_grid <- ggpubr::ggarrange(plot_output,
figure_number_grob,
ncol = 1,
nrow = 2,
heights = c(1,
0.05))
# save it
png(filename = '~/test.png', width = 3000, height = 1500, res = 300, type = 'cairo')
print(plot_grid)
dev.off()
And here's the output image: https://i.stack.imgur.com/rgzFy.png

Draw line between multiple plots in R [duplicate]

Take a very simple example, mfrow=c(1,3); each figure is a different histogram; how would I draw a horizontal line (akin to abline(h=10)) that went across all 3 figures? (That is, even the margins between them.) Obviously, I could add an abline to each figure, but that's not what I want. I can think of a very complicated way to do this by really only having 1 figure, and drawing each 'figure' within it using polygon etc. That would be ridiculous. Isn't there an easy way to do this?
As #joran noted, the grid graphical system offers more flexible control over arrangement of multiple plots on a single device.
Here, I first use grconvertY() to query the location of a height of 50 on the y-axis in units of "normalized device coordinates". (i.e. as a proportion of the total height of the plotting device, with 0=bottom, and 1=top). I then use grid functions to: (1) push a viewport that fills the device; and (2) plot a line at the height returned by grconvertY().
## Create three example plots
par(mfrow=c(1,3))
barplot(VADeaths, border = "dark blue")
barplot(VADeaths, border = "yellow")
barplot(VADeaths, border = "green")
## From third plot, get the "normalized device coordinates" of
## a point at a height of 50 on the y-axis.
(Y <- grconvertY(50, "user", "ndc"))
# [1] 0.314248
## Add the horizontal line using grid
library(grid)
pushViewport(viewport())
grid.lines(x = c(0,1), y = Y, gp = gpar(col = "red"))
popViewport()
EDIT: #joran asked how to plot a line that extends from the y-axis of the 1st plot to the edge of the last bar in the 3rd plot. Here are a couple of alternatives:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
# barplot #1
barplot(VADeaths, border = "dark blue")
X1 <- grconvertX(0, "user", "ndc")
# barplot #2
barplot(VADeaths, border = "yellow")
# barplot #3
m <- barplot(VADeaths, border = "green")
X2 <- grconvertX(tail(m, 1) + 0.5, "user", "ndc") # default width of bars = 1
Y <- grconvertY(50, "user", "ndc")
## Horizontal line
pushViewport(viewport())
grid.lines(x = c(X1, X2), y = Y, gp = gpar(col = "red"))
popViewport()
Finally, here's an almost equivalent, and more generally useful approach. It employs the functions grid.move.to() and grid.line.to() demo'd by Paul Murrell in the article linked to in #mdsumner's answer:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
barplot(VADeaths); vps1 <- do.call(vpStack, baseViewports())
barplot(VADeaths)
barplot(VADeaths); vps3 <- do.call(vpStack, baseViewports())
pushViewport(vps1)
Y <- convertY(unit(50,"native"), "npc")
popViewport(3)
grid.move.to(x = unit(0, "npc"), y = Y, vp = vps1)
grid.line.to(x = unit(1, "npc"), y = Y, vp = vps3,
gp = gpar(col = "red"))
This is the best I can do without thinking about it harder:
par(mfrow = c(1,3),xpd = NA)
for (i in 1:3){
x <- rnorm(200,i)
hist(x)
if (i == 1) segments(par("usr")[1],10,30,10)
}
I'm not sure how to make sure the line ends at the right spot without tinkering. Plotting a segment in each region would solve that, but introduce the issue of having the heights line up properly. But this might be a good starting point, at least.
I'd guess this is easier in grid graphics, but I'd have to do some research to verify.
This article by Paul Murrell shows the use of grid graphics to draw lines between two different coordinate systems, in this case lines that have end points specified in the native space of two separate sub-plots:
Paul Murrell. The grid graphics package. R News, 2(2):14-19, June 2002
It's on page 17 of the PDF article:
http://cran.r-project.org/doc/Rnews/Rnews_2002-2.pdf

R: print multiple types of plots on one page

I am trying to plot multiple plots in one page. I know functions like gridExtra::grid.arrange that can plot graphs generated by ggplot2 package. The problem I am facing is that I have two plots (bar.plot and density.plot below) that are generated by ggplot2 package and one plot generated using limma::vennDiagram function. I have tried the below but it is not working:
output <- paste('summary.pdf')
pdf(output,width = 25,height = 20)
par(mfrow = c(3, 3))
plot(bar.plot)
plot(density.plot)
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)))
invisible(dev.off())
dat.venn is a data of type VennCounts:
I-H-10003-T1-D1 I-H-10003-T2-D1 I-H-10003-T3-D1 Counts
0 0 0 0
0 0 1 41
0 1 0 81
0 1 1 66
1 0 0 10
1 0 1 2
1 1 0 4
1 1 1 56
attr(,"class")
[1] "VennCounts"
I am unable to find a venn diagram package that is compatible with the grid.arrange function. I don't think that VennCounts cant be printed out with grid.arrange function and ggplot2 plots can be printed out with par function.
UPDATE:
I tried using pushViewport but it is still printing the venn diagram on the next page:
pdf(output,width = 25,height = 20)
# Create layout : nrow = 2, ncol = 2
pushViewport(viewport(layout = grid.layout(2, 2)))
# A helper function to define a region on the layout
define_region <- function(row, col){
viewport(layout.pos.row = row, layout.pos.col = col)
}
# Arrange the plots
print(bar.plot, vp = define_region(1, 1:2))
print(density.plot, vp = define_region(2, 1))
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)), vp = define_region(2, 2))
dev.off()
Any help would be much appreciated!
First, save each of the files as .png files, such as save(yourfile, file = "yourname.png").
Second, use the read.PNG function of the grid package to load them, such as yours.png <- readPNG("yourfile.PNG")
After that, convert them with the rasterGrob function as in g1 <- rasterGrob(yours.png, interpolate=TRUE).
Once all the plots are comparable in format, grid.arrange() does the trick. It might look like this:
grid.arrange(g1, g2, g3, nrow=1) # one row
dev.copy(png,'threeplots.png') # to save the array of plots
dev.off() # to close the device
The gridGraphics package, like gridBase, makes the mixing of grid, ggplot, lattice and base graphics fairly straightforward. The grid.echo() function converts a base graphic into a grid graphic. (The advantage of using gridGraphics is that base graphics become amenable to editing and modification via grid; see gridGraphics.pdf.)
In the following, I draw two ggplots and a venn diagram (taken for the vennDiagram help page in the limma package), and I position the three plots within viewports, making sure the venn diagram plot is subjected to the grid.echo treatment.
UPDATE to ggplot2 2.0.0 (Default stat for geom_bar is stat = "count")
library(ggplot2)
library(gridGraphics)
library(grid)
library(limma) # From bioC software repository
grid.newpage()
# Position 1st ggplot
pushViewport(viewport(y = 1, height = 1/3, just = "top"))
gg1 <- ggplot(mtcars, aes(x = mpg)) + geom_density()
print(gg1, newpage = FALSE)
upViewport()
# Position 2nd ggplot
pushViewport(viewport(y = .5, height = 1/3, just = "centre"))
gg2 <- ggplot(mtcars, aes(x = factor(carb, levels = 1:8))) +
geom_bar() + scale_x_discrete(drop = FALSE)
print(gg2, newpage = FALSE)
upViewport()
# Function to draw venn diagram - the venn diagram is taken from ?vennDiagram
plotfun <- function() {
Y <- matrix(rnorm(100*6),100,6)
Y[1:10,3:4] <- Y[1:10,3:4]+3
Y[1:20,5:6] <- Y[1:20,5:6]+3
design <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
fit <- eBayes(lmFit(Y,design))
results <- decideTests(fit)
a <- vennCounts(results)
print(a)
mfrow.old <- par()$mfrow
par(mfrow=c(1,2))
vennDiagram(a)
vennDiagram(results,
include=c("up", "down"),
counts.col=c("red", "blue"),
circle.col = c("red", "blue", "green3"))
}
# Position the venn diagram
pushViewport(viewport(y = 0, height = 1/3, just = "bottom"))
grid.echo(plotfun, newpage = FALSE)
upViewport(0)

How to get equal-sized subplots with different multi-figure layouts in R and grid

I am using the grid package to do a multi-figure graph:
# load libraries
library(grid)
library(ggplot2)
library(gridSVG)
# create some data
p <- ggplot(mtcars, aes(wt, mpg))
# push Viewport and create layout
pushViewport(viewport(layout = grid.layout(nrow = 5, ncol = 4)))
matrixindex = cbind(rep(1:5,each =4), rep(1:4,times=5))
#fill viewport
for (k in 1:20){
print(p+geom_point(),
vp=viewport(layout.pos.row=matrixindex[k,1],layout.pos.col=matrixindex[k,2]))}
# export as SVG
gridToSVG("trial.svg","none","none")
Now, I would like to produce a second figure, with a changed layout (just one row, but again
4 columns). But the individual plots within the figure should have the same size like in the 5x4 layout. How can I achieve this?
You can use the heights and / or widths argument to grid.layout to fix the sizes, eg:
pushViewport(viewport(layout = grid.layout(heights = unit(0.2 , "npc" ) ,nrow = 1, ncol = 4)))
'npc' means normalised parent coordinates, so 0.2 takes 1/5 the viewport.

How do you draw a line across a multiple-figure environment in R?

Take a very simple example, mfrow=c(1,3); each figure is a different histogram; how would I draw a horizontal line (akin to abline(h=10)) that went across all 3 figures? (That is, even the margins between them.) Obviously, I could add an abline to each figure, but that's not what I want. I can think of a very complicated way to do this by really only having 1 figure, and drawing each 'figure' within it using polygon etc. That would be ridiculous. Isn't there an easy way to do this?
As #joran noted, the grid graphical system offers more flexible control over arrangement of multiple plots on a single device.
Here, I first use grconvertY() to query the location of a height of 50 on the y-axis in units of "normalized device coordinates". (i.e. as a proportion of the total height of the plotting device, with 0=bottom, and 1=top). I then use grid functions to: (1) push a viewport that fills the device; and (2) plot a line at the height returned by grconvertY().
## Create three example plots
par(mfrow=c(1,3))
barplot(VADeaths, border = "dark blue")
barplot(VADeaths, border = "yellow")
barplot(VADeaths, border = "green")
## From third plot, get the "normalized device coordinates" of
## a point at a height of 50 on the y-axis.
(Y <- grconvertY(50, "user", "ndc"))
# [1] 0.314248
## Add the horizontal line using grid
library(grid)
pushViewport(viewport())
grid.lines(x = c(0,1), y = Y, gp = gpar(col = "red"))
popViewport()
EDIT: #joran asked how to plot a line that extends from the y-axis of the 1st plot to the edge of the last bar in the 3rd plot. Here are a couple of alternatives:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
# barplot #1
barplot(VADeaths, border = "dark blue")
X1 <- grconvertX(0, "user", "ndc")
# barplot #2
barplot(VADeaths, border = "yellow")
# barplot #3
m <- barplot(VADeaths, border = "green")
X2 <- grconvertX(tail(m, 1) + 0.5, "user", "ndc") # default width of bars = 1
Y <- grconvertY(50, "user", "ndc")
## Horizontal line
pushViewport(viewport())
grid.lines(x = c(X1, X2), y = Y, gp = gpar(col = "red"))
popViewport()
Finally, here's an almost equivalent, and more generally useful approach. It employs the functions grid.move.to() and grid.line.to() demo'd by Paul Murrell in the article linked to in #mdsumner's answer:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
barplot(VADeaths); vps1 <- do.call(vpStack, baseViewports())
barplot(VADeaths)
barplot(VADeaths); vps3 <- do.call(vpStack, baseViewports())
pushViewport(vps1)
Y <- convertY(unit(50,"native"), "npc")
popViewport(3)
grid.move.to(x = unit(0, "npc"), y = Y, vp = vps1)
grid.line.to(x = unit(1, "npc"), y = Y, vp = vps3,
gp = gpar(col = "red"))
This is the best I can do without thinking about it harder:
par(mfrow = c(1,3),xpd = NA)
for (i in 1:3){
x <- rnorm(200,i)
hist(x)
if (i == 1) segments(par("usr")[1],10,30,10)
}
I'm not sure how to make sure the line ends at the right spot without tinkering. Plotting a segment in each region would solve that, but introduce the issue of having the heights line up properly. But this might be a good starting point, at least.
I'd guess this is easier in grid graphics, but I'd have to do some research to verify.
This article by Paul Murrell shows the use of grid graphics to draw lines between two different coordinate systems, in this case lines that have end points specified in the native space of two separate sub-plots:
Paul Murrell. The grid graphics package. R News, 2(2):14-19, June 2002
It's on page 17 of the PDF article:
http://cran.r-project.org/doc/Rnews/Rnews_2002-2.pdf

Resources