Instructions: This assignment is intended to give you experience with some common plotting functions (hist(), boxplot) and challenge you to fine-tune your plots to make them a little nicer than the defaults. This will also give you experience working in RMarkdown. Upload your compiled .html document by the deadline. Be sure to add your name above.

  1. In this R chunk called “setup”, read in the Poa arachnifera data, and load any libraries you might need. Note that the chunk is set so that the code inside it is not printed in the compiled document (echo=F).

  2. In a new chunk of code, create the variable “growth” that is the log ratio of final:initial leaf length. Set the chunk so that this code is not printed in your compiled document (echo=F). Give this chunk a name.

    1. Use the hist() function to create two histograms of the growth variable, one that uses a binning interval of 0.25 and the other using a binning interval of 0.75. Display the histograms so that they appear side by side, in one row and two columns. Change the variable names and plot titles away from the defaults to make them more natural to interpret for a new reader. Give this chunk a name.

Hint: use the code

par(mfrow=c(1,2))

lower <- floor(min(Poa$growth, na.rm = TRUE))
upper <- ceiling(max(Poa$growth, na.rm = TRUE))

par(mfrow = c(1, 2))

hist(Poa$growth,
     breaks = seq(lower, upper + 0.25, by = 0.25),
     main = "Plant Growth: 0.25-Unit Bins",
     xlab = "Growth (log final/initial leaf length)",
     ylab = "Number of Plants",
     col = "seagreen")

hist(Poa$growth,
     breaks = seq(lower, upper + 0.75, by = 0.75),
     main = "Plant Growth: 0.75-Unit Bins",
     xlab = "Growth (log final/initial leaf length)",
     ylab = "Number of Plants",
     col = "green")
par(mfrow = c(1, 1))

preceding the hist() functions to specify that you are plotting two panels in one row and two columns.

    1. Do you think this distribution is bimodal? Why or why not?n ``` This distribution is not clearly bimodal because the 0.25-unit histogram shows two peaks, while the 0.75-unit histogram does not. This suggests the appearance depends on the bin width
  1. Calculate the standard deviation of the growth variable “by hand”, using this formula. Check that your answer matches the sd() function. Beware of NAs in the growth data.
    \[s = \sqrt{\frac{1}{(n-1)} \sum_{i=1}^{n} (x_i - \bar{x})^2 }\]
# The dead plant and or N/A's before calculating
growth <- Poa$growth[!is.na(Poa$growth)]

# Count the observations and calculate their mean
n <- length(growth)
mean_growth <- mean(growth)

# Calculate each difference from the mean, then square it
squared_differences <- (growth - mean_growth)^2

# Add the squared differences, divide by n - 1, and take the square root
sd_by_hand <- sqrt(sum(squared_differences) / (n - 1))

# Display answers
sd_by_hand
## [1] 0.6715849
sd(Poa$growth, na.rm = TRUE)
## [1] 0.6715849
    1. Create a boxplot comparing growth between females and males (use the boxplot() function). Give females and males different colors. Change the variable names away from the defaults to make them more natural to interpret for a new reader. Give this chunk a name.
boxplot(growth ~ PlantSex,
        data = Poa,
        col = c("purple", "lavender"),
        main = "Plant Growth by Sex",
        xlab = "Plant Sex",
        ylab = "Growth (log final/initial leaf length)")

    1. Do you think females and males have different growth rates? Why or why not? ```Yes it seems like they have a higher growth rate because there median is higher.
  1. Make either of the above plots (histogram or boxplot) as a ggplot instead of a base R plot. If your ggplot throws a warning message (e.g., because there are missing values), use the chunk options to suppress this message.
library(ggplot2)

ggplot(Poa, aes(x = PlantSex, y = growth, fill = PlantSex)) +
  geom_boxplot() +
  scale_fill_manual(values = c("Female" = "purple", "Male" = "lavender")) +
  labs(title = "Plant Growth by Sex",
       x = "Plant Sex",
       y = "Growth (log final/initial leaf length)") +
  theme_minimal() +
  theme(legend.position = "none")

When finished, please upload both the .Rmd and .html documents.