Making Beautiful Tables with R
Add summaries
This lesson is called Add summaries, part of the Making Beautiful Tables with R course. This lesson is called Add summaries, part of the Making Beautiful Tables with R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
It’s always good to not just show the data, but to also to show a summary of the data. In this case, it could be something as simple as the maximum number of penguins of a specific species living on an island over the years.
maximum_summary <- penguin_counts_wider |>
group_by(island) |>
summarise(across(
.cols = -year,
.fns = max
)) |>
mutate(year = 'Maximum')
total_summary <- penguin_counts_wider |>
group_by(island) |>
summarise(across(
.cols = -year,
.fns = sum
)) |>
mutate(year = 'Total')
bind_rows(penguin_counts_wider, maximum_summary, total_summary) |>
mutate(island = paste('Island: ', island)) |>
arrange(island, year) |>
as_grouped_data(groups = 'island') |>
as_flextable(hide_grouplabel = TRUE) |>
set_header_labels(
island = 'Island',
year = '',
Adelie_female = 'Female',
Adelie_male = 'Male',
Chinstrap_female = 'Female',
Chinstrap_male = 'Male',
Gentoo_female = 'Female',
Gentoo_male = 'Male'
) |>
add_header_row(
values = c('', 'Adelie', 'Chinstrap', 'Gentoo'),
colwidths = c(1, 2, 2, 2)
) |>
add_header_lines(
values = c('Penguins in the Palmer Archipelago', 'Data is courtesy of the {palmerpenguins} R package')
) |>
align(i = 3, align = 'center', part = 'header') |>
colformat_num(i = ~ (is.na(island)), na_str = '-') |>
align(
i = ~ (year %in% 2007:2009),
j = 'year',
align = 'right'
) |>
autofit()
Your Turn
For each continent, add the average and maximum life expectancy for each year to the table. Your table should look like this:
Have any questions? Put them below and we will help you out!
Course Content
16 Lessons
1
Get Data Into the Right Format to Create your First Table
12:08
2
Use better column names and a title
07:39
3
Align columns
06:53
4
Use groups instead of repetitive columns
04:37
5
Format your table's numbers
03:48
6
Add summaries
10:26
7
Add additional horizontal lines
04:51
8
Add background colors
03:54
9
Change the text appearance
09:14
10
Change cell properties
20:09
11
Export Your Tables
11:32
12
Heat map columns
11:00
13
Adding Charts with Flextable
10:04
14
Add your own ggplot
12:32
15
Case Study
44:16
16
Wrapping Up Making Beautiful Tables with R
You need to be signed-in to comment on this post. Login.