Solution to yesterday’s challenge.
Welcome to Day 21 of “Viz with Me in R”!
Let’s dive into the solution to yesterday’s exercise.
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.
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()
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()
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.
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()
You can also add annotations to indicate which panel represents what if you’d like.
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! 🌟
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} }