library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
#load in the data
{data(PlantGrowth)}
#this is a dataset of plant weight values, with a group factor
head(PlantGrowth)
## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
#there are 30 rows and 3 levels to the group factor. There is a control
#and two treatment groups
str(PlantGrowth)
## 'data.frame': 30 obs. of 2 variables:
## $ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
## $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
#there are 10 values per group and roughly normal distribution of weights
summary(PlantGrowth)
## weight group
## Min. :3.590 ctrl:10
## 1st Qu.:4.550 trt1:10
## Median :5.155 trt2:10
## Mean :5.073
## 3rd Qu.:5.530
## Max. :6.310
hist(PlantGrowth$weight)

#quickly view differences between group distributions
plot(PlantGrowth$weight~PlantGrowth$group)

#create a data.frame object with mean and sd
averages <- PlantGrowth %>%
group_by(group) %>%
summarise(mean = mean(weight), sd = sd(weight))
#plot the basic bar graph with the means and standard deviation error bars
#here the highest average weight is in treatment 2 plants
ggplot(data = averages, aes(x = group, y = mean)) +
geom_col() +
geom_errorbar(ymin = averages$mean-averages$sd, ymax = averages$mean+averages$sd) +
ylim(0, 6) +
ylab("mean weight")
