0

This is some data I made. I have two data frames with two variables each.

var1 <- (1:10)*(rnorm(10,2,0.1))
var2 <- (6:15)*(rnorm(10,1,0.1))       
df1 <- as.data.frame(cbind(var1,var2))

var3 <- (1:10)*(rnorm(10,3,0.1))
var4 <- (6:15)*(rnorm(10,1.5,0.1))       
df2 <- as.data.frame(cbind(var3,var4))

There is a loop for plotting the first variable of df1 and df2, and the second of df1 and df2 too.

plot_list = list()  
for(i in 1:ncol(df1)){
        p=ggplot(df1, 
                 aes_string(x=df1[,i], 
                            y=df2[,i]))+
                geom_point()
        plot_list[[i]] = p
}

library(gridExtra)
do.call("grid.arrange", c(plot_list[c(1:2)], ncol=1)) 

And this is the plot I got.

enter image description here

So far so good. But, I would like to x and y within each plot had the same limit based on max and min. For example, in the above plot both x and should go from ~5 to ~30. In the below plot both x and should go from ~6 to ~24. I could set the limits manually, but I need to do this for many plots.

Is there any way to set the x and y limits for each plot based on min and max observed in any of the axis?

Thanks for the help.

Giuseppe Petri
  • 604
  • 4
  • 14
  • 1
    Are `facet_wrap` or `facet_grid` options for you? – markus Feb 21 '19 at 19:02
  • 2
    Possible duplicate: https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots Just calculate the ranges and adjust your scales. These two plots don't have any way to talk to each other or know they are being plotted together so you need to explicitly tell it the range you want to use. – MrFlick Feb 21 '19 at 19:04
  • @markus, Yes, both are options but I don't know how to use those withing the loop. Thanks – Giuseppe Petri Feb 21 '19 at 19:29
  • @MrFlick, I don't need the two plots to talk each other. The same x and y limits need to be set within each plot. I checked the post you mentioned but it seems to me that that's for manually setting the limits. Thanks – Giuseppe Petri Feb 21 '19 at 19:32
  • 1
    @GiuseppePetri You would need to rbind your dataframes. See [Merging a lot of data.frames](https://stackoverflow.com/questions/14096814/merging-a-lot-of-data-frames) Note the column names must be the same. Here is an intro to facets: http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/ – markus Feb 21 '19 at 19:56

1 Answers1

1

In general, I’d suggest that the data for each plot should be in its own data.frame. Having a single data.frame and using facets is an option, but facets make it difficult to specify different limits for each plot. I’ve therefore gone with a grid.arrange solution similar to yours.

library(ggplot2)
library(purrr)
var1 <- (1:10)*(rnorm(10,2,0.1))
var2 <- (6:15)*(rnorm(10,1,0.1))       

var3 <- (1:10)*(rnorm(10,3,0.1))
var4 <- (6:15)*(rnorm(10,1.5,0.1))       

df1 <- data.frame(x = var1, y = var3)
df2 <- data.frame(x = var2, y = var4)

plots <- map(
  list(df1, df2),
  function(data) {
    ggplot(data, aes(x, y)) +
      geom_point() +
      coord_fixed(xlim = range(c(data$x, data$y)), ylim = range(c(data$x, data$y)))
  })

gridExtra::grid.arrange(grobs = plots, nrow = 2)
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52