I have a data set where I created ggplots
for each ind
where I end up with four different graphs. I had put these graphs into a list, and then used plot_grid
from cowplot
to get a single output that displays all four graphs at once. I'm not quite sure how to create a generic example here.The four graphs have different scales, but I would like my end product to have the same scales for each graph from the plot_grid
output. How would I do this?
Here is a sample data set:
library(ggplot2)
data(iris)
rsq <- lapply(1:length(unique(iris$Species)), function(i) {
cor(iris[iris$Species == unique(iris$Species)[i], "Sepal.Length"], iris[iris$Species == unique(iris$Species)[i], "Petal.Length"])^2
})
p.list <- lapply(1:length(unique(iris$Species)), function(i) {
ggplot(iris[iris$Species == unique(iris$Species)[i], ], aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() + theme_bw()+
geom_text(aes(x=min(Sepal.Length),y=max(Petal.Length),label=paste0("R= ",round(rsq[[i]],2))))
})
cowplot::plot_grid(plotlist = p.list[1:3], nrow = 1, ncol = 3,
labels = "AUTO")