Get data
library(tidyverse)
## ── Attaching packages ── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(cowplot)
##
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
## default ggplot2 theme anymore. To recover the previous
## behavior, execute:
## theme_set(theme_cowplot())
## ********************************************************
library(calecopal)
heights<-read.table("~/Desktop/heights.txt",header=T)
heights
## height rep sex
## 1 52 1 XX
## 2 56 1 XX
## 3 58 2 XX
## 4 59 2 XX
## 5 60 5 XX
## 6 61 1 XX
## 7 62 11 XX
## 8 63 12 XX
## 9 64 6 XX
## 10 65 6 XX
## 11 66 13 XX
## 12 67 6 XX
## 13 68 7 XX
## 14 69 3 XX
## 15 70 1 XX
## 16 71 1 XX
## 17 72 1 XX
## 18 73 1 XX
## 19 59 2 XY
## 20 60 2 XY
## 21 64 1 XY
## 22 65 1 XY
## 23 66 2 XY
## 24 67 7 XY
## 25 68 5 XY
## 26 69 2 XY
## 27 70 2 XY
## 28 71 1 XY
## 29 72 3 XY
## 30 73 3 XY
## 31 74 1 XY
## 32 75 1 XY
Replicate heights, make new data frame
xx<-filter(heights,sex =="XX") %>% select(height)
xxrep<-filter(heights,sex =="XX") %>% select(rep)
xx<-rep(xx$height,xxrep$rep)
xy<-filter(heights,sex =="XY") %>% select(height)
xyrep<-filter(heights,sex =="XY") %>% select(rep)
xy<-rep(xy$height,xyrep$rep)
new_heights<-data.frame(c(xx,xy),c(rep("XX",length(xx)),rep("XY",length(xy))))
colnames(new_heights)=c("height","sex")
head(new_heights)
## height sex
## 1 52 XX
## 2 56 XX
## 3 58 XX
## 4 58 XX
## 5 59 XX
## 6 59 XX
Get normals
group_by(heights,sex) %>%
summarize(mean(height),sd(height))
## # A tibble: 2 x 3
## sex `mean(height)` `sd(height)`
## <fct> <dbl> <dbl>
## 1 XX 64.2 5.86
## 2 XY 68.1 4.92
Plot data
ggplot(new_heights,aes(x=height,fill=sex))+theme_bw()+
geom_histogram(position="identity",color="black", aes(y=..density..),alpha=0.5,breaks=50:76)+
scale_fill_manual(values = cal_palette("superbloom3"))+
stat_function(fun = dnorm, args = list(mean = 64.22, sd = 5.86),col=cal_palette("superbloom3")[1])+
stat_function(fun = dnorm, args = list(mean = 68.07, sd = 4.92),col=cal_palette("superbloom3")[2])
xx
## [1] 52 56 58 58 59 59 60 60 60 60 60 61 62 62 62 62 62 62 62 62 62 62 62
## [24] 63 63 63 63 63 63 63 63 63 63 63 63 64 64 64 64 64 64 65 65 65 65 65
## [47] 65 66 66 66 66 66 66 66 66 66 66 66 66 66 67 67 67 67 67 67 68 68 68
## [70] 68 68 68 68 69 69 69 70 71 72 73