Day 21 of viz with me

Data viz Beginner ggplot iris DataViz Challenge

Solution to yesterday’s challenge.

Soundarya Soundararajan true
2024-10-21

Welcome to Day 21 of “Viz with Me in R”!

Let’s dive into the solution to yesterday’s exercise.

1. Choosing any dataset of your interest.

You can use datasets like Palmer Penguins, Gapminder, or your own dataset. For today, I’ll be working with the Iris dataset.

To familiarize yourself with the Iris dataset type ?iris in the console and hit enter. A help page will open up with details about the dataset.

2. Drawing a Scatterplot

library(tidyverse)

iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "Sepal Length", 
       y = "Sepal Width", 
       title = "Sepal Length vs Width", 
       caption = "Iris Dataset") +
  theme_classic()

3. My Plot of Choice

Here, I’ll use a boxplot to compare Sepal Length across species.

iris %>%
  ggplot(aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  labs(x = "Species", 
       y = "Sepal Length", 
       title = "Sepal Length by Species", 
       caption = "Iris Dataset") +
  theme_classic()

4. Polishing up

Ensure Your Labels, Titles, and Captions are Clear Remember to always label your axes and include a title, subtitle, or caption where necessary. Also, feel free to change the themes to suit your visual preference.

5. Prelude to patching

Store Both Plots as Objects. This way, you can use them later or combine them.

scatter_plot <- iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "Sepal Length", 
       y = "Sepal Width", 
       title = "Sepal Length vs Width", 
       caption = "Iris Dataset") +
  theme_classic()

boxplot <- iris %>%
  ggplot(aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  labs(x = "Species", 
       y = "Sepal Length", 
       title = "Sepal Length by Species", 
       caption = "Iris Dataset") +
  theme_classic()

6. Combining and Presenting the Output Using patchwork

library(patchwork)
scatter_plot + boxplot

You can also add annotations to indicate which panel represents what if you’d like.

7. Adding Annotations

combinedplots <- scatter_plot + boxplot

combinedplots + plot_annotation(tag_levels = "A")

Beautiful, isn’t it?

I will be back tomorrow with a new challenge. Until then, happy coding! 🌟

Citation

For attribution, please cite this work as

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

BibTeX citation

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