Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot two Graphs on Same Chart R, ggplot2 par(mfrow())

Tags:

plot

r

ggplot2

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,

like image 392
MCP_infiltrator Avatar asked Apr 24 '14 19:04

MCP_infiltrator


People also ask

Does par () work with Ggplot?

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.

What does par Mfrow do in R?

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.

How do I put two Ggplots on top of each other?

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.


3 Answers

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)
like image 78
hrbrmstr Avatar answered Oct 05 '22 22:10

hrbrmstr


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))
like image 29
Bryan Hanson Avatar answered Oct 05 '22 22:10

Bryan Hanson


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)

Multi-plot with ggplot and grid

Just put you plot generating function in the lapplyand you're all set.

like image 41
victor_v Avatar answered Oct 05 '22 20:10

victor_v