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.
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).
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.
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.
# 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
boxplot(growth ~ PlantSex,
data = Poa,
col = c("purple", "lavender"),
main = "Plant Growth by Sex",
xlab = "Plant Sex",
ylab = "Growth (log final/initial leaf length)")
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.