Site icon R-bloggers

Horizontal Boxplots in R using the Palmer Penguins Data Set

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

Introduction

Boxplots are a great way to visualize the distribution of a numerical variable. They show the median, quartiles, and outliers of the data, and can be used to compare the distributions of multiple groups.

Horizontal boxplots are a variant of the traditional boxplot, where the x-axis is horizontal and the y-axis is vertical. This can be useful for visualizing data where the x-axis variable is categorical, such as species or treatment group.

< section id="creating-horizontal-boxplots-in-base-r" class="level1">

Creating horizontal boxplots in base R

To create a horizontal boxplot in base R, we can use the boxplot() function with the horizontal argument set to TRUE.

library(palmerpenguins)
# Create a horizontal boxplot of bill length by species
boxplot(
  bill_length_mm ~ species,
  data = penguins,
  horizontal = TRUE,
  main = "Bill length by species in Palmer penguins",
  xlab = "Bill length (mm)",
  ylab = "Species"
)

This code will produce a horizontal boxplot with one box for each species of penguin. The boxes show the median, quartiles, and outliers of the bill length data for each species.

< section id="creating-horizontal-boxplots-in-ggplot2" class="level1">

Creating horizontal boxplots in ggplot2

To create a horizontal boxplot in ggplot2, we can use the geom_boxplot() function with the coord_flip() function.

library(ggplot2)

# Create a horizontal boxplot of bill length by species using ggplot2
ggplot(penguins, aes(x = bill_length_mm, y = species)) +
  geom_boxplot() +
  labs(
    title = "Bill length by species in Palmer penguins",
    x = "Bill length (mm)",
    y = "Species"
  )

This code will produce a horizontal boxplot that is similar to the one produced by the base R code above. However, the ggplot2 code is more flexible and allows us to customize the appearance of the plot more easily.

< section id="encouragement" class="level1">

Encouragement

I encourage you to try creating horizontal boxplots for your own data. You can use the Palmer penguins data set as a starting point, or you can use your own data. Experiment with different options to customize the appearance of your plots.

Here are some ideas for things to try:

I hope this blog post has been helpful. If you have any questions, please leave a comment below.

< section id="conclusion" class="level1">

Conclusion

Horizontal boxplots can be a useful way to visualize the distribution of data when the x-axis variable is categorical. They are easy to create in both base R and ggplot2.

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version