ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
38 stars 11 forks source link

Please remove or change the way you set default alpha in histograms/density plots #154

Closed adamblake closed 2 years ago

adamblake commented 2 years ago

Hi, love this package, we use it extensively over at CourseKata.org. Lately, we have been working on a CourseKata theme for ggplot2 plots (and ggformula, naturally) and the way we set defaults for geoms is to use ggplot2::update_geom_defaults() (we have some functions called load_coursekata_themes() and restore_default_themes() that update the defaults).

Using this method, we can set up colorblind and contrast-friendly colors for all of our plots. This works for every geom except the histograms and density plots when we run them through ggformula. It appears that this is because you set the default transparency as a default for the alpha parameter in the function signature. When we try to update the geom defaults (we prefer the more opaque .7), your alpha parameter overrides our defaults to .5

Would you consider changing the way you set this default, or possibly remove it? It seems somewhat out-of-place to have this set in this package as the rest of the package seems to mostly just wrap ggplot2, but maybe I have misinterpreted something.

Thanks for your consideration!

rpruim commented 2 years ago

This looks like it will be pretty easy to do. My plan is to create two new functions: use_ggformula_defaults() and revert_ggformula_defaults(). The former will be called when the package loads so the change won't be noticeable for most users. revert_ggformula_defaults() currently has hard-coded defaults from ggplot2, so it isn't a proper reversion if other things have been done.

One note: You can't actually set the defaults for histograms since there is no histogram geom or stat. Instead geom_histogram() is a wrapper around a call to layer() with StatBin and GeomBar. So the defaults need to be set for GeomBar and will affect any plot that uses bars, not just histograms. That probably isn't a problem, but it will be a small change in behavior.

rpruim commented 2 years ago
suppressPackageStartupMessages(library(ggformula))
revert_ggformula_defaults()
gf_histogram(~rnorm(100), color = "black")

use_ggformula_defaults()
gf_histogram(~rnorm(100), color = "black")

Created on 2022-07-18 by the reprex package (v2.0.1)

rpruim commented 2 years ago

While I'm at it, I think I'll set the ggformula default fill to NA for boxplots. Students often have issues layering boxplots because of the ggplot2's default opaque fill.

suppressPackageStartupMessages(library(ggformula))
revert_ggformula_defaults()
mosaicData::HELPrct |>
  gf_sina(age ~ substance) |>
  gf_boxplot()

use_ggformula_defaults()
mosaicData::HELPrct |>
  gf_sina(age ~ substance) |>
  gf_boxplot()

Created on 2022-07-18 by the reprex package (v2.0.1)

rpruim commented 2 years ago

Demo of alpha for boxplots:

suppressPackageStartupMessages(library(ggformula))
revert_ggformula_defaults()
mosaicData::HELPrct |>
  gf_sina(age ~ substance, color = ~ substance, fill = ~ substance) |>
  gf_boxplot()

use_ggformula_defaults()
mosaicData::HELPrct |>
  gf_sina(age ~ substance, color = ~ substance, fill = ~ substance) |>
  gf_boxplot()

Created on 2022-07-18 by the reprex package (v2.0.1)

adamblake commented 2 years ago

This looks fantastic @rpruim

rpruim commented 2 years ago

@adamblake , this should be heading to CRAN soon. {mosaic} and {mosaicCalc} were updated last week, so now I'm ready to do the testing and other prep to get this package onto CRAN.

adamblake commented 2 years ago

Looking forward to trying it out! Thanks!