This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
If you would like to play around with the visual design of the document, there are numerous templates to choose from. These can be specified into themes and highlights, and can be selected in the YAML box at the start of this document.
For more information on styling, please check the following link: (https://bookdown.org/yihui/rmarkdown/html-document.html#appearance-and-style).
In this particular analysis, we aiming to present how the penguins
differ from species to species, and from island to island. We want to
show case how these differences are present across the 4 measures
collected bill_length_mm
, bill_depth_mm
,
flipper_length_mm
, and body_mass_g
.
To help users navigate our beautiful document with greater ease, and
to provide an element of interactivity, we will be using
{.tabset}
on our headers. Behind the scenes, it operates on
a hierarchical structure, which is dependent on the level of your
heading. This gets a bit weird and theoretical, so playing around with
it in your own work is the best approach to getting a feel for it. But
if you would like more information, please check the following link: https://bookdown.org/yihui/rmarkdown-cookbook/html-tabs.html
For the overall summary table, we will be using the
modelsummmary
package. It has the really cool feature that
it will place histograms into your table! It is a very customisable
package, with plenty of options to choose from for all sorts of
analyses. For more information, please check their website.
Unique | Missing Pct. | Mean | SD | Min | Median | Max | Histogram | |
---|---|---|---|---|---|---|---|---|
Bill Length (mm) | 165 | 1 | 43.9 | 5.5 | 32.1 | 44.5 | 59.6 | |
Bill Depth(mm) | 81 | 1 | 17.2 | 2.0 | 13.1 | 17.3 | 21.5 | |
Flipper Length(mm) | 56 | 1 | 200.9 | 14.1 | 172.0 | 197.0 | 231.0 | |
Body Mass(g) | 95 | 1 | 4201.8 | 802.0 | 2700.0 | 4050.0 | 6300.0 | |
N | % | |||||||
species | Adelie | 152 | 44.2 | |||||
Chinstrap | 68 | 19.8 | ||||||
Gentoo | 124 | 36.0 | ||||||
island | Biscoe | 168 | 48.8 | |||||
Dream | 124 | 36.0 | ||||||
Torgersen | 52 | 15.1 | ||||||
sex | female | 165 | 48.0 | |||||
male | 168 | 48.8 |
For the within group summaries, we are using the
gtsummary
package. This is another flexible table creator,
which I find to be particularly useful and easy to use for subset
summaries and analyses. For more information, check out their website.
Characteristic | Adelie, N = 1521 | Chinstrap, N = 681 | Gentoo, N = 1241 |
---|---|---|---|
island | |||
Biscoe | 44 (29%) | 0 (0%) | 124 (100%) |
Dream | 56 (37%) | 68 (100%) | 0 (0%) |
Torgersen | 52 (34%) | 0 (0%) | 0 (0%) |
Bill Length (mm) | 38.8 (2.7) | 48.8 (3.3) | 47.5 (3.1) |
Unknown | 1 | 0 | 1 |
Bill Depth(mm) | 18.35 (1.22) | 18.42 (1.14) | 14.98 (0.98) |
Unknown | 1 | 0 | 1 |
Flipper Length(mm) | 190 (7) | 196 (7) | 217 (6) |
Unknown | 1 | 0 | 1 |
Body Mass(g) | 3,701 (459) | 3,733 (384) | 5,076 (504) |
Unknown | 1 | 0 | 1 |
sex | |||
female | 73 (50%) | 34 (50%) | 58 (49%) |
male | 73 (50%) | 34 (50%) | 61 (51%) |
Unknown | 6 | 0 | 5 |
1 n (%); Mean (SD) |
Characteristic | Biscoe, N = 1681 | Dream, N = 1241 | Torgersen, N = 521 |
---|---|---|---|
species | |||
Adelie | 44 (26%) | 56 (45%) | 52 (100%) |
Chinstrap | 0 (0%) | 68 (55%) | 0 (0%) |
Gentoo | 124 (74%) | 0 (0%) | 0 (0%) |
Bill Length (mm) | 45.3 (4.8) | 44.2 (6.0) | 39.0 (3.0) |
Unknown | 1 | 0 | 1 |
Bill Depth(mm) | 15.87 (1.82) | 18.34 (1.13) | 18.43 (1.34) |
Unknown | 1 | 0 | 1 |
Flipper Length(mm) | 210 (14) | 193 (8) | 191 (6) |
Unknown | 1 | 0 | 1 |
Body Mass(g) | 4,716 (783) | 3,713 (417) | 3,706 (445) |
Unknown | 1 | 0 | 1 |
sex | |||
female | 80 (49%) | 61 (50%) | 24 (51%) |
male | 83 (51%) | 62 (50%) | 23 (49%) |
Unknown | 5 | 1 | 5 |
1 n (%); Mean (SD) |
Characteristic | female, N = 1651 | male, N = 1681 |
---|---|---|
species | ||
Adelie | 73 (44%) | 73 (43%) |
Chinstrap | 34 (21%) | 34 (20%) |
Gentoo | 58 (35%) | 61 (36%) |
island | ||
Biscoe | 80 (48%) | 83 (49%) |
Dream | 61 (37%) | 62 (37%) |
Torgersen | 24 (15%) | 23 (14%) |
Bill Length (mm) | 42.1 (4.9) | 45.9 (5.4) |
Bill Depth(mm) | 16.43 (1.80) | 17.89 (1.86) |
Flipper Length(mm) | 197 (13) | 205 (15) |
Body Mass(g) | 3,862 (666) | 4,546 (788) |
1 n (%); Mean (SD) |
We can also report our data in text. It will look ugly here, but will be magical in the finished document. The in-line coding is achieved through using backticks and including the letter “r” (as seen below). To simplify process, we first use some data manipulation to define items we want to communicate. The end product allows us to produce transparent analysis, and minimises the risk of typos. As a bonus, it also allows us to re-run our analysis at a much quicker rate if our data is updated!
Once again, we will use the {.tabset}
to allow our
audience to quickly navigate the document. However, this time we are
also using {.tabset-pills}
to change the appearance. Pick
and choose which visual style you think is most appropriate for your
presentation. (Playing with the themes will change the appearance of the
tabsets and tabset-pills as well, so there is plenty of scope for
experimenting).
The mean weight of the Adelie penguins is 3700.66g (sd = 458.57).
The mean weight of the Chinstrap penguins is 3733.09g (sd = 384.34).
The mean weight of the Gentoo penguins is 5076.02g (sd = 504.12).
The mean weight of the penguins on Biscoe island is 4716.02g (sd = 782.86).
The mean weight of the penguins on Dream island is 3712.9g (sd = 384.34).
The mean weight of the penguins on Torgersen island is 3706.37g (sd = 504.12).
Characteristic | Adelie, N = 1521 | Chinstrap, N = 681 | Gentoo, N = 1241 | Test Statistic | p-value2 |
---|---|---|---|---|---|
Body Mass(g) | 3,701 (459) | 3,733 (384) | 5,076 (504) | 343.6263 | <0.001 |
1 Mean (SD) | |||||
2 One-way ANOVA |
One way ANOVA on differences of body mass by species was significant (F(2,339) = 343.6262752, p < 0.001).
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
Characteristic | Biscoe, N = 1681 | Dream, N = 1241 | Torgersen, N = 521 | Test Statistic | p-value2 |
---|---|---|---|---|---|
Body Mass(g) | 4,716 (783) | 3,713 (417) | 3,706 (445) | 110.008 | <0.001 |
1 Mean (SD) | |||||
2 One-way ANOVA |
One way ANOVA on differences of body mass by Island was significant (F(2,339) = 110.0079651, p < 0.001).
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
We can also use R to allow for the reporting of more complex analyses, such as hierarchical regressions. In this example, we will the tabset feature to communicate the seperate layers of our hierarchical regression, so that the audience can compare the differences.
Here we make use of the broom
package to help us extract
our regression stats straight into our text. Specifically, we use the
tidy()
function to extract the model variable statistics,
and the glance()
function to extract the model fit
statistics. No more copy and pasting values and re-writing entire
documents at the whim of reviewers/supervisors. Simply update the model,
and the rest will take care of itself.
Simple model | |
---|---|
(Intercept) | −0.68*** |
(0.06) | |
scale(flipper_length_mm) | 0.35*** |
(0.05) | |
speciesChinstrap | −0.11+ |
(0.06) | |
speciesGentoo | 1.04*** |
(0.11) | |
sexmale | 0.66*** |
(0.05) | |
Num.Obs. | 333 |
R2 | 0.867 |
R2 Adj. | 0.865 |
Simple model | Advanced model | |
---|---|---|
(Intercept) | −0.68*** | −0.69*** |
(0.06) | (0.07) | |
scale(flipper_length_mm) | 0.35*** | 0.32*** |
(0.05) | (0.07) | |
speciesChinstrap | −0.11+ | −0.13 |
(0.06) | (0.08) | |
speciesGentoo | 1.04*** | 0.91*** |
(0.11) | (0.12) | |
sexmale | 0.66*** | 0.65*** |
(0.05) | (0.05) | |
scale(flipper_length_mm) × speciesChinstrap | −0.09 | |
(0.11) | ||
scale(flipper_length_mm) × speciesGentoo | 0.16 | |
(0.10) | ||
Num.Obs. | 333 | 333 |
R2 | 0.867 | 0.869 |
R2 Adj. | 0.865 | 0.867 |
Species
is a significant predictor of Bodymass in Penguins
(\(\beta\) = 0.32, p <
0.001).Here we will make use of the plotly
package to make our
plots interactive. The good news is, this is super simple if you already
know how to use ggplot
. Simply assign your plot to an
object, and then wrap it in the ggplotly()
function. The
appearance of your plot may change compared to your original design as
its converted to a plotly
item, so so experimenting is
recommended. For more information and vignettes, check the following
link: https://plotly.com/ggplot2/
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_ydensity()`).
## Warning: Groups with fewer than two data points have been dropped.
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).