Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

superimposing ggbreak plots through ggplotGrob #52

Open
dcousin3 opened this issue Aug 23, 2022 · 2 comments
Open

superimposing ggbreak plots through ggplotGrob #52

dcousin3 opened this issue Aug 23, 2022 · 2 comments

Comments

@dcousin3
Copy link

I often superimpose plots by generating them, all of them with transparent background, then transformed them into grobs and staking the grobs in an empty plot. For example, this works fine:

library(superb) # for makeTransparent()
library(ggplot2)
library(ggbreak)

set.seed(2022-08-23)
d <- data.frame(x = 1:20,
   y = c(rnorm(5) + 4, rnorm(5) + 20, rnorm(5) + 5, rnorm(5) + 22))
 
##########################################################
plt1 <- ggplot(d, aes(y, x)) + geom_col(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20))

plt2 <- ggplot(d, aes(y, x)) + geom_line(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20))

plt1g <- ggplotGrob(print(plt1))
plt2g <- ggplotGrob(print(plt2 + makeTransparent() ))

plt <- ggplot() + 
            annotation_custom(grob=plt1g) + 
            annotation_custom(grob=plt2g) 
plt

However, adding scale_x_break, things seems to fall down, eg.:

plt1 <- ggplot(d, aes(y, x)) + geom_col(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20)) +
    scale_x_break(c(7, 17), scales = 1.5) + scale_x_break(c(18, 21), scales=2)

plt2 <- ggplot(d, aes(y, x)) + geom_line(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20)) +
    scale_x_break(c(7, 17), scales = 1.5) + scale_x_break(c(18, 21), scales=2)

plt1g <- ggplotGrob(print(plt1))
plt2g <- ggplotGrob(print(plt2 + makeTransparent() ))

plt <- ggplot() + 
            annotation_custom(grob=plt1g) + 
            annotation_custom(grob=plt2g) 
plt

Is there a way to make this work with ggbreak? Thanks.

@dcousin3
Copy link
Author

This is the content of makeTransparent():

theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_rect(fill = "transparent", 
            colour = NA), plot.background = element_rect(fill = "transparent", 
            colour = NA))

Is there something else that becomes non-transparent once ggbreak is used?

@dcousin3
Copy link
Author

I don't know if this can help, but I found out that is the default theme is changed permanently, it then works:

# modify the default theme.
old_theme <- theme_get()
theme_update(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_rect(fill = "transparent", colour = NA), 
        plot.background = element_blank()
)

plt1 <- ggplot(d, aes(y, x)) + geom_col(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20)) + old_theme + 
    scale_x_break(c(7, 17), scales = 1.5) + scale_x_break(c(18, 21), scales=2)

plt2 <- ggplot(d, aes(y, x)) + geom_line(orientation="y") + 
    coord_cartesian(xlim=c(0,24), ylim=c(1,20)) +
    scale_x_break(c(7, 17), scales = 1.5) + scale_x_break(c(18, 21), scales=2)

plt1g <- ggplotGrob(print(plt1))
plt2g <- ggplotGrob(print(plt2 + makeTransparent()+  
  theme(panel.background=element_rect(fill = "transparent", colour = NA) )))

plt <- ggplot() + 
            annotation_custom(grob=plt1g) + 
            annotation_custom(grob=plt2g) 
plt
# restore the theme
theme_set(old_theme )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant