This function visualises percentages, but avoids a clear cut for the sample point estimate, instead using the confidence (as in confidence interval) to create a gradient. This effectively hinders drawing conclusions on the basis of point estimates, thereby urging a level of caution that is consistent with what the data allows.

ggProportionPlot(dat, items = NULL,
                 loCategory = NULL,
                 hiCategory = NULL,
                 subQuestions = NULL,
                 leftAnchors = NULL,
                 rightAnchors = NULL,
                 compareHiToLo = TRUE,
                 showDiamonds = FALSE,
                 diamonds.conf.level=.95,
                 diamonds.alpha=1,
                 na.rm = TRUE,
                 barHeight = 0.4,
                 conf.steps = seq(from=0.001, to=.999, by=.001),
                 scale_color = viridis(option="magma", 2, begin=0, end=.5),
                 scale_fill = viridis(option="magma", 2, begin=0, end=.5),
                 linetype=1,
                 theme = theme_bw(),
                 returnPlotOnly = TRUE)

Arguments

dat

The dataframe containing the items (variables), or a vector.

items

The names of the items (variables). If none are specified, all variables in the dataframe are used.

loCategory

The value of the low category (usually 0). If not provided, the minimum value is used.

hiCategory

The value of the high category (usually 1). If not provided, the maximum value is used.

subQuestions

The labels to use for the variables (for example, different questions). The variable names are used if these aren't provided.

leftAnchors

The labels for the low categories. The values are used if these aren't provided.

rightAnchors

The labels for the high categories. The values are used if these aren't provided.

compareHiToLo

Whether to compare the percentage of low category values to the total of the low category values and the high category values, or whether to ignore the high category values and compute the percentage of low category values relative to all cases. This can be useful when a variable has more than two values, and you only want to know/plot the percentage relative to the total number of cases.

showDiamonds

Whether to add diamonds to illustrate the confidence intervals.

diamonds.conf.level

The confidence level of the diamonds' confidence intervals.

diamonds.alpha

The alpha channel (i.e. transparency, or rather 'obliqueness') of the diamonds.

na.rm

Whether to remove missing values.

barHeight

The height of the bars, or rather, half the height. Use .5 to completely fill the space.

conf.steps

The number of steps to use to generate the confidence levels for the proportion.

scale_color, scale_fill

A vector with two values (valid colors), that are used for the colors (stroke) and fill for the gradient; both vectors should normally be the same, but if you feel adventurous, you can play around with the number of conf.steps and this. If you specify only one color, no gradient is used but a single color (i.e. specifying the same single color for both scale_color and scale_fill simply draws bars of that color).

linetype

The linetype to use (0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash).

theme

The theme to use.

returnPlotOnly

Whether to only return the ggplot2 plot or the full object including intermediate values and objects.

Details

This function used confIntProp to compute confidence intervals for proportions at different levels of confidence. The confidence interval bounds at those levels of confidence are then used to draw rectangles with colors in a gradient that corresponds to the confidence level.

Note that percentually, the gradient may not look continuous because at the borders between lighter and darker rectangles, the shade of the lighter rectangle is perceived as even lighter than it is, and the shade of the darker rectangle is perceived as even darker. This makes it seem as if each rectange is coloured with a gradient in the opposite direction.

Value

A ggplot2 object (if returnPlotOnly is TRUE), or an object containing that ggplot2 object and intermediate products.

See also

confIntProp and binom.test

Examples

### V/S (no idea what this is: ?mtcars only mentions 'V/S' :-)) ### and transmission (automatic vs manual) ggProportionPlot(mtcars, items=c('vs', 'am'));
#> TableGrob (10 x 8) "layout": 18 grobs #> z cells name grob #> 1 0 ( 1-10, 2- 8) background rect[plot.background..rect.4151] #> 2 5 ( 5- 5, 4- 4) spacer zeroGrob[NULL] #> 3 7 ( 6- 6, 4- 4) axis-l absoluteGrob[GRID.absoluteGrob.4139] #> 4 3 ( 7- 7, 4- 4) spacer zeroGrob[NULL] #> 5 6 ( 5- 5, 5- 5) axis-t zeroGrob[NULL] #> 6 1 ( 6- 6, 5- 5) panel gTree[panel-1.gTree.4122] #> 7 9 ( 7- 7, 5- 5) axis-b absoluteGrob[GRID.absoluteGrob.4132] #> 8 4 ( 5- 5, 6- 6) spacer zeroGrob[NULL] #> 9 8 ( 6- 6, 6- 6) axis-r absoluteGrob[GRID.absoluteGrob.4146] #> 10 2 ( 7- 7, 6- 6) spacer zeroGrob[NULL] #> 11 10 ( 4- 4, 5- 5) xlab-t zeroGrob[NULL] #> 12 11 ( 8- 8, 5- 5) xlab-b titleGrob[axis.title.x..titleGrob.4125] #> 13 12 ( 6- 6, 3- 3) ylab-l zeroGrob[NULL] #> 14 13 ( 6- 6, 7- 7) ylab-r zeroGrob[NULL] #> 15 14 ( 3- 3, 5- 5) subtitle zeroGrob[plot.subtitle..zeroGrob.4148] #> 16 15 ( 2- 2, 5- 5) title zeroGrob[plot.title..zeroGrob.4147] #> 17 16 ( 9- 9, 5- 5) caption zeroGrob[plot.caption..zeroGrob.4149] #> 18 17 ( 6- 6, 1- 1) subquestions gtable[layout]
### Number of cylinders, by default comparing lowest value ### (4) to highest (8): ggProportionPlot(mtcars, items=c('cyl'));
#> TableGrob (10 x 8) "layout": 18 grobs #> z cells name grob #> 1 0 ( 1-10, 2- 8) background rect[plot.background..rect.4239] #> 2 5 ( 5- 5, 4- 4) spacer zeroGrob[NULL] #> 3 7 ( 6- 6, 4- 4) axis-l absoluteGrob[GRID.absoluteGrob.4227] #> 4 3 ( 7- 7, 4- 4) spacer zeroGrob[NULL] #> 5 6 ( 5- 5, 5- 5) axis-t zeroGrob[NULL] #> 6 1 ( 6- 6, 5- 5) panel gTree[panel-1.gTree.4210] #> 7 9 ( 7- 7, 5- 5) axis-b absoluteGrob[GRID.absoluteGrob.4220] #> 8 4 ( 5- 5, 6- 6) spacer zeroGrob[NULL] #> 9 8 ( 6- 6, 6- 6) axis-r absoluteGrob[GRID.absoluteGrob.4234] #> 10 2 ( 7- 7, 6- 6) spacer zeroGrob[NULL] #> 11 10 ( 4- 4, 5- 5) xlab-t zeroGrob[NULL] #> 12 11 ( 8- 8, 5- 5) xlab-b titleGrob[axis.title.x..titleGrob.4213] #> 13 12 ( 6- 6, 3- 3) ylab-l zeroGrob[NULL] #> 14 13 ( 6- 6, 7- 7) ylab-r zeroGrob[NULL] #> 15 14 ( 3- 3, 5- 5) subtitle zeroGrob[plot.subtitle..zeroGrob.4236] #> 16 15 ( 2- 2, 5- 5) title zeroGrob[plot.title..zeroGrob.4235] #> 17 16 ( 9- 9, 5- 5) caption zeroGrob[plot.caption..zeroGrob.4237] #> 18 17 ( 6- 6, 1- 1) subquestions gtable[layout]
# NOT RUN { ### Not running these to save time during package building/checking ### We can also compare 4 to 6: ggProportionPlot(mtcars, items=c('cyl'), hiCategory=6); ### Now compared to total records, instead of to ### highest value (hiCategory is ignored then) ggProportionPlot(mtcars, items=c('cyl'), compareHiToLo=FALSE); ### And for 6 cylinders: ggProportionPlot(mtcars, items=c('cyl'), loCategory=6, compareHiToLo=FALSE); ### And for 8 cylinders: ggProportionPlot(mtcars, items=c('cyl'), loCategory=8, compareHiToLo=FALSE); ### And for 8 cylinders with different labels ggProportionPlot(mtcars, items=c('cyl'), loCategory=8, subQuestions='Cylinders', leftAnchors="Eight", rightAnchors="Four\nor\nsix", compareHiToLo=FALSE); ### ... And showing the diamonds for the confidence intervals ggProportionPlot(mtcars, items=c('cyl'), loCategory=8, subQuestions='Cylinders', leftAnchors="Eight", rightAnchors="Four\nor\nsix", compareHiToLo=FALSE, showDiamonds=TRUE); # }
### Using less steps for the confidence levels and changing ### the fill colours ggProportionPlot(mtcars, items=c('vs', 'am'), showDiamonds = TRUE, scale_fill = c("#B63679FF", "#FCFDBFFF"), conf.steps=seq(from=0.0001, to=.9999, by=.2));
#> TableGrob (10 x 8) "layout": 18 grobs #> z cells name grob #> 1 0 ( 1-10, 2- 8) background rect[plot.background..rect.4335] #> 2 5 ( 5- 5, 4- 4) spacer zeroGrob[NULL] #> 3 7 ( 6- 6, 4- 4) axis-l absoluteGrob[GRID.absoluteGrob.4323] #> 4 3 ( 7- 7, 4- 4) spacer zeroGrob[NULL] #> 5 6 ( 5- 5, 5- 5) axis-t zeroGrob[NULL] #> 6 1 ( 6- 6, 5- 5) panel gTree[panel-1.gTree.4306] #> 7 9 ( 7- 7, 5- 5) axis-b absoluteGrob[GRID.absoluteGrob.4316] #> 8 4 ( 5- 5, 6- 6) spacer zeroGrob[NULL] #> 9 8 ( 6- 6, 6- 6) axis-r absoluteGrob[GRID.absoluteGrob.4330] #> 10 2 ( 7- 7, 6- 6) spacer zeroGrob[NULL] #> 11 10 ( 4- 4, 5- 5) xlab-t zeroGrob[NULL] #> 12 11 ( 8- 8, 5- 5) xlab-b titleGrob[axis.title.x..titleGrob.4309] #> 13 12 ( 6- 6, 3- 3) ylab-l zeroGrob[NULL] #> 14 13 ( 6- 6, 7- 7) ylab-r zeroGrob[NULL] #> 15 14 ( 3- 3, 5- 5) subtitle zeroGrob[plot.subtitle..zeroGrob.4332] #> 16 15 ( 2- 2, 5- 5) title zeroGrob[plot.title..zeroGrob.4331] #> 17 16 ( 9- 9, 5- 5) caption zeroGrob[plot.caption..zeroGrob.4333] #> 18 17 ( 6- 6, 1- 1) subquestions gtable[layout]