Skip to content
Coming soon: Ally. Your guide to the world of AI and R. Learn More →
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: