Today, we will finish our bar charts and work on some exercises.
Welcome to Day 10 of “Viz with Me”!
Thank you for joining me on this journey so far. Are you as excited as I am to see the bar plots after counting the penguins by species yesterday? Let’s dive right in!
Goals for today: To draw a bar plot representing the number of penguin species from the palmerpenguins
dataset.
First, let’s revisit the code we wrote yesterday:
# A tibble: 3 × 2
species n
<fct> <int>
1 Adelie 152
2 Chinstrap 68
3 Gentoo 124
Now, we need to call ggplot.
Remember, the output from the earlier code will serve as input for the aesthetics (aes
) in our ggplot
function today. Here’s how we do it:
Feel free to add your titles and any other customizations you’d like. Here’s a refined version that includes labels:
Now, after going through the ordeal of counting and then inputting the data into ggplot
, you might wonder why we used geom_col()
instead of geom_bar()
. In fact geom_bar()
can be quite handy, even without counting the penguins first. See here:
But sometimes, geom_bar()
gets finicky and will require you to add, stat = "count"
within it. This is why I prefer geom_col()
for simplicity even though I have to count the penguins first.
Feel free to try both and pick what works best for you!
Did you notice that I left the x-axis label as ""
? This ensures that no title is added to the x-axis, as the bars are already categorized by species.
Since today is Day 10, I’d like to leave you with some exercise questions to ponder and recreate:
Try creating a bar plot for the island
variable in the palmerpenguins dataset. This is another categorical variable you can explore.
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.
I will see you tomorrow with the solutions and another exciting topic: changing themes!
Jump to tomorrow’s blog for the solutions.
If you are up to some advanced challenges, explore here for adding sample sizes to your bar plots: Adding sample sizes to bar plots and flipping the bars!
For attribution, please cite this work as
Soundararajan (2024, Oct. 10). My R Space: Day 10 of viz with me. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-10-10-day-10-of-viz-with-me/
BibTeX citation
@misc{soundararajan2024day, author = {Soundararajan, Soundarya}, title = {My R Space: Day 10 of viz with me}, url = {https://github.com/soundarya24/SoundBlog/posts/2024-10-10-day-10-of-viz-with-me/}, year = {2024} }