Skip to content
R for the Rest of Us Logo

Making Beautiful Tables with R

Add your own ggplot

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.

Let’s redo our own density plots.

single_species_weights <- penguin_mass$body_masses[[1]]

gg_density_plot <- function(weights) {
  ggplot() +
    stat_density(aes(x = weights), fill = 'dodgerblue4', col = 'grey20') +
    coord_cartesian(xlim = range(penguins$body_mass_g)) +
    theme_void()
}


list_of_ggplots <- map(penguin_mass$body_masses, gg_density_plot)


penguin_mass |> 
  flextable() |> 
  compose(
    j = 'body_masses',
    value = as_paragraph(
      gg_chunk(
        value = list_of_ggplots,
        width = 3,
        height = 1
      )
    )
  )
gg_density_box_plot <- function(weights) {
  ggplot(mapping = aes(x = weights)) +
    stat_density(fill = 'dodgerblue4', col = 'grey20') +
    geom_boxplot(width = 0.0005, position = position_nudge(y = -0.0005)) +
    coord_cartesian(xlim = range(penguins$body_mass_g)) +
    theme_void()
}

list_of_ggplots <- map(penguin_mass$body_masses, gg_density_box_plot)


penguin_mass |> 
  flextable() |> 
  compose(
    j = 'body_masses',
    value = as_paragraph(
      gg_chunk(
        value = list_of_ggplots,
        width = 3,
        height = 1
      )
    )
  )

Your Turn

Redo the table from the previous exercise but use ggplot to draw the sparklines. This time, also add small dots at beginning and end of each sparkline.

Hint: If you want, you can manually change the width of the sparkline column with width() after autofit() was called.

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.

Majeed Oladokun

Majeed Oladokun • August 27, 2024

Hi,

I need help on adding the sparkline as ggplot object. I was able to add the sparklines and the dots as required. However, the dots are being partly cut-off in the cell. Below are excerpts from the code I used.

# Define plot function
gg_sparkline_plot <- function(life_exp) {
  
  # define x-variable
  xvar = seq_along(life_exp) 
  
  ggplot() +
    geom_line(aes(x = xvar, y = life_exp), colour = 'dodgerblue4', linewidth = 1.2) +
    geom_point(
      aes(x = c(first(xvar), last(xvar)),
          y = c(first(life_exp), last(life_exp))), 
      size = 3,
      colour = 'dodgerblue4') +
    coord_cartesian(clip = "off") +
    theme_void()
}

# Data used for table
# Note life_expectancies_wider is the wide format of gapminder data
# after subseting it for the selected countries and year. 
life_expectancies <- gapminder::gapminder |>
  janitor::clean_names()

selected_countries_vector <- c(
  'Egypt', 'Sierra Leone', 'Nicaragua', 
  'Jamaica', 'Syria', 'Singapore',
  'Netherlands', 'United Kingdom', 
  'New Zealand', 'Australia'
)

life_expectancies_wider <- life_expectancies |>
  filter(country %in% selected_countries_vector) |>
  filter(year %in% seq(1957, 2007, 10)) |>
  select(country:life_exp) |>
  pivot_wider(
    names_from = c(year),
    names_prefix = "year",
    values_from = life_exp
  ) |>
  relocate(continent, .before = country) |>
  arrange(continent, country) 

table_dat <- life_expectancies_wider |> 
  bind_rows(life_expectancies_summary_stats) |> 
  arrange(continent) |>
  rowwise() %>%
  # nest all the life_exp from 1957:2007
  mutate(trends = list(c_across(year1957:year2007))) |>
  ungroup() 

# Create flextable 
table_dat |> 
  mutate( trends = map(trends, gg_sparkline_plot) ) |>
  flextable() |> 
  compose(
    j = 'trends',
    value = as_paragraph(
      gg_chunk(
        value = trends,
        width = 1,
        height = 0.3
      )
    )
  ) |>
  # style(j = "trends", pr_c = fp_cell(margin = 5)) |>
  autofit() |>
  width(j = "trends", width = 1.2, unit = "cm")

The code runs but the dots are being chopped off within the table. I attemped to use the commented # style(j = "trends", pr_c = fp_cell(margin = 5)) to check if increasing the cell margin will correct the problem, but to no avail.

I am wonderng if you could share the TRICK to correct the problem.

Thank you.

Albert Rapp

Albert Rapp • August 28, 2024

Hi Majeed,

try making the underlying chart image have more spacing. To do so, you could expand the x- and y-axis spacing by adding

    scale_y_continuous(expand = expansion(mult = 0.5)) +
    scale_x_continuous(expand = expansion(mult = 0.1))

to your ggplot.

Best, Albert