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

Optimize boxplot right margin #36

Open
logstar opened this issue Sep 17, 2021 · 0 comments
Open

Optimize boxplot right margin #36

logstar opened this issue Sep 17, 2021 · 0 comments

Comments

@logstar
Copy link
Contributor

logstar commented Sep 17, 2021

After rotating boxplot x-axis labels 45 degrees, as suggested by @dunnpa, some right side labels are truncated by default right margin. To include all x-axis labels in the plot, the right margin is set with the following procedure. However, this procedure needs to be optimized for the following two purposes:

  • Guarantee that all x-axis labels are included and not truncated.
  • Guarantee that right margin always extend the rightmost label by 5.5 points, which is the default ggplot2 margin.

# The x-axis labels are long and rotated 45 degrees, so they are out of the
# plot in the default margin. Increase right margin to fit all text.
plot_margin <- ggplot2::theme_get()$plot.margin
if (!identical(length(plot_margin), 4L)) {
plot_margin <- rep(grid::unit(x = 5.5, units = "points"), 4)
}
rightmost_x_label <- dplyr::last(
levels(gene_tpm_boxplot_tbl$x_labels), default = "")
# increase right margin by the width of the last x label * 0.71
plot_margin[2] <- grid::unit(
x = 0.8, units = "strwidth", data = rightmost_x_label)

Example boxplot with truncated x-axis labels:

library(ggplot2)
data(diamonds)
diamonds$cut <- paste("Very very very long x-axis labels like our boxplot",as.character(diamonds$cut))
qplot(cut,carat,data=diamonds,geom="boxplot") +
  theme(axis.text.x = element_text(angle = -45, vjust = 1, hjust = 0))

image

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

No branches or pull requests

1 participant