创建/查看后放大ggplot [英] Zoom in ggplot after creating / reviewing

查看:35
本文介绍了创建/查看后放大ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以批处理模式制作绘图.在查看图形时,放大关注的几个区域会很有用.有没有办法在绘图完成后缩放/重新缩放轴,然后将其恢复到原始轴范围?

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
}

我想放大图 27 中 x=0.52 附近的下降

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

这再现了 x 轴在 0.5 到 0.6 之间放大的绘图.

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

推荐答案

是的,使用coord_cartesian(或适当的coord_xxxx)

Yes, using coord_cartesian (or the appropriate coord_xxxx)

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

ex

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

# the original still exists
ex

如果你有一个情节列表

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

这篇关于创建/查看后放大ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆