-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots_iris_mean.R
32 lines (30 loc) · 1.38 KB
/
plots_iris_mean.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
library(tidyr)
data <- iris
## Agreggate data and calculate mean
data_mean <- aggregate(data[, 1:4], list(data$Species), mean)
clean_mean <- data_mean %>% pivot_longer(cols = c("Sepal.Length", "Sepal.Width",
"Petal.Length","Petal.Width"),
names_to = "Character")
## Agreggate data and calculate standard deviation
data_sd <- aggregate(data[, 1:4], list(data$Species), sd)
clean_sd <- data_sd %>% pivot_longer(cols = c("Sepal.Length", "Sepal.Width",
"Petal.Length","Petal.Width"),
names_to = "Character")
## Bind into one dataframe
statistics <- cbind(clean_mean,clean_sd[,3])
colnames(statistics) <- c("Species","Character","Mean","SD")
## Plot mean with error bar
library(ggplot2)
p <-ggplot(statistics, aes(x=Character, y=Mean, fill=Species)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=.2,
position=position_dodge(.9))
p
## Plot dispersion between petal length and width
petal <- ggplot(data, aes(x=Petal.Length, y= Petal.Width, color=Species)) +
geom_point()
petal
## Plot dipsersion between sepal length and width
sepal <- ggplot(data, aes(x=Sepal.Length, y= Sepal.Width, color=Species)) +
geom_point()
sepal