Increase ggplot after build / view
I am making charts in batch. When viewing graphs, it would be helpful to zoom in on the serval areas. Is there a way to scale / scale the axis after creating the plot and then restore it back to its 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 would like to scale up this drop around x = 0.52 on graph 27
print(gplist[[27]] + coord_cartesian(xlim= c(.5,.6)))
This reproduces a plot with the x-axis zoomed between .5 and .6.
+3
source to share
1 answer
Yes, using coord_cartesian
(or 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 charts
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))
+7
source to share