Combine Multiple Plots, Generated Using the "By" R Function, in One Figure

How do I combine multiple plots in one graph?

The easiest way to combine multiple ggplot-based plots is with the patchwork package:

library(patchwork)
plot1 + plot2 + plot3 + plot4

How to combine 4 pairs plots in one single figure?

Update, 12014-07-31 11:48:35Z

As ilir pointed out below pairs somehow overwrites par, most likely for some good reason.

@user44037, can you solve your problem working form this code snippet? Copy/pasted from here. I believe the solution can be found using splom from lattice. take a look at ?splom.

 library(lattice) 
splom(~iris[1:3]|Species, data = iris,
layout=c(2,2), pscales = 0,
varnames = c("Sepal\nLength", "Sepal\nWidth", "Petal\nLength"),
page = function(...) {
ltext(x = seq(.6, .8, len = 4),
y = seq(.9, .6, len = 4),
lab = c("@user44037,", "can you solve your", "problem working form ", "this code snippet?"),
cex = 1)
})

Sample Image

Initial answer, 12014-07-31 11:35:33Z

Simply following Avinash directions by copy/pasting code from the website Quick-R. Feel free to improve on this example.

I'm happy to troubleshoot your specific problem if you provide a reproducible example.

# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")

Sample Image

Store multiple plots into a list using a function

I don't have your data, so I'll simplify the plotting mechanism.

The first problem is that you should not use your [[i]] referencing in your function. Just have your function deal with data as-is, it really doesn't know that its argument is (in another environment) an element with a list. It knows just the object itself.

# a simple plot function
myfunc <- function(x) ggplot(x, aes_string(names(x)[1], names(x)[2])) + geom_point()
# a list of frames, nothing fancy here
datalist <- replicate(3, mtcars, simplify = FALSE)
# just call it ...
myplots <- lapply(datalist, myfunc)
class(myplots[[1]])
# [1] "gg" "ggplot"

When myfunc is called, its argument x is just a data.frame, the function has no idea that x is the first (or second or third) frame in a list of frames.

If you want to include the nth frame with an index indicating which element it is, this is in my view "zipping" data together, so I suggest Map. (You can also use purrr::imap or related tidyverse functions.)

myfunc2 <- function(x, title = "") ggplot(x, aes_string(names(x)[1], names(x)[2])) + geom_point() + labs(title = title)
myplots <- Map(myfunc2, datalist, sprintf("Plot number %s", seq_along(datalist)))
class(myplots[[1]])
# [1] "gg" "ggplot"

To understand how Map relates to lapply, then understand that lapply(datalist, myfunc) is "unrolled" to something like:

myfunc(datalist[[1]])
myfunc(datalist[[2]])
myfunc(datalist[[3]])

With Map, however, it takes one function that must accept one or more arguments in each call. With that, Map accepts as many lists (or vectors) as the function accepts arguments. The two functions are synonomously

lapply(datalist, myfunc) # data first,     function second
Map(myfunc, datalist) # function first, data second

and a more complicated call unrolls like thus:

titles <- sprintf("Plot number %d", seq_along(datalist)) # "Plot number 1", ...
Map(myfunc2, datalist, titles)
# equivalent to
myfunc2(datalist[[1]], titles[[1]])
myfunc2(datalist[[2]], titles[[2]])
myfunc2(datalist[[3]], titles[[3]])

It doesn't really matter if each of the arguments is a true list (as in datalist) or a vector (as in titles), as long as they are the same length (or length 1).

Plot two graphs in same plot in R

lines() or points() will add to the existing graph, but will not create a new window. So you'd need to do

plot(x,y1,type="l",col="red")
lines(x,y2,col="green")

How to combine 4 png graphs into a single one in R?

Are you just looking for par(mfrow = c(2, 2)) ?

png("multiplot.png", width = 640, height = 640)
par(mfrow = c(2, 2))
for(i in 1:4) plot(rnorm(50), rnorm(50), col = i,
xlab = "x", ylab = "y", main = paste("plot", i))
dev.off()

Which results in:

multiplot.png

Sample Image

You simply open the png device, set it's graphical parameters with par(mfrow = c(2, 2)) and then each of the next four plots you make are put on the same image. After four plots are drawn, call dev.off() to save the image.

How to combine two graphs created by autoplot?

Using e.g. patchwork this could be achieved like so:

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
library(quantmod)

library(ggplot2)
getSymbols("AAPL", src = "yahoo", from = start, to = end)

#> [1] "AAPL"
getSymbols("GOLD", src = "yahoo", from = start, to = end)
#> [1] "GOLD"

library(patchwork)

p1 <- autoplot(Cl(AAPL))
p2 <- autoplot(Cl(GOLD))
p1 + p2

Sample Image

EDIT Following the example in docs of zoo::autopilot.zoo you could make your plot manually using ggplot2 like so:

ggplot(mapping = aes(x = Index, y = Value)) +
geom_line(data = fortify(Cl(AAPL), melt = TRUE), aes(color = "AAPL")) +
geom_line(data = fortify(Cl(GOLD), melt = TRUE), aes(color = "GOLD")) +
xlab("Index") + ylab("x")

Sample Image

multiple plots in one RStudio

You can plot multiple scatterplots in the same window using the par() function.

par(mfrow=c(2,2))
plot(top20NA, col ="Blue", ylab="Sales", xlab="Ranking")
plot(top20EU, col = "Black", ylab="Sales", xlab="Ranking")
plot(top20JP, col = "Yellow", ylab="Sales", xlab="Ranking")
plot(top20other, col = "Green", ylab="Sales", xlab="Ranking")

If you want to plot all of your series on the same plot, you can use the lines() and points() functions.

plot(top20NA, ylim = c(0,15), col = "Blue", type = "b",
ylab="Sales", xlab="Ranking")
points(top20EU, col = "Black")
lines(top20EU, col = "Black")
points(top20JP, col = "Yellow")
lines(top20JP, col = "Yellow")
points(top20other, col = "Green")
lines(top20other, col = "Green")

Admittedly, this is a bit clunky in base R, but it does get the job done.



Related Topics



Leave a reply



Submit