---
# System prepended metadata

title: Exercise 1. Markdown & R (BI377)

---



### Problem 1
Using the `ChickWeights` dataset, create boxplots showing log10 weights only on days zero (0),10, and 20 of the experiment. The plot should have proper axis labels.

```R
data("ChickWeight")

subset_data <- ChickWeight[ChickWeight$Time %in% c(0, 10, 20), ]

subset_data$log10_weight <- log10(subset_data$weight)

boxplot(log10_weight ~ Time, data = subset_data,
        xlab = "Day",
        ylab = "Log10 Weight",
        main = "Log10 Chick Weights on Days 0, 10, and 20",
        col = c('gold'))
```

##### Log10 Chick Weights (Problem 1)
![Screen Shot 2024-09-12 at 5.01.03 PM](https://hackmd.io/_uploads/H1rjLRxa0.png)


#### Challenge 1a
Have the figure display results for each diet separately.

```R
data("ChickWeight")

subset_data <- ChickWeight[ChickWeight$Time %in% c(0, 10, 20), ]

subset_data$log10_weight <- log10(subset_data$weight)

boxplot(log10_weight ~ interaction(Time, Diet), data = subset_data, 
        xlab = "Day and Diet (day.diet)", 
        ylab = "log 10 weight", 
        main = "Log10 Chick Weights by Diet on Days 0, 10, and 20",
        col = c("blue", "green", "red", "gold"))
```

##### Challenge 1a
![Screen Shot 2024-09-12 at 5.08.48 PM](https://hackmd.io/_uploads/SywquRlT0.png)

#### Challenge 1b
With the figure features above, communicate both the individual values and the distribution of those values to a viewer.

### 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.

```R
randomvector1 <- runif(100, min = 0, max = 1000)
randomvector2 <- runif(100, min = 0, max = 500)

plot(randomvector1, randomvector2, 
     xlab = 'randomly generated vector 1',
     ylab = 'randomly generated vector 2',
     main = 'Random Vector Scatter Plot'
)

model <- lm(randomvector2 ~ randomvector1)
abline(model, col = 'red', lwd = 2)
```
#### Problem 2
![Screen Shot 2024-09-13 at 11.22.44 AM](https://hackmd.io/_uploads/BJpRuA-6A.png)


#### Challenge 2a
Find the slope of the line you've drawn above.

You can find the slope of a line using coef():
```R
randomvector1 <- runif(100, min = 0, max = 1000)
randomvector2 <- runif(100, min = 0, max = 500)

plot(randomvector1, randomvector2, 
     xlab = 'randomly generated vector 1',
     ylab = 'randomly generated vector 2',
     main = 'Random Vector Scatter Plot'
)

model <- lm(randomvector2 ~ randomvector1)
cf <- coef(model)
print(cf)
abline(model, col = 'red', lwd = 2)

slope <- cf[2]
print(slope)
```
The slope was 0.1298968.

#### Challenge 2b
Add text to the plot, showing the slope.

```R
randomvector1 <- runif(100, min = 0, max = 1000)
randomvector2 <- runif(100, min = 0, max = 500)

plot(randomvector1, randomvector2, 
     xlab = 'randomly generated vector 1',
     ylab = 'randomly generated vector 2',
     main = 'Random Vector Scatter Plot'
)

model <- lm(randomvector2 ~ randomvector1)
cf <- coef(model)
abline(model, col = 'red', lwd = 2)
text(300, 145,"slope = 0.1298968")

slope <- cf[2]
print(slope)
```
##### Challenge 2b
![Screen Shot 2024-09-13 at 11.41.04 AM](https://hackmd.io/_uploads/SJyNaCbaR.png)

#### Challenge 2c
Make that text with the slope's value held in a variable, rather than re-typing the digits by hand. And display only 4 significant digits.

```R
randomvector1 <- runif(100, min = 0, max = 1000)
randomvector2 <- runif(100, min = 0, max = 500)

plot(randomvector1, randomvector2, 
     xlab = 'randomly generated vector 1',
     ylab = 'randomly generated vector 2',
     main = 'Random Vector Scatter Plot'
)

model <- lm(randomvector2 ~ randomvector1)
abline(model, col = 'red', lwd = 2)
cf <- coef(model)
slope <- cf[2]

text(300, 145, labels = paste("Slope=", signif(slope,4)))
```
##### Challenge 2c
![Screen Shot 2024-09-13 at 11.51.45 AM](https://hackmd.io/_uploads/BkShk1GpC.png)

#### Challenge 2d
Do the same thing using ggplot.

```R
library(ggplot2)

randomvector1 <- runif(100, min = 0, max = 1000)
randomvector2 <- runif(100, min = 0, max = 500)

dataframe <- data.frame(randomvector1, randomvector2)

model <- lm(randomvector2 ~ randomvector1)
cf <- coef(model)
slope <- cf[2]

ggplot(dataframe, aes(x = randomvector1, y = randomvector2)) + 
  geom_point() +
  labs(
    x = 'randomly generated vector 1',
    y = 'randomly generated vector 2',
    title = 'Random Vector Scatter Plot'
  ) + 
  geom_smooth(method = 'lm', se = FALSE, color = 'red', lwd = 2) +
  annotate('text', x = 300, y = 145, label = paste("Slope = ", signif(slope, 4)))
```

##### Challenge 2d
![Screen Shot 2024-09-13 at 12.06.19 PM](https://hackmd.io/_uploads/HkxMXkzaR.png)

