# BI376 Lab 9/14 *Collaborators: Taylor and Youmian* ## 1) I used ggplot to create boxplots of chick weight by day: ![](https://i.imgur.com/B4Rfkmu.png) My code was a pretty standard ggplot command. I used the ```%in%``` operator to subset only days 0, 10, and 21. ``` p <- ggplot(subset(ChickWeight, Time %in% c(0,10,21)), aes(x=as.factor(Time), y=log10(as.numeric(weight)))) + geom_boxplot() + ggtitle("Chick Weight by Day") + xlab('Day') + ylab('log10 Weight') p ``` **1a)** I separated the diet groups by including a ```facet_wrap```. ``` p2 <- ggplot(subset(ChickWeight, Time %in% c(0,10,21)), aes(x=as.factor(Time), y=log10(as.numeric(weight)))) + geom_boxplot() + facet_wrap(.~Diet) + ggtitle("Chick Weight by Diet and Day") + xlab('Day') + ylab('log10 Weight') p2 ``` ![](https://i.imgur.com/alGWtm1.png) **1b)** To better indicate individual values and distributions, I changed by plots to violins and added jittered points. ``` p3 <- ggplot(subset(ChickWeight, Time %in% c(0,10,21)), aes(x=as.factor(Time), y=log10(as.numeric(weight)))) + geom_violin(color = 'grey', size = 1, trim = FALSE, alpha = 0.75, draw_quantiles = 0.5) + facet_wrap(.~Diet) + geom_jitter(position = position_jitter(width = 0.2, height = 0),color = 'darkblue', size=.3, alpha=0.8)+ ggtitle("Chick Weight by Diet and Day") + xlab('Day') + ylab('log10 Weight') + theme_bw() p3 ``` ![](https://i.imgur.com/5kgGyi2.png) ## 2) I used ```runif``` and ```rnorm``` to generate my random values. ``` a <- runif(n=100, min = 0, max = 100) b <- rnorm(n=100, mean = 50, sd = 10) p4 <- ggplot(data.frame(a,b),aes(x=a, y=b)) + theme_bw() + geom_point(size = 2, alpha = 0.5) + stat_smooth(method=lm) + ggtitle("Random Plot") p4 ``` ![](https://i.imgur.com/fB56M8h.png) **2a, 2b)** I used the ```lm``` command to find the slope and added text to the plot with an ```annotate``` call. ``` p5 <- p4 + annotate(geom = "text", x =30, y = 63, label = paste('slope:', round(lm(b ~a)[["coefficients"]][["a"]],5)), hjust = 0, vjust = 1, size = 4) ``` ![](https://i.imgur.com/tduczjL.png)