This document demonstrates how to implement and plot different chart types and chart combinations using minCombinR. It will be using the classic tabular mtcars dataset that is built into R. This document assumes that you have already run the "Getting started with minCombinR".

devtools::load_all() # TODO: temporary once things are done
library(dplyr)
library(shiny)

# Load in the mtcars dataset
data(mtcars)

# Let's first transform the "am" attribute from ordered to categorical:
# This involves transforming all instances of 1/0 to "Automatic"/"Manual": 
mtcars$am <- factor(mtcars$am, labels = c("Automatic", "Manual"))
mtcars$carb <- factor(mtcars$carb)

#add names of cars, make it more interesting
mtcars$brand<-sapply(rownames(mtcars),function(x){strsplit(x,"\\s+")[[1]][1]}) %>% unname()

Common statistical charts

# Let's specify and plot some single charts.

# Bar chart:
bar_chart <- specify_single(chart_type = "bar", data = "mtcars", x = "am")
plot(bar_chart)

# Line chart:
line_chart <- specify_single(chart_type = "line", data = "mtcars", x = "mpg", y = "hp")
plot(line_chart)

# Scatter plot:
scatter_chart <- specify_single(chart_type = "scatter", data = "mtcars", x = "mpg", y = "hp")
plot(scatter_chart)

# Histogram:
histogram_chart <- specify_single(chart_type = "histogram", data = "mtcars", x = "mpg")
plot(histogram_chart)

# Probability Density Function (PDF) plot:
pdf_chart <- specify_single(chart_type = "pdf", data = "mtcars", x = "wt")
plot(pdf_chart)

# Boxplot:
boxplot_chart <- specify_single(chart_type = "boxplot", data = "mtcars", x = "am", y = "hp")
plot(boxplot_chart)

# Swarm plot:
swarmplot_chart <- specify_single(chart_type = "swarmplot", data = "mtcars", x = "am", y = "mpg")
plot(swarmplot_chart)

# We'll even let you make a pie chart:
pie_chart <- specify_single(chart_type = "pie", data = "mtcars", x = "carb")
plot(pie_chart)

Combinations

Unaligned

Unaligned combinations can be used when you just want to put a bunch of plots together and there are no spatial or visual linkages between the plots themselves.

# Specify that you want to combine the bar_chart, box plot, scatter_chart and swarmplot_chart
mg_combo <- specify_combination(combo_type = "unaligned", 
                                base_charts = c("bar_chart","boxplot_chart","scatter_chart", "swarmplot_chart"))
# Now plot it!
plot(mg_combo)

Small Multiples

Small multiple charts are visually linked because they show the same underlying chart type while showing different subsets of the data. Another common name for this is facets.

# Let's try it again with a box plot:
# We will use the boxplot_chart specification that we made earlier

# We will make the engine attribute a categorical variable (either V-shaped or Straight) so we can facet using this variable.
mtcars$vs <- factor(mtcars$vs, labels = c("V-shaped", "Straight"))

# Now specify the combination
sm_combo_boxplot <- specify_combination(combo_type = "small_multiple", 
                                        base_charts = "boxplot_chart",
                                        facet_by = "vs")
# And plot!
plot(sm_combo_boxplot)

Color Aligned Combinations

It could be interesting to link several different chart types together by color.

If you have nontabular data, minCombinR will find matching links between non-tabular and tabular data so they can be color aligned. Mtcars is a tabular dataset, so we won't be demonstrating this here but if you are interested, check out the examples using the ebola dataset that can be found in the inst/examples folder in the r markdown notebook called chart_combinations.

# Specify the line chart and the scatter chart that we want to combine
my_scatter_chart <- specify_single(chart_type = "scatter", data = "mtcars", x = "hp", y = "mpg")
my_bar_chart <- specify_single(chart_type = "bar", data = "mtcars", x = "carb")

plot(my_scatter_chart)
plot(my_bar_chart)

# Specify that you want to combine with color
color_combo <- specify_combination(combo_type = "color_aligned", 
                                   base_charts = c("my_scatter_chart", "my_bar_chart"),
                                   link_by = "am")
# Now plot!
plot(color_combo)

Spatially Aligned Combinations

# Table data
scatter_chart <- specify_single(chart_type = "scatter", data = "mtcars", x = "wt", y = "brand")
scatter_chart_two <- specify_single(chart_type = "scatter", data = "mtcars", x = "wt", 
                                  y = "brand")
bar_alt <- specify_single(chart_type = "bar", data = "mtcars", x = "brand", y = "wt", rm_x_label=TRUE)

spatial_aligned_combo <- specify_combination(combo_type = "spatial_aligned",
                                       base_charts = c("bar_alt", "scatter_chart", "scatter_chart_two"))

plot(spatial_aligned_combo)


sfisher4/gevitR documentation built on Feb. 10, 2020, 6:29 p.m.