Skip to content
R for the Rest of Us Logo

Making Beautiful Tables with R

Add additional horizontal lines

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.

We’ve arrived at the probably the most fun lesson in this part of the course which is about theming and making the table more pretty. We’ll start by adding more lines to distinguish our summaries from the rest.

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'
  ) |> 
  hline(i = ~ (!is.na(island) | year %in% c('2009', 'Total'))) |> 
  autofit()

Your Turn

Structure your table by adding additional horizontal lines. More specifically, add horizontal lines after each continent label and before and after the block of maximum and average values. Your table should look like this:

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Jesmin Das

Jesmin Das • January 31, 2024

Pending approval

How to use : lead under hline " lead(country == 'Maximum', 2, default = FALSE) " Any documentation for reference ?

David Keyes

David Keyes Founder • February 1, 2024

Yes, check out the documentation here!

Majeed Oladokun

Majeed Oladokun • August 25, 2024

Hi, On the exercise in module 7, as the hline() adds horizontal line to the bottom of selected cell, I could not add the horizontal without listing some countries as vector. I used the following code for the horizontal line:

hline(
    i = ~ (!is.na(continent) | 
             country %in% c("Maximum", "Sierra Leone", "Nicaragua",
                            "Syria", "United Kingdom", "New Zealand"))
  )

Although the code works fine, I'm wondering if there is a way to avoid hard coding the vector of countries above "Average" entry in the country field.

Thank you.

Albert Rapp

Albert Rapp • August 27, 2024

Hi Majeed,

that's a great question and indeed I have used a little trick in the solution to get the job done. The first set of lines is easy. I first check whether country == "Maximum" and whether continent doesn't have missing values just like you do. And then, I do another check of country == "Maximum" and shift the resulting TRUE/FALSE vector by 2 (knowing that I always have two rows with the average and maximum in it). This will do the trick using only hline():

hline(
    i = ~ (
      !is.na(continent) | 
        country == 'Maximum' | 
        lead(country == 'Maximum', 2, default = FALSE)
    )
  )