# R Studio Tutorial Exercise ## Problem 1 **Using the ChickWeights dataset, create boxplots showing log10 weights only on the first, tenth, and last days of the experiment.** First, load and view the data ``` data("ChickWeight") dim(ChickWeight) names(ChickWeight) head(ChickWeight) ``` Then change the column names for easy use! ``` colnames(ChickWeight) <- c("weight", "days", "individual", "diet") ``` ### Problem 1 Data To use only the first, tenth, and last days of the experiment, I created a new variable `new.days`. (I guessed you meant day=0 for the first day since day=1 does not exist) ``` day21<-which(ChickWeight$days == max(ChickWeight$days)) day0<-which(ChickWeight$days == 0) day10<-which(ChickWeight$days == 10) justdays <- c(day0, day10, day21) ChickWeight[justdays,] new.days<-ChickWeight[justdays,] ``` This creates variables (`day21`,`day10`,`day0`) that just contain the different days, which can be combined into `justdays` and then used to define `new.days`, which is a variable that contains all the information from the `ChickWeight` matrix, but for only the specified days. ### Boxplot ``` boxplot(log10(new.days$weight) ~ new.days$days, names=c("First Day","Day 1","Day 21"), xlab="Days", ylab="log10 Weights", main="Problem 1 Boxplot") ``` ![image alt](https://bi376.colby.edu/graphics/cf648e5c-4d50-4716-a1f5-333071f55d22.png) This creates a boxplot with weights of chicks on the different days! ### Challenge 1a **Have the figure display results for each diet separately.** ``` library(ggplot2) C1a <- ggplot(new.days, aes(x=diet, y=log10(weight))) + geom_boxplot() + geom_jitter(position = position_jitter(width = 0.2, height = 0))+ ggtitle("Weights of chicks based on different diets") + xlab("Artifical diet number") + ylab(expression(paste(log[10]," chick weight (g)", sep=""))) + theme_bw() C1a ``` This plots this ggplot: ![image alt](https://bi376.colby.edu/graphics/b0e365c6-d9e7-4be2-947b-d0f52253c55a.png) The plot shows the weights of chicks based on different diets but just the chicks from the first day, day 10, and last day. ***Oops this should have actually been done differently, with ggplot to make it so that there are 4 different graphs o.o*** ***+the images don't load unless I am logged onto the BI376 rstudio server*** ### Challenge 1b **With the figure features above, communicate both the individual values and the distribution of those values to a viewer.** This can be done by plotting the same data with jitters in a violin plot! The violin plot shows the distribution more clearly than a boxplot while the jitters show the individuals as points on the plot. ``` C1b <- ggplot(new.days, aes(x=diet, y=log10(weight))) + geom_violin(trim = FALSE) + geom_jitter(position = position_jitter(width = 0.2, height = 0))+ ggtitle("Weights of chicks based on different diets") + xlab("Artifical diet number") + ylab(expression(paste(log[10]," chick weight (g)", sep=""))) + theme_bw() C1b ``` ![image alt](https://bi376.colby.edu/graphics/02c0a49c-5293-4fff-9242-f214d696796b.png) ## Problem 2 **Randomly generate two vectors of 100 values each. Plot them against one another and draw a linear trend line through the group of points.** ``` a <- rnorm(n=100) b <-runif(n=100) plot(a,b) trendline<-lm(b~a) abline(trendline) ``` Vectors `a` and `b` of 100 values were created and plotted. The line was saved as `trendline` for easy use later on. ![image alt](https://bi376.colby.edu/graphics/a82bffc9-b130-4114-b514-18344a6eaa92.png) ### Challenge 2a **Find the slope of the line you’ve drawn above.** ``` trendline #(Intercept) a #0.47722 0.04658 ``` This gives both the intercept (0.47722) and slope a (0.04658). ### Challenge 2b **Add text to the plot, showing the slope.** ``` equation<- ("y=0.04658x + 0.47722") mtext(equation) ``` Using what was learned from challenge 2a, the equation in the form of y=mx+b can be made by plugging in the intercept and slope. Using `mtext` allows us to put the text we want on the plot. ![image alt](https://bi376.colby.edu/graphics/ab5cca56-daf8-4803-aac5-0f8a2092fda5.png) **Done!**