问题
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
# plot with "zoomed region"
ex + coord_cartesian(xlim = c(10,25),ylim= c(3,5))
# the original still exists
ex
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