Normalized and Non-Normalized Diverging barchart
spsanderson opened this issue · 0 comments
Compare variation in values between a small number of items (or categories) with respect to a fixed reference.
Diverging Bars is a bar chart that can handle both negative and positive values. This can be implemented by a smart tweak with geom_bar(). But the usage of geom_bar() can be quite confusing. That's because it can be used to make a bar chart as well as a histogram.
By default, geom_bar() has the stat set to count. That means, when you provide just a continuous X variable (and no Y variable), it tries to make a histogram out of the data.
In order to make a bar chart create bars instead of the histogram, you need to do two things.
Set stat=identity
Provide both x and y inside aes()
where, x is either character or factor and y is numeric.
In order to make sure you get diverging bars instead of just bars, make sure, your categorical variable has 2 categories that change values at a certain threshold of the continuous variable. In the below example, the mpg from mtcars
dataset is normalized by computing the z score. Those vehicles with mpg above zero are marked green and those below are marked red.
The following should be in the parameters list:
- .data
- .plot_title
- .plot_subtitle
- .plot_caption
- .x_axis_title
- .y_axis_ttle
- main
ggplot aes()
x, y, label - .interactive - return plotly plot?
library(ggplot2)
theme_set(theme_bw())
# Data Prep
data("mtcars") # load data
mtcars$`car name` <- rownames(mtcars) # create new column for car names
mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2) # compute normalized mpg
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above") # above / below avg flag
mtcars <- mtcars[order(mtcars$mpg_z), ] # sort
mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`) # convert to factor to retain sorted order in plot.
# Diverging Barcharts
ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) +
geom_bar(stat='identity', aes(fill=mpg_type), width=.5) +
scale_fill_manual(name="Mileage",
labels = c("Above Average", "Below Average"),
values = c("above"="#00ba38", "below"="#f8766d")) +
labs(subtitle="Normalised mileage from 'mtcars'",
title= "Diverging Bars") +
coord_flip()
ggplot2 Diverging Bars