This tutorial is inspired and adapted from the sthda practical guide published under the creative commons license.
Download the mousew
dataset from here and load it in R. This dataset contains the weight (in grams) of two strains of mice for both genders.
Draw a histogram of the weight distribution and adjust the range for every bar to 0.5 g.
position
argument.
Draw a density curve of the weight for both genders.
Overlay the histogram representation to the previous plot.
ggplot2
automatically adjusts the range of the axis. Try to override this behaviour and let the x axis start at 0geom_*
can takes its own data
argument that overwrite the one inherited from the ggplot()
call. Might be worth summarising by the mean the weight and a 4 rows tibble. Then pass it to geom_vline()
. Of note, aesthetics are also not inherited when a new data is specified.
expand_limit()
functionstrain
and sex
), see the labeller
argument of facet_wrap()
geom_col()
requires a y
aesthetetic to map on the continuous variablealpha
paramater to give some transparency, will help to spot inconsistencies
Does it make sense?
Draw a bar chart of the summarised weight (with mean) for each sex, colored by strain
position
argument for geom_col()
, default is stack
, alternatives are dodge
(side by side) or fill
for proportions
geom_errorbar()
and using the standard deviations (sd
).position = "dodge"
calls the position_dodge()
function. Look at the help of this function, one example is describing how to align narrower elements like error bars.width
element of geom_errorbar()
to reduce the default which is too large.