Zoom in ggplot after creating / reviewing

时光怂恿深爱的人放手 提交于 2020-12-31 05:51:26

问题


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.


回答1:


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))


来源:https://stackoverflow.com/questions/26225670/zoom-in-ggplot-after-creating-reviewing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!