ggthemes icon indicating copy to clipboard operation
ggthemes copied to clipboard

theme_excel_new() doesn't display labels (and base size doesnt work as expected for title)

Open Yann-C-INN opened this issue 2 months ago • 0 comments

This was originally a bug report issue but I was able to edit the source code and fix it myself, but I couldnt submit a pull request so I attached what I wrote

Problem:

  • base_size does not work
  • When increasing base_size it actually shrinks the plot, but does not change the text size
  • Also, when edited the source code i changed size argument in element_line() to linewidth as using size is deprecated as of ggplot2 3.4.0 - as the warning states.

Changes I made:

  • From Google, I found the Points to MM coefficient and set it within the function to be 0.352777778. -I then re-coded everywhere where it said element_text(size = 9,...) to be size = base_size and re-coded plot.title() to size = rel(1.5555) which corresponds to the original decision (base size = 9, title = 14).

I did not change axis.title that is set to blank, although I would suggest to not ommit axis labels as a default.

My corrected (and tested) code:

theme_excel_new <- function(base_size=9,
                            base_family = "sans") {
  PT_TO_MM <- 0.352777778
  colorlist <- list(
    lt_gray = "#D9D9D9",
    gray = "#BFBFBF",
    dk_gray = "#595959"
  )
  theme_bw(
    base_family = base_family,
    base_size = base_size
  ) +
    theme(
      text = element_text(
        colour = colorlist$dk_gray,
        size = base_size
      ),
      line = element_line(
        linetype = "solid",
        colour = colorlist$gray
      ),
      rect = element_rect(
        linetype = 0,
        colour = "white"
      ),
      panel.grid.major = element_line(
        linetype = "solid",
        colour = colorlist$gray,
        linewidth = 0.75 * PT_TO_MM
      ),
      panel.grid.minor = element_blank(),
      axis.title = element_blank(),
      axis.text = element_text(
        colour = colorlist$dk_gray,
        size = base_size
      ),
      strip.background = element_rect(
        fill = NA
      ),
      strip.text = element_text(
        colour = colorlist$dk_gray,
        size = base_size
      ),
      axis.ticks = element_blank(),
      panel.background = element_blank(),
      panel.border = element_rect(colour = NA),
      title = element_text(
        face = "plain",
        hjust = 0.5
      ),
      plot.title = element_text(
        hjust = 0.5,
        size = rel(1.555555)
      ),
      plot.subtitle = element_blank(),
      legend.position = "bottom",
      legend.text = element_text(
        size = base_size,
        colour = colorlist$dk_gray
      ),
      legend.title = element_blank(),
    )
}

Yann-C-INN avatar Apr 16 '24 08:04 Yann-C-INN