Grouped Barplot With Error Bars in R

Barplots are an essential and widely used visualization tool for several reasons. They are excellent choices for visualizing the relationship between numeric and categorical variables, making it easy to understand the differences between categories or groups. They can represent counts, frequencies, proportions, and percentages, making them versatile for various data types.
In R, we have the ability to analyze categorical data and represent it through barplots. Nevertheless, beginners venturing into R programming often encounter challenges when it comes to estimating means, standard errors, and creating grouped barplots with Error Bars. To counter these challenges one must have a basic understanding of data types, data structures, and the operations required for data analysis.
In this tutorial, we start by creating a simple dataset to understand different kinds of data types and how to convert them into suitable formats for data analysis. Then, we will delve into the process of estimating means and standard errors. Subsequently, we will proceed to create a grouped barplot with error bars. To assist beginners, I will meticulously dissect the code, step by step, to ensure a thorough understanding of the programming process.
I assume our readers are familiar with the process of installing and loading R packages. If not, please refer to STHDA for guidance.
Let's jump right into creating the dataset, modifying it, and visualizing it. Begin by loading the necessary libraries as demonstrated below.
library(tidyverse)
library(ggthemes)
library(ggpubr)
The tidyverse is a core collection of R packages, which includes [dplyr](https://r4ds.hadley.nz/data-transform.html)
for data manipulation and analysis, and [Ggplot2](https://r4ds.hadley.nz/data-visualize)
for data visualization. The ggthemes provides alternative themes and theme components to style ggplot2 plots. The ggpubr offers ggsave()
to save plots with customizable dimensions, resolution, and file formats. To explore these packages further, simply click on the hyperlinks provided.
Creating a Dataframe:
In the code below data.frame()
function initiates a dataframe named df
with three columns: Animals
, Category
, and Counts
.
df <- data.frame(Animals = c("cats", "dogs", "cows",
"cats", "dogs", "cows",
"cats", "dogs", "cows",
"cats", "dogs", "cows"),
Category = c("Domestic", "Domestic", "Domestic",
"Domestic","Domestic","Domestic",
"Stray", "Stray", "Stray",
"Stray", "Stray", "Stray"),
Counts = c("28", "22", "45",
"30", "18", "43",
"40", "65","10",
"35", "72", "8"))