# Draw ggplot2 Plot with Mean & Standard Deviation by Category in R (Example Code)

This tutorial demonstrates how to draw a plot with mean and standard deviation by category in the R programming language.

## Preparing the Example

```data(iris) # Loading example data frame head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa```
```install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr```
```iris_group <- iris %>% # Calculate means & standard deviations group_by(Species) %>% summarise_at(vars(Sepal.Length), list(mn = mean, sd = sd)) iris_group # # A tibble: 3 × 3 # Species mn sd # <fct> <dbl> <dbl> # 1 setosa 5.01 0.352 # 2 versicolor 5.94 0.516 # 3 virginica 6.59 0.636```

## Example: Draw ggplot2 Plot with Means & Standard Deviations by Group

```install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package```
```ggplot(iris_group, # Plot means & standard deviations for each category aes(x = Species, y = mn)) + geom_point() + geom_errorbar(aes(ymin = mn - sd, ymax = mn + sd))```

## Related Articles

Below, you may find some further resources that are related to the content of this post.