The qol-package already is its own eco system in terms of data wrangling and tabulation. And some time in the future it will implement it’s own diagram framework built from scratch with version 1.4.0. Isn’t this great? Or are you scratching your head and ask yourself: Why?
The short answer is: Because diagrams can be created easier, faster and more beautiful than with any other framework. For the long answer keep on reading.
For the very long answer and all the backgrounds have a look at this post: https://s3rdia.github.io/qol_blog/posts/12.%20Graphics/
And if you just want to get a package overview look here: https://github.com/s3rdia/qol/tree/main
With every tools like Excel, SAS or ggplot and every way of creating diagrams there is this one thing that bothers me a lot: You can get diagrams on screen pretty easy, but they all look like ... well not that good. To me the focus is always too much on the engineering part than on the designing part. I want my full attention to be on the diagram the whole time and not on the code. When you have to fight the code, you can’t clearly see where you are heading. This is the problem I like to solve.
So my basic question for this new framework was: Why can’t I work like I think? Wouldn’t it be great to generate multiple different iterations of a graphic in no time with different visuals, without having to write much code and with the computer bothering with the engineering of the diagram and not the user? Here are some coding impressions of how the diagrams from the screenshots were made:
###############################################################################
# First we create our dummy data and some formats
###############################################################################
my_data <- dummy_data(1000000, insert_na = FALSE)
age. <- discrete_format(
"Total" = 0:100,
"under 18" = 0:17,
"18 to under 65" = 18:64,
"65 and older" = 65:100)
sex. <- discrete_format(
"Total" = 1:2,
"Male" = 1,
"Female" = 2)
education. <- discrete_format(
"Low" = "low",
"Middle" = "middle",
"High" = "high")
###############################################################################
# Top diagram
###############################################################################
# All options concerning the visual appearance of the diagram can be set globally,
# like here, so that these options transfer across multiple diagrams. Or you can
# set them directly in the design_graphic() function so that they only apply
# locally for that specific diagram.
set_graphic_options(color_theme = "aurora",
primary_values_decimals = 0,
segment_label_group = 6,
segment_line_length = 2,
segment_line_type = "dotted",
rotate_values = TRUE)
# To set variable expressions beside each other on the axes, we just pass a vector
# of variable names into the parameter.
# We can also just put in a different statistic, the value axes will adjust
# accordingly.
# To add custom texts just add a textbox in the "add_texts" parameter.
my_data |>
design_graphic(axes_variables = c(age, education),
segments = sex,
values = weight,
statistics = sum,
diagram = dg_vbars,
formats = list(sex = sex., age = age., education = education.),
titles = "Multiple variables side by side can be achieved by passing a vector",
footnotes = "Also note the custom text to the right.",
add_texts = add_textbox("Degree of education", 11, 1, font_face = "bold"))
###############################################################################
# Center diagram
###############################################################################
# Playing around with the different sliders and switches can actually be fun
set_graphic_options(primary_axes_max = 90000,
primary_axes_steps = 3,
segment_label_group = 2,
rotate_segment_labels = TRUE)
# To set variable expressions beside each other as segments, we just do the same
# with the segments parameter as before with the axes.
my_data |>
design_graphic(axes_variables = age,
segments = c(sex, education),
values = weight,
statistics = sum,
diagram = dg_vbars,
formats = list(sex = sex., age = age., education = education.),
titles = "Multiple variables side by side can be achieved by passing a vector",
footnotes = "Also note the custom text to the right.",
add_texts = add_textbox("Degree of education", 7, 8, 3, font_face = "bold"))
###############################################################################
# Bottom diagram
###############################################################################
# Let us create a format without total first to have a bit more visual space
sex2. <- discrete_format(
"Male" = 1,
"Female" = 2)
# We try out a different style again, because why not?
set_graphic_options(diagram_height = 6.8,
color_usage = high_contrast_usage,
primary_axes_max = 20000,
primary_axes_scale = 0.001,
segment_label_type = "legend",
legend_y_pos = "top",
legend_columns = 3,
rotate_values = TRUE)
# To have a multi layered variable axes you just "add up" the variables with a
# + symbol in between them. There is nothing more to it. You can nest as many
# variables as you like – of course at some point it doesn't make sense anymore.
# Additionally we can add as many custom texts as we like, just put some textboxes
# inside a list.
my_data |>
design_graphic(axes_variables = "age + education",
segments = sex,
values = weight,
statistics = sum,
diagram = dg_vbars,
formats = list(sex = sex2., age = age., education = education.),
titles = "Nesting categories works with a + symbol",
footnotes = "That was quite easy.",
add_texts = list(add_textbox("Degree of education", 6.8, 1, font_face = "bold"),
add_textbox("in thousand", 0.2, 8.2, 1),
add_textbox("HAVE AS MANY TEXTS AS YOU LIKE", 8, 7.5,
font_color = "#FF0000",
font_size = 20,
font_face = "italic",
rotation = -45)))
This framework – as of now – has reached an alpha stage. Which means there are things one can actually work with and that work fairly robust, but there are also some things missing and the function can error when it shouldn’t.
As of now one can only create vertical bar charts, but there are over 150 cosmetic parameters to play with. I implemented a lot of unit tests so that over 90 % of the code is covered. Which means, if I implement new features, I make sure that nothing else breaks unintentional. But of course there can be breaking changes down the road.
During this alpha stage I will mainly concentrate on building multiple different diagram types and add parameters along the way. How many built in diagram types there will be and which ones, I don’t know, but I will focus on the most common/useful ones – in my eyes. Of course making this thing more robust is always on my todo list as well as optimizing things as I stumble upon them.
So stay tuned, there is more to come in the future.