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

[enhancement] patchwork instead of cowplot #104

Open
Generalized opened this issue May 21, 2021 · 0 comments
Open

[enhancement] patchwork instead of cowplot #104

Generalized opened this issue May 21, 2021 · 0 comments

Comments

@Generalized
Copy link

Generalized commented May 21, 2021

Dear Author of the dabestr,

It's not about any error, but something that might, potentially, facilitate your work.
I noted that some people asked you "how to add a title to the plot", "how to control this-and-that". I admit, I was searching for this too. Once you answered, that the final graph consists of a set of sub-graphs, combined via cowplot (#85)

There is another package, that might be useful for your (awesome!) work, the patchwork:

BTW, it's possible to add title to the plot via cowplot, but it's not very intuitive:

dp stands for dabestr_plot

title <- ggdraw() + 
    draw_label(
        "My title", fontface = 'bold', x = 0, hjust = 0) +
    theme( plot.margin = margin(0, 0, 0, 7)) # the margin aligns title with left edge of the first plot

plot_grid(title, dp,
    ncol = 1, rel_heights = c(0.1, 1) #vertical title margins
)

...with patchwork it's easier. Let me use a fake, simple plot to illustrate (just 2 histograms).

p1 <- ggplot(mtcars) + geom_histogram(aes(x=mpg))
p2 <- ggplot(mtcars) + geom_boxplot(aes(y=hp))

p1 + p2+ plot_annotation(title = 'The title', subtitle = 'and subtitle') # 2 plots + common title

obraz

But that's not all! You can access each component and modify it after the composition:

compound_graph <- p1 + p2+ plot_annotation(title = 'The title', subtitle = 'and subtitle') # 2 plots + common title

# Let's modify the title of the X axis for the 1st graph:
compound_graph[[1]] <- compound_graph[[1]] + xlab("XXXX")

# also, let's modify the boxplot (just by adding another one, overlying the old one) and change its style:
compound_graph[[2]] <- compound_graph[[2]] + geom_boxplot(aes(y=hp), col="red", fill="blue", alpha=0.5) + theme_bw()
compound_graph

obraz

If you want to change the title after all, just do it:

compound_graph + plot_annotation(title = "Naaah, let's change the title :)")

obraz

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