Skip to contents

This vignette shows you how to plot tableplots from eatRep data. The workflow is optimized for Bildungstrend-graphs, but can be expanded for plotting other eatRep data as well.

Geschlechterkapitel

Abbildung 6.5

First we prepare the data:

mw_prepped <- prep_tablebarplot(trend_mw,
  subgroup_var = "geschlecht",
  comparisons = c("groupDiff", "crossDiff_of_groupDiff")
)

It makes our live a lot easier if we set the comparions argument in the above column. This dramatically reduces the output, as only the relevant columns are prepared. To see which comparisons we actually need, we can take a look at the plain data.frame:

View(trend_mw$plain)

Now, the plot we want to build has some special requirements which demand some further preparation of the data. First of all, the example data only contains one domain, I add a second one manually. Of course you don’t have to do this, its just for demonstration purposes.

mw_prepped_lesen <- mw_prepped
mw_prepped_lesen$domain <- "Leseverstehen"

mw_prepped_hören <- mw_prepped
mw_prepped_hören$domain <- "Hörverstehen"

mw_prepped <- rbind(mw_prepped_lesen, mw_prepped_hören)

Now we can easily plot the barplot, after sorting the data.frame and adding a y-axis:

mw_prepped <- mw_prepped[order(mw_prepped$TR_BUNDESLAND), ]
mw_prepped$y_axis <- 1:nrow(mw_prepped)

## Rename "total" to "Deutschland"
mw_prepped$TR_BUNDESLAND <- gsub("total", "Deutschland", mw_prepped$TR_BUNDESLAND)
mw_prepped$TR_BUNDESLAND <- gsub("ue", "ü", mw_prepped$TR_BUNDESLAND)

The tables need some more work, because they need to be connected with the barplot, but only have half the rows. Therefore we need to build separate variables for each table part, with the respective rows of the domain not plotted in the table half set to empty strings.

mw_prepped_lesen <- mw_prepped
mw_prepped_lesen[mw_prepped_lesen$domain == "Hörverstehen", colnames(mw_prepped) != "y_axis"] <- NA

mw_prepped_hören <- mw_prepped
mw_prepped_hören[mw_prepped_hören$domain == "Leseverstehen", colnames(mw_prepped) != "y_axis"] <- NA


mw_preped_lh <- merge(mw_prepped_lesen, mw_prepped_hören, by = c("y_axis"), suffixes = c("_lesen", "_hören"))
mw_prepped_final <- merge(mw_prepped, mw_preped_lh, by = c("y_axis"))

Now we can plot the table like usual, with one exception: I can nudge the table contents a bit downwards, so they also fill the empty row.

tableplot_6.5 <- plot_tablebarplot(
  dat = mw_prepped_final,
  bar_est = "est_2022_groupDiff_mean",
  bar_fill = "domain",
  bar_sig = "sig_2022_groupDiff_mean",
  columns_table = list(
    "TR_BUNDESLAND_lesen", "est_2022_groupDiff_mean_lesen", "se_2022_groupDiff_mean_lesen", "es_2022_groupDiff_mean_lesen", "est_2022_groupDiff_mean_hören", "se_2022_groupDiff_mean_hören", "es_2022_groupDiff_mean_hören"
  ),
  columns_table_sig_bold = list(NULL, "sig_2022_groupDiff_mean_lesen", NULL, "sig_2022_groupDiff_mean_lesen", "sig_2022_groupDiff_mean_hören", NULL, "sig_2022_groupDiff_mean_hören"),
  columns_table_sig_superscript = list(NULL, "sig_2022_crossDiff_of_groupDiffTotal_mean_lesen", NULL, "sig_2022_crossDiff_of_groupDiffTotal_mean_lesen", "sig_2022_crossDiff_of_groupDiffTotal_mean_hören", NULL, "est_2022_crossDiff_of_groupDiffTotal_mean_hören"),
  columns_round = list(NULL, 0, 1, 2, 0, 1, 2),
  columns_table_se = list(NULL, NULL, "se_2022_groupDiff_mean_lesen", NULL, NULL, "se_2022_groupDiff_mean_hören", NULL),
  headers = list("", "*M<sub>Jungen</sub> - M<sub>Mädchen</sub>*", "(*SE*)", "*d*", "*M<sub>Jungen</sub> - M<sub>Mädchen</sub>*", "(*SE*)", "*d*", "**Vorsprung zugunsten der<br>Mädchen**"),
  column_spanners = list("**Leseverstehen**" = c(2, 4), "**Höhrverstehen**" = c(5, 7)),
  y_axis = "y_axis",
  plot_settings = plotsettings_tablebarplot(
    axis_x_lims = c(-5, 70),
    columns_alignment = c(0, rep(2, 6), NULL), # NULL evtl. nicht optimal, oder in die Message mit rein.
    headers_alignment = c(rep(0.5, 2), 2, rep(0.5, 2), 2, 0.5, 0.5),
    headers_nudge_y = c(rep(0, 7), 0.5),
    columns_nudge_y = c(rep(-0.5, 4), rep(0.5, 3)),
    columns_width = c(0.15, rep(0.1, 6), 0.25),
    bar_nudge_y = rep(c(-0.1, 0.1), nrow(mw_prepped_lesen) / 2),
    background_stripes_colour = c(rep(c("white", "white", "#EBFDF3", "#EBFDF3"), 8), "grey", "grey"),
    background_stripes_border = "background_line_table",
    bar_fill_colour = c("#20D479", "#8DEBBC"),
    bar_background_lines = "scale_breaks",
    bar_background_lines_linetype = "solid",
    default_list = barplot_table_plot_pattern
  )
)

Finally we can save the plot:

save_plot(tableplot_6.5, filename = "C:/Users/hafiznij/Downloads/abb6.5.pdf")

Abbildung 6.6

First the data has to be prepared:

dat_6.6 <- prep_tablebarplot(
  trend_mw,
  subgroup_var = "geschlecht",
  par = c("mean", "sd"), ## We need both mean and sd for the plot
  comparisons = c("none", "trend", "trend_crossDiff") ## Set for smaller output-table
)

Afterwards, we have to bring it in the format we want to plot it in:

dat_6.6 <- subset(dat_6.6, geschlecht %in% c("m", "w"))
dat_6.6$geschlecht <- gsub("m", "Jungen", dat_6.6$geschlecht)
dat_6.6$geschlecht <- gsub("w", "Mädchen", dat_6.6$geschlecht)

dat_6.6_a <- dat_6.6[order(dat_6.6$TR_BUNDESLAND), ]

dat_6.6_a$TR_BUNDESLAND[duplicated(dat_6.6_a$TR_BUNDESLAND)] <- " "
dat_6.6_a$TR_BUNDESLAND <- gsub("total", "Deutschland", dat_6.6_a$TR_BUNDESLAND)
dat_6.6_a <- process_bundesland(dat_6.6_a)

We will have to plot multiple plots and combine them. To do that, we will have to set the column widths, so they are identical over the different plots:

column_widths_stand <- standardize_column_width(
  column_widths = list(
    p1 = c(0.1, rep(0.04, 7), 0.05, rep(0.04, 2), NA),
    p2 = c(0.05, rep(0.04, 2), NA)
  ),
  plot_ranges = c(75, 75) # Range of the x-axes of both plots set in 'axis_x_lims'.
)

We can just use this object in the plot-settings to adjust the column widths.

p_1 <- plot_tablebarplot(
  dat = dat_6.6_a,
  bar_est = "est_2015 - 2009_trend_mean",
  bar_label = NULL,
  bar_sig = "sig_2015 - 2009_trend_mean",
  bar_fill = "geschlecht",
  column_spanners = list(
    "**2009**<sup>a</sup>" = c(3, 4),
    "**2015**<sup>a</sup>" = c(5, 6),
    "**2022**<sup>a</sup>" = c(7, 8),
    "**Differenz 2015-2009<sup>a</sup>**" = c(9, 12)
  ),
  columns_table_se = list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "se_2015 - 2009_trend_mean", NULL),
  headers = list("**Land**", " ", "*M*", "*SD*", "*M*", "*SD*", "*M*", "*SD*", "*M<sub>2015</sub> - M<sub>2009</sub>*", "*(SE)*", "*d*", " "),
  columns_table = c("TR_BUNDESLAND", "geschlecht", "est_2009_none_mean", "est_2009_none_sd", "est_2015_none_mean", "est_2015_none_sd", "est_2022_none_mean", "est_2022_none_sd", "est_2015 - 2009_trend_mean", "se_2015 - 2009_trend_mean", "es_2015 - 2009_trend_mean"),
  columns_table_sig_bold = list(NULL, NULL, "sig_2009_none_mean",
    NULL, "sig_2015_none_mean", NULL, "sig_2022_none_mean", NULL, "sig_2015 - 2009_trend_mean", NULL, "sig_2015 - 2009_trend_mean"),
  columns_table_sig_superscript = list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "sig_2015 - 2009_trend_crossDiffTotal_mean", NULL,NULL),
  y_axis = "y_axis",
  columns_round = c(rep(0, 10), 2),
  plot_settings = plotsettings_tablebarplot(
    axis_x_lims = c(-5, 70),
    bar_pattern_spacing = 0.0154, ## We calculated this below in standardize_pattern_spacing()
    background_stripes_colour = c(rep(c("white", "white", "#EBFDF3", "#EBFDF3"), 8), "grey", "grey"),
    bar_fill_colour = c("#20D479", "#8DEBBC"),
    columns_alignment = c(0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 2),
    columns_width = column_widths_stand$p1, ## This is the column-width object we set above
    headers_alignment = c(0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0),
    default_list = barplot_table_plot_pattern
  )
)

p_2 <- plot_tablebarplot(
  dat = dat_6.6_a,
  bar_est = "est_2022 - 2015_trend_mean",
  bar_label = NULL,
  bar_sig = "sig_2022 - 2015_trend_mean",
  bar_fill = "geschlecht",
  column_spanners = list(
    "**Differenz 2022-2015**" = c(1, 4)
  ),
  headers = list(
    "*M<sub>2022</sub> - M<sub>2015</sub>*",
    "*(SE)*",
    "*d*",
    " "
  ),
  columns_table = c(
    "est_2022 - 2015_trend_mean",
    "se_2022 - 2015_trend_mean",
    "es_2022 - 2015_trend_mean"
  ),
  columns_table_se = list(NULL, "se_2022 - 2015_trend_mean", NULL),
  columns_table_sig_bold = list("sig_2022 - 2015_trend_mean", NULL, "sig_2022 - 2015_trend_mean"),
  columns_table_sig_superscript = list("sig_2022 - 2015_trend_crossDiffTotal_mean", NULL, NULL),
  y_axis = "y_axis",
  columns_round = c(0, 0, 2),
  plot_settings = plotsettings_tablebarplot(
    axis_x_lims = c(-5, 70),
    bar_pattern_spacing = 0.0346, ## We calculated this below in standardize_pattern_spacing()
    background_stripes_colour = c(rep(c("white", "white", "#EBFDF3", "#EBFDF3"), 8), "grey", "grey"),
    bar_fill_colour = c("#20D479", "#8DEBBC"),   
    columns_alignment = c(rep(0.5, 2), 2),
    columns_width = column_widths_stand$p2, ## This is the column-width object we set above
    headers_alignment = c(0.5, 0.5, 0.5, 0),
    default_list = barplot_table_plot_pattern
  )
)

It might be necessary to standardize the pattern spacing over the plots:

bar_pattern_spacing_stand <- standardize_pattern_spacing(list(p_1, p_2), pattern_spacing = 0.05)
## Next Step: Update the bar_pattern_spacing values in your respective plots with the output values.
bar_pattern_spacing_stand
## [1] 0.0154 0.0346

These values have to be set in the plot-settings above.

Now we can combine the plots:

tableplot_6.6 <- combine_plots(list(p_1, p_2))

And save the result:

save_plot(tableplot_6.6, filename = "C:/Users/hafiznij/Downloads/abb_6.6.pdf", width = 320)