Closed ibecav closed 5 years ago
Completely agree with this! The plan is actually to write a more general version of the function ggpiestats
and call it ggcatstats
(for cat
egorical data) and provide a plot.type
argument that can work with "pie"
, "bar"
, "slopegraph"
, and "alluvial"
plots.
This is something that should happen by 1.0.0
, so still a few months away. But I can already start adding some basic functionality/setup needed for this.
Understand and as long as you're thinking about a plot.type
argument please consider mosaic
using ggmosaic. If you're unfamiliar it's like a barchart only the x axis factors are proportional...
library(ggplot2)
library(dplyr)
library(ggstatsplot)
library(ggmosaic)
ddd <- group_by(Titanic_full, Class, Sex, Age, Survived) %>% count()
ggplot(data=ddd) +
geom_mosaic(aes(weight=n, x=product(Class), fill=Survived)) +
ggtitle("test", subtitle = ggstatsplot::subtitle_contigency_tab(Titanic_full,Class,Survived))
#> Note: 95% CI for Cramer's V was computed with 25 bootstrap samples.
#>
Created on 2018-10-30 by the reprex package (v0.2.1)
I'm about 65% done with this... have not created a pull request yet but working.
Hmm, but the plan was not to create a new function just for the bar plots, but rather to create a general function ggcatstats
(for categorical data) that has plot.type
argument and deprecate ggpiestats
. The problem with writing a separate function for each type of plot is that there is a lot of duplication of code across plotting functions. This means each modification will simultaneously need to be made to all functions since only the plotting portion of the function will change across functions.
Yes I remember the concept of minimizing the number of new functions. Problem is three fold:
ggcatstats
would be a misnomer since bars and mosaics deal nicely with ordinals and other levels of variables.Anyway I will certainly endeavor to minimize the amount of new or overlapping code. When I get to a good stable point (soon) I'll formally submit a PR and you can take a look.
Fair enough!
Let's proceed with separate functions and we can later figure out how to refactor them to avoid code duplication.
Getting there. An example using one of the common datasets.
library(ggstatsplot)
# using the current ggpiestats
# pies are especially inefficent when you have many levels of a factor
ggpiestats(movies_long,
mpaa,
genre,
bf.message = TRUE,
sampling.plan = "jointMulti",
title = "MPAA Ratings by Genre",
caption = "As of January 23, 2019",
nboot = 5,
perc.k = 1,
facet.proptest = FALSE,
palette = "Set2")
#> Note: 95% CI for effect size estimate was computed with 5 bootstrap samples.
#>
# using nthe still nascent ggbarstats
ggbarstats(movies_long,
mpaa,
genre,
bf.message = TRUE,
sampling.plan = "jointMulti",
title = "MPAA Ratings by Genre",
caption = "As of January 23, 2019",
nboot = 5,
perc.k = 1,
facet.proptest = FALSE,
palette = "Set2")
#> Note: 95% CI for effect size estimate was computed with 5 bootstrap samples.
#>
Created on 2019-01-23 by the reprex package (v0.2.1)
Thanks, Chuck. This looks awesome!
In case you already don't have these changes in mind, here are some minor comments to have aeshetic consistency across the plots from these two similar functions-
n =
(in ggpiestats
) and not N=
(in ggbarstats
). Also, they should not be bold (in ggbarstats
).y
-axis label should be "percent"
or "percentage"
(or even "proportion"
, like in gghistostats
)? I don't feel strongly either way. ggpiestats
) and not on the right hand side (in ggbarstats
). Laugh out loud. We have such different aesthetic tastes. But okay using your numbering system
n
instead of N
but I am worried that on wider plots the spaces around the equals sign take up a lot of room.geom_label
instead of geom_text
. But since I detest the white background I'll allow the user to select color
and alpha
Should be able to do these and cleanup tomorrow.
Okay all these are fixed as well as adding a few features. PR follows. Need some more doco cleanup and some testing but it's done.
library(jmv)
#>
#> Attaching package: 'jmv'
#> The following object is masked from 'package:stats':
#>
#> anova
library(ggstatsplot)
# for reproducibility
set.seed(123)
# simple function call with the defaults (with condition)
ggstatsplot::ggbarstats(
data = datasets::mtcars,
main = vs,
condition = cyl,
bf.message = TRUE,
nboot = 10,
factor.levels = c("0 = V-shaped", "1 = straight"),
legend.title = "Engine"
)
#> Warning in stats::chisq.test(x = data$main, y = data$condition, correct =
#> FALSE, : Chi-squared approximation may be incorrect
#> Note: 95% CI for effect size estimate was computed with 10 bootstrap samples.
#>
# simple function call with count data
ggstatsplot::ggbarstats(
data = as.data.frame(HairEyeColor),
main = Eye,
condition = Hair,
counts = Freq
)
#> Note: 95% CI for effect size estimate was computed with 25 bootstrap samples.
#>
ggbarstats(movies_long,
mpaa,
genre,
bf.message = TRUE,
sampling.plan = "jointMulti",
title = "MPAA Ratings by Genre",
caption = "As of January 23, 2019",
nboot = 5,
perc.k = 1,
x.axis.orientation = "slant",
facet.proptest = FALSE,
ggplot.component = ggplot2::theme(axis.text.x = ggplot2::element_text( face = "italic")),
palette = "Set2"
)
#> Note: 95% CI for effect size estimate was computed with 5 bootstrap samples.
#>
Created on 2019-01-24 by the reprex package (v0.2.1)
Thanks, Chuck. This all looks good to me. I will make minor modifications after the PR is merged-
(
to sample sizes to be consistent with ggpiestats
. size
of the sample size label text seems a bit small compared to the rest of the label text in the plot. I'll toy around with different values to see if a bit bigger text size looks better.#' @author Chuck Powell
-> #' @author Chuck Powell, Indrajeet Patil
) since the entire non-plotting related body of the function comes from ggpiestats
which I had written.Would also like to add grouped_ggbarstats
and tests for this function or do you want me to take care of it?
I am fine either way.
Here is what the output looks like with the modifications I have introduced to make this aesthetically as close to ggpiestats
as possible. Lemme know what you think before I push these changes to master.
Reproducing the same examples you used above-
set.seed(123)
ggstatsplot::ggbarstats(
data = datasets::mtcars,
main = vs,
condition = cyl,
bf.message = TRUE,
nboot = 10,
factor.levels = c("0 = V-shaped", "1 = straight"),
legend.title = "Engine"
)
#> Note: Results from one-sample proportion tests for each
#> level of the condition variable testing for equal
#> proportions of the main variable.
#>
#> # A tibble: 3 x 7
#> condition `0` `1` `Chi-squared` df `p-value` significance
#> <fct> <chr> <chr> <dbl> <dbl> <dbl> <chr>
#> 1 4 9.09% 90.91% 7.36 1 0.007 **
#> 2 6 42.86% 57.14% 0.143 1 0.705 ns
#> 3 8 100.00% 0.00% 14 1 0 ***
#> Warning in stats::chisq.test(x = data$main, y = data$condition, correct =
#> FALSE, : Chi-squared approximation may be incorrect
#> Note: 95% CI for effect size estimate was computed with 10 bootstrap samples.
#>
set.seed(123)
ggstatsplot::ggbarstats( data = as.data.frame(HairEyeColor), main = Eye, condition = Hair, counts = Freq )
Chi-squared
df p-value
![](https://i.imgur.com/0AIY71i.png)
- Example 3
``` r
set.seed(123)
ggstatsplot::ggbarstats(
ggstatsplot::movies_long,
mpaa,
genre,
bf.message = TRUE,
sampling.plan = "jointMulti",
title = "MPAA Ratings by Genre",
caption = "As of January 23, 2019",
nboot = 5,
perc.k = 1,
x.axis.orientation = "slant",
facet.proptest = FALSE,
ggplot.component = ggplot2::theme(axis.text.x = ggplot2::element_text(face = "italic")),
palette = "Set2"
)
#> Note: 95% CI for effect size estimate was computed with 5 bootstrap samples.
#>
Created on 2019-01-24 by the reprex package (v0.2.1)
So the biggest difference I can see visually is that you want the significance testing results from the proportions test up top rather than down below? I can certainly live with that.
Hard for me to see anything else you've done.
Yes, additional changes that are not conspicuous-
grey
to black
And that's about it.
If you are okay with these changes, I will push these changes to master and close this issue.
Let's see there's this usual issue that I know you have a method for...
checking R code for possible problems (11.8s) ggbarstats: no visible binding for global variable āNā Undefined global functions or variables: N
The minor grid fix on y is very nice I had removed the x grid
Can we make the outline color selecatable. I don't care about the default
Ditto sample size. I don't much care about the defaults as long as I can adjust
@ibecav I have added tests for this function.
Do you also want to make a PR for the grouped_
version of this function?
Yes I will happily do so. I've been thinking about the other issue (how to consolidate top level functions effectively) as well. One strategy that comes to my mind is to group more by the number of variables involved (univariate, bivariate & multivariate) and less by plot type. For example in my mind pie charts are more like histograms and belong in that grouping whereas chi square association tests are bivariate and not much different in many ways than ggbetweenstats.
Just early thinking. More when I have a chance to think it out.
So I find piecharts very appealing for looking at univariate situations, although there are many who dislike them even when you add labels as you have. But as soon as you move to bivariate cases especially when one of the variables has multiple factor levels, I (and my students more so) have a hard time interpreting multiple pie charts depicting the relationship between two variables.
So looking at the usual Titanic example currently I can imagine a function that is very similiar in nature but shifts to percentage bars with labels.
You'll notice all I have really done is take hunks of your current code and change the call to
ggplot
in one small way...Created on 2018-10-26 by the reprex package (v0.2.1) Thanks for considering