4

I'm making plots in batch mode. While reviewing the graphs, it would be useful to zoom in on serval areas of interest. Is there a way to zoom / rescale axis after the plot is made, and then restore it back to original axis range?

Answer, after incorporating feedback and comments....

set.seed(5)
gplist<-list()
for (i in seq(1,29)) {
  mod_evt = paste("plot",i)
  df <- data.frame(x=runif(10), y=runif(10))
  gp <- ggplot(df,aes(x=x,y=y)) + geom_line() + geom_point() +
    labs(title = mod_evt, x="X", y="Y") 
  print(gp)
  gplist[[i]] <- gp
}

I'd like to zoom in on that dip near x=0.52 in plot 27

print(gplist[[27]] +  coord_cartesian(xlim= c(.5,.6)))

This reproduces the plot with x axis zoomed in between .5 and .6.

  • If you save the ggplot objects (and not just the plots), yes. You can reload them from an R data file and change any of the `scale_` parameters. – hrbrmstr Oct 06 '14 at 22:15
  • ok, I'll do that... Thanks. –  Oct 06 '14 at 22:15
  • 1. Better form is to do `gplist[[i]] <- gp` instead of `gplist <- c(gplist, gp)`. – Gregor Thomas Oct 06 '14 at 22:52
  • 2. Related, use `[[` for accessing single list elements (`[` gives you a list back`). Thus your last line should be `gplist[[27]] + ...`. [See here](http://stackoverflow.com/q/1169456/903061) for details. – Gregor Thomas Oct 06 '14 at 22:53
  • ok, works after making above changes –  Oct 06 '14 at 23:12

1 Answers1

9

Yes, using coord_cartesian (or the appropriate coord_xxxx)

ex <- ggplot(mtcars, aes(x=mpg,y=drat, colour=factor(cyl))) + geom_point()

ex

enter image description here

# plot with "zoomed region"
ex + coord_cartesian(xlim = c(10,25),ylim= c(3,5))

enter image description here

# the original still exists
ex

enter image description here

If you have a list of plots

 plot_list <- list(ggplot(mtcars, aes(x=mpg,y=drat, colour=factor(cyl))) + geom_point(),
                   ggplot(mtcars, aes(x=mpg,y=drat, colour=factor(am))) + geom_point())
 zoomed <- lapply(plot_list, function(p) p + coord_cartesian(xlim= c(15,30)))


 # or for a single plot
 plot_list[[1]] + coord_cartesian(xlim= c(15,30))
mnel
  • 113,303
  • 27
  • 265
  • 254
  • I have the plots generated in a loop. What type of variable can I use to store the plots by index, a vector? –  Oct 06 '14 at 22:20
  • 2
    store the plots in a list. – Ben Bolker Oct 06 '14 at 22:22
  • I did this... gplist=list() for (i in seq(1,29) { ... make plot print(gp) gplist <- c(gplist,gp) –  Oct 06 '14 at 22:42
  • I updated the question to include your solution, but I get an error, see above.... –  Oct 06 '14 at 22:51
  • @user, your error comes from indexing the list using `[` not `[[`. `[` returns a list (as you can return one or more elements), `[[` can only return one element, and will return the element, not the list containing the element – mnel Oct 06 '14 at 22:55
  • I changed the access to [[]], but still get the same error. –  Oct 06 '14 at 23:07
  • Also had to change gplist <- c(gplist,gp) to gplist[[i]] = gp. Thanks for your patience. –  Oct 06 '14 at 23:16