Day 11 of viz with me

Data viz Beginner ggplot count geom_bar geom_col themes DataViz Challenge

Today we will create polished charts.

Soundarya Soundararajan true
2024-10-11

Welcome to Day 11 of “Viz with Me”!

Yesterday we made some bar plots

Goals for today:

  1. Solutions for yesterday’s exercises.

  2. Changing themes to create more polished looking charts.

Solution for exercise 1

  1. Try creating a bar plot for the island variable in the palmerpenguins dataset. This is another categorical variable you can explore.
library(palmerpenguins)
library(tidyverse)
penguins %>%
  count(island) %>%
  ggplot(aes(x = island, y = n)) +
  geom_col()+
  labs(title = "Number of penguins on each island",
       x = "Island",
       y = "Number of penguins")

Now this is a simple default bar plot. But what if you want to make it look more polished?

Changing themes

Themes in ggplot2 allow you to change the appearance of your plots. You can change the background, gridlines, text, and more.

Here are some popular themes you can use:

theme_minimal() - A minimalistic theme with no gridlines.

Let’s try this theme on our plot.

penguins %>%
  count(island) %>%
  ggplot(aes(x = island, y = n)) +
  geom_col()+
  labs(title = "Number of penguins on each island",
       x = "Island",
       y = "Number of penguins")+
  theme_minimal() # Add this line to change the theme

You can also try other themes like theme_light(), theme_dark(), theme_bw(), etc. Let me know which one you like the best!

Solution for exercise 2

  1. Draw a scatter plot for bill depth vs. bill length in the palmerpenguins dataset. See if you can add labels and make a complete graph using what we’ve learned so far.
penguins %>%
  ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
  geom_point()+
  labs(title = "Bill depth vs. Bill length",
       x = "Bill length (mm)",
       y = "Bill depth (mm)")

Let’s try changing the theme for this plot as well. But, we will use a different theme this time.

penguins %>%
  ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
  geom_point()+
  labs(title = "Bill depth vs. Bill length",
       x = "Bill length (mm)",
       y = "Bill depth (mm)")+
  theme_classic()

How does this theme look?

Take a look at all the themes here which are part of the ggplot2 package. If you are fan of dark themes, check out the theme_dark().

If you are up to advancing your plots with more themes, try using the hrbrthemes (Rudis 2024). It has a wide variety of themes to choose from.

I shall see you tomorrow with more exciting stuff!

Rudis, Bob. 2024. “Hrbrthemes: Additional Themes, Theme Components and Utilities for ’Ggplot2’.” https://CRAN.R-project.org/package=hrbrthemes.

References

Citation

For attribution, please cite this work as

Soundararajan (2024, Oct. 11). My R Space: Day 11 of viz with me. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-10-10-day-11-of-viz-with-me/

BibTeX citation

@misc{soundararajan2024day,
  author = {Soundararajan, Soundarya},
  title = {My R Space: Day 11 of viz with me},
  url = {https://github.com/soundarya24/SoundBlog/posts/2024-10-10-day-11-of-viz-with-me/},
  year = {2024}
}