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 = 0.95,
  diamonds.alpha = 1,
  na.rm = TRUE,
  barHeight = 0.4,
  conf.steps = seq(from = 0.001, to = 0.999, by = 0.001),
  scale_color = c("#21908CFF", "#FDE725FF"),
  scale_fill = c("#21908CFF", "#FDE725FF"),
  rank.conf = FALSE,
  linetype = 1,
  theme = ggplot2::theme_bw(),
  returnPlotOnly = TRUE
)

# S3 method for ggProportionPlot
print(x, ...)

# S3 method for ggProportionPlot
grid.draw(x, ...)

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).

rank.conf

Whether to let the fill and color gradients use the confidence or the ranked confidence.

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.

x

The object to print/plot.

...

Any additional arguments are passed on to print and grid.draw.

Value

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

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.

Author

Gjalt-Jorn Peters

Maintainer: Gjalt-Jorn Peters gjalt-jorn@userfriendlyscience.com

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 (16 x 14) "layout": 23 grobs
#>     z         cells             name
#> 1   0 ( 1-16, 2-14)       background
#> 2   5 ( 8- 8, 7- 7)           spacer
#> 3   7 ( 9- 9, 7- 7)           axis-l
#> 4   3 (10-10, 7- 7)           spacer
#> 5   6 ( 8- 8, 8- 8)           axis-t
#> 6   1 ( 9- 9, 8- 8)            panel
#> 7   9 (10-10, 8- 8)           axis-b
#> 8   4 ( 8- 8, 9- 9)           spacer
#> 9   8 ( 9- 9, 9- 9)           axis-r
#> 10  2 (10-10, 9- 9)           spacer
#> 11 10 ( 7- 7, 8- 8)           xlab-t
#> 12 11 (11-11, 8- 8)           xlab-b
#> 13 12 ( 9- 9, 6- 6)           ylab-l
#> 14 13 ( 9- 9,10-10)           ylab-r
#> 15 14 ( 9- 9,12-12)  guide-box-right
#> 16 15 ( 9- 9, 4- 4)   guide-box-left
#> 17 16 (13-13, 8- 8) guide-box-bottom
#> 18 17 ( 5- 5, 8- 8)    guide-box-top
#> 19 18 ( 9- 9, 8- 8) guide-box-inside
#> 20 19 ( 4- 4, 8- 8)         subtitle
#> 21 20 ( 3- 3, 8- 8)            title
#> 22 21 (14-14, 8- 8)          caption
#> 23 22 ( 9- 9, 1- 1)     subquestions
#>                                              grob
#> 1                rect[plot.background..rect.1889]
#> 2                                  zeroGrob[NULL]
#> 3            absoluteGrob[GRID.absoluteGrob.1877]
#> 4                                  zeroGrob[NULL]
#> 5                                  zeroGrob[NULL]
#> 6                       gTree[panel-1.gTree.1869]
#> 7            absoluteGrob[GRID.absoluteGrob.1873]
#> 8                                  zeroGrob[NULL]
#> 9            absoluteGrob[GRID.absoluteGrob.1881]
#> 10                                 zeroGrob[NULL]
#> 11                                 zeroGrob[NULL]
#> 12 titleGrob[axis.title.x.bottom..titleGrob.1884]
#> 13                                 zeroGrob[NULL]
#> 14                                 zeroGrob[NULL]
#> 15                                 zeroGrob[NULL]
#> 16                                 zeroGrob[NULL]
#> 17                                 zeroGrob[NULL]
#> 18                                 zeroGrob[NULL]
#> 19                                 zeroGrob[NULL]
#> 20         zeroGrob[plot.subtitle..zeroGrob.1886]
#> 21            zeroGrob[plot.title..zeroGrob.1885]
#> 22          zeroGrob[plot.caption..zeroGrob.1887]
#> 23                                 gtable[layout]

### Number of cylinders, by default comparing lowest value
### (4) to highest (8):
ggProportionPlot(mtcars, items=c('cyl'));
#> TableGrob (16 x 14) "layout": 23 grobs
#>     z         cells             name
#> 1   0 ( 1-16, 2-14)       background
#> 2   5 ( 8- 8, 7- 7)           spacer
#> 3   7 ( 9- 9, 7- 7)           axis-l
#> 4   3 (10-10, 7- 7)           spacer
#> 5   6 ( 8- 8, 8- 8)           axis-t
#> 6   1 ( 9- 9, 8- 8)            panel
#> 7   9 (10-10, 8- 8)           axis-b
#> 8   4 ( 8- 8, 9- 9)           spacer
#> 9   8 ( 9- 9, 9- 9)           axis-r
#> 10  2 (10-10, 9- 9)           spacer
#> 11 10 ( 7- 7, 8- 8)           xlab-t
#> 12 11 (11-11, 8- 8)           xlab-b
#> 13 12 ( 9- 9, 6- 6)           ylab-l
#> 14 13 ( 9- 9,10-10)           ylab-r
#> 15 14 ( 9- 9,12-12)  guide-box-right
#> 16 15 ( 9- 9, 4- 4)   guide-box-left
#> 17 16 (13-13, 8- 8) guide-box-bottom
#> 18 17 ( 5- 5, 8- 8)    guide-box-top
#> 19 18 ( 9- 9, 8- 8) guide-box-inside
#> 20 19 ( 4- 4, 8- 8)         subtitle
#> 21 20 ( 3- 3, 8- 8)            title
#> 22 21 (14-14, 8- 8)          caption
#> 23 22 ( 9- 9, 1- 1)     subquestions
#>                                              grob
#> 1                rect[plot.background..rect.1959]
#> 2                                  zeroGrob[NULL]
#> 3            absoluteGrob[GRID.absoluteGrob.1947]
#> 4                                  zeroGrob[NULL]
#> 5                                  zeroGrob[NULL]
#> 6                       gTree[panel-1.gTree.1939]
#> 7            absoluteGrob[GRID.absoluteGrob.1943]
#> 8                                  zeroGrob[NULL]
#> 9            absoluteGrob[GRID.absoluteGrob.1951]
#> 10                                 zeroGrob[NULL]
#> 11                                 zeroGrob[NULL]
#> 12 titleGrob[axis.title.x.bottom..titleGrob.1954]
#> 13                                 zeroGrob[NULL]
#> 14                                 zeroGrob[NULL]
#> 15                                 zeroGrob[NULL]
#> 16                                 zeroGrob[NULL]
#> 17                                 zeroGrob[NULL]
#> 18                                 zeroGrob[NULL]
#> 19                                 zeroGrob[NULL]
#> 20         zeroGrob[plot.subtitle..zeroGrob.1956]
#> 21            zeroGrob[plot.title..zeroGrob.1955]
#> 22          zeroGrob[plot.caption..zeroGrob.1957]
#> 23                                 gtable[layout]

if (FALSE) {
### 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 (16 x 14) "layout": 23 grobs
#>     z         cells             name
#> 1   0 ( 1-16, 2-14)       background
#> 2   5 ( 8- 8, 7- 7)           spacer
#> 3   7 ( 9- 9, 7- 7)           axis-l
#> 4   3 (10-10, 7- 7)           spacer
#> 5   6 ( 8- 8, 8- 8)           axis-t
#> 6   1 ( 9- 9, 8- 8)            panel
#> 7   9 (10-10, 8- 8)           axis-b
#> 8   4 ( 8- 8, 9- 9)           spacer
#> 9   8 ( 9- 9, 9- 9)           axis-r
#> 10  2 (10-10, 9- 9)           spacer
#> 11 10 ( 7- 7, 8- 8)           xlab-t
#> 12 11 (11-11, 8- 8)           xlab-b
#> 13 12 ( 9- 9, 6- 6)           ylab-l
#> 14 13 ( 9- 9,10-10)           ylab-r
#> 15 14 ( 9- 9,12-12)  guide-box-right
#> 16 15 ( 9- 9, 4- 4)   guide-box-left
#> 17 16 (13-13, 8- 8) guide-box-bottom
#> 18 17 ( 5- 5, 8- 8)    guide-box-top
#> 19 18 ( 9- 9, 8- 8) guide-box-inside
#> 20 19 ( 4- 4, 8- 8)         subtitle
#> 21 20 ( 3- 3, 8- 8)            title
#> 22 21 (14-14, 8- 8)          caption
#> 23 22 ( 9- 9, 1- 1)     subquestions
#>                                              grob
#> 1                rect[plot.background..rect.2037]
#> 2                                  zeroGrob[NULL]
#> 3            absoluteGrob[GRID.absoluteGrob.2025]
#> 4                                  zeroGrob[NULL]
#> 5                                  zeroGrob[NULL]
#> 6                       gTree[panel-1.gTree.2017]
#> 7            absoluteGrob[GRID.absoluteGrob.2021]
#> 8                                  zeroGrob[NULL]
#> 9            absoluteGrob[GRID.absoluteGrob.2029]
#> 10                                 zeroGrob[NULL]
#> 11                                 zeroGrob[NULL]
#> 12 titleGrob[axis.title.x.bottom..titleGrob.2032]
#> 13                                 zeroGrob[NULL]
#> 14                                 zeroGrob[NULL]
#> 15                                 zeroGrob[NULL]
#> 16                                 zeroGrob[NULL]
#> 17                                 zeroGrob[NULL]
#> 18                                 zeroGrob[NULL]
#> 19                                 zeroGrob[NULL]
#> 20         zeroGrob[plot.subtitle..zeroGrob.2034]
#> 21            zeroGrob[plot.title..zeroGrob.2033]
#> 22          zeroGrob[plot.caption..zeroGrob.2035]
#> 23                                 gtable[layout]