I am getting the feeling after doing some digging that this probably will not work and I will need to discover an alternate method but I am going to ask anyways.
I have to graphs that I want to plot on the same chart, by making use of par(mfrow=c(1,2))
My code for the graphs is as follows:
mTotal <- mean(data$Total)
mTotal
data$valence1[data$Total >= mTotal] <- "Above Mean"
data$valence1[data$Total < mTotal] <- "Below Mean"
data$valence2[data$Delta >= 0] <- "Positive"
data$valence2[data$Delta < 0] <- "Negative"
data
par(mfrow=c(1,2))
ggplot(data,
aes(x = Index,
y = Total,
fill = valence1)) +
geom_bar(stat = "identity",
colour = "black",
alpha = 0.618) +
geom_hline(yintercept = mTotal,
linetype = "dashed",
colour = "red") +
annotate("text", x = 19, y = mTotal + 50,
label = "Problem Period") +
xlab("Date") +
ylab("Ambulance Arrivals") +
ggtitle("Ambulance Arrivals by Month
Jan 2013 - Feb 2014")
maxDelta <- max(data$Delta)
maxDelta
minDelta <- min(data$Delta)
minDelta
ggplot(data,
aes(x = Index,
y = Delta,
fill = valence2)) +
geom_bar(stat = "identity",
position = "identity",
colour = "black",
alpha = 0.618) +
annotate("rect", xmin = 13.5, xmax = 24.5,
ymin = minDelta, ymax = maxDelta,
alpha = 0.3, fill = "blue") +
annotate("text", x = 19, y = maxDelta + 25,
label = "Problem Period") +
xlab("Date") +
ylab("Change in Arrivals") +
ggtitle("Change in Ambulance Arrivals Month over Month")
If this is not possible, then a direction to a better route would be appreciated.
Thank you,
One disadvantage for par() is that it cannot work for ggplot, we can see below that the plot should appear on the upper left of the page, but it just happen as if par() isn't written here.
The par() function allows to set parameters to the plot. The mfrow() parameter allows to split the screen in several panels. Subsequent charts will be drawn in panels. You have to provide a vector of length 2 to mfrow() : number of rows and number of columns.
Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.
Look at the gridExtra
package and use grid.arrange
instead. Works wonderfully with ggplot
.
Just assign your first ggplot
call to a variable (e.g. plot1
) and the next to another (e.g. plot2
) and do something like:
grid.arrange(plot1, plot2, nrow=1, ncol=2)
mfrow
is for use with base graphics. For ggplot2
you need a different approach, like the one mentioned by @hrbmstr, or this one:
library("ggplot2")
library("grid")
a <- qplot(x = rnorm(10))
b <- qplot(x = rnorm(10))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(a, vp = vplayout(1,1))
print(b, vp = vplayout(1,2))
Late to the party, but I just had to deal with this and found a simple solution for multiplots simply by looking in the in the gridExtra
package (builind on @hrbrmstr):
library("ggplot2")
library("gridExtra")
pl <- lapply(1:4, function(.x) qplot(1:10, rnorm(10), main=paste("plot", .x)))
marrangeGrob(pl, nrow=2, ncol=2)
Just put you plot generating function in the lapply
and you're all set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With