Patchwork and combining plots with /

Hi,
I have tripped over a syntax when slash / was used with ggplot code, took me long time to figure it out that / is from patchwork.

Is there an explicit way to write syntax like this:

patchwork::/

This obviously does't work, but maybe anybody knows another way.

Can you give us a bit more background/

We really need a minimum working example to see what you are doing.

See FAQ Asking Questions

And here we are:

library(ggplot2)
library(patchwork)

# Create some example plots
plot1 <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point()
plot2 <- ggplot(data = diamonds, aes(x = carat, y = price)) + geom_point()

# Combine plots using the + operator
combined_plot <- plot1 + plot2

# Display the combined plot
combined_plot


# or use /:

plot1/plot2 

I don't understand the problem. As written everything seems fine. Sorry to be so dense.

Why /: ?

Not /: but only "/". I used : colon in the comment as just a colon.

Now what part don't you understand ? In R code we can write a syntax like dplyr::count(), in long and complicated code I did not spot / as a connector for plots originated from patchwork, so my question was: what is so different in patchwork or syntax that I can't write it as follows:

patchwork::/  

in order to know that / is not there as a math division symbol but function coming specifically from patchwork ?

Ah, okay. I see what you mean now. We probably need an R expert for this but my guess is that / is some kind of "primitive" in base R that is interpreted in context just as + is. It is not a function in {patchwork}. Does that make sense?

You can define your own infix operator like this:

library(ggplot2)

`%/%` <- patchwork:::`/.ggplot`

p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()

p1 %/% p2

If you don't want to define a new operator, you can also call it directly on the plots:

(patchwork:::`/.ggplot`)(p1, p2)

I find both ways kind of ugly but that's essentially what you wanted.

Thank you very much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.