This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Load required libraries
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.3
library(ggplot2)
library(ggpubr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
library(grid)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
# ChickWeight Plot
chi <- ggplot(ChickWeight, aes(x = Time, y = weight, color = factor(Chick))) +
geom_line(alpha = 0.9, size = 0.8) +
geom_smooth(aes(group = factor(Diet)), se = TRUE, fill = "darkgray", color = "black", size = 1.2) +
facet_wrap(~Diet, ncol = 4) +
labs(
title = "Chick Growth by Diet Type",
x = "Time (Days)",
y = "Weight (Grams)"
) +
theme_minimal() +
theme(
legend.position = "none",
plot.title = element_text(size = 14),
axis.title = element_text(size = 12),
strip.text = element_text(size = 12, face = "bold")
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(chi)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
# Load and Prepare CO2 Data
data("CO2")
CO2$TreatmentType <- interaction(CO2$Treatment, CO2$Type)
CO2$PlantType <- interaction(CO2$Plant, CO2$Type)
# Violin Plot
vn <- ggplot(CO2, aes(x = uptake, y = Treatment, fill = TreatmentType)) +
geom_jitter(position = position_jitter(0.1), aes(color = TreatmentType)) +
geom_violin(alpha = 0.5) +
scale_fill_manual(values = c("thistle", "bisque", "lightsalmon", "lightgreen")) +
scale_color_manual(values = c("thistle", "bisque", "lightsalmon", "lightgreen")) +
labs(
title = "CO2 Uptake of Grass Plants in Chilled and Unchilled Conditions",
x = "Uptake Rates (μmol/m2sec)"
) +
theme_minimal() +
theme(
plot.title.position = "plot",
plot.margin = margin(t = 50, r = 20, b = 20, l = 20)
)
print(vn)
# Line Plot
ln <- ggplot(CO2, aes(x = conc, y = uptake, fill = Treatment)) +
geom_point(aes(color = Treatment)) +
geom_smooth(aes(color = Treatment), method = "lm", alpha = 0.3) +
facet_wrap(~Type, ncol = 4) +
scale_fill_manual(values = c("red", "blue")) +
scale_color_manual(values = c("red", "blue")) +
labs(
title = "Ambient CO2 Concentration by Uptake Rates and Treatment",
x = "CO2 Concentration (mL/L)",
y = "Uptake Rates (μmol/m2sec)"
) +
theme_minimal()
print(ln)
## `geom_smooth()` using formula = 'y ~ x'
# Boxplot
bx <- ggplot(CO2, aes(x = Treatment, y = uptake, fill = Treatment)) +
geom_boxplot() +
scale_fill_manual(values = c("nonchilled" = "red", "chilled" = "blue")) +
labs(
title = "CO2 Uptake Rates by Treatment Type",
x = "Treatment",
y = "Uptake Rates (μmol/m2sec)"
) +
theme_minimal()
print(bx)
# Customize Themes for Combined Plot
vn <- vn +
theme(
plot.margin = margin(5, 5, 5, 5),
aspect.ratio = 1,
axis.title.y = element_blank(),
axis.title.x = element_text(size = 7),
plot.title = element_blank(),
legend.position = "bottom",
legend.key.size = unit(0.3, "cm"),
legend.text = element_text(size = 5),
legend.title = element_blank(),
legend.box = "horizontal",
legend.direction = "horizontal",
legend.key.height = unit(0.3, "cm"),
legend.key.width = unit(1, "cm"),
legend.box.spacing = unit(0.2, "cm")
) +
guides(fill = guide_legend(ncol = 2))
ln <- ln +
theme(
plot.margin = margin(5, 5, 5, 5),
aspect.ratio = 1,
plot.title = element_blank(),
legend.position = "bottom",
axis.title.y = element_text(size = 7),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
legend.key.size = unit(0.3, "cm"),
legend.text = element_text(size = 5),
legend.title = element_blank(),
legend.box = "horizontal",
legend.direction = "horizontal",
legend.key.height = unit(0.3, "cm"),
legend.key.width = unit(0.5, "cm"),
legend.box.spacing = unit(0.2, "cm")
) +
guides(fill = guide_legend(ncol = 2))
bx <- bx +
theme(
plot.margin = margin(5, 5, 5, 5),
aspect.ratio = 1,
plot.title = element_blank(),
axis.title.y = element_text(size = 5),
axis.title.x = element_text(size = 7),
legend.position = "right",
legend.key.size = unit(0.3, "cm"),
legend.text = element_text(size = 5),
legend.title = element_blank(),
legend.box = "horizontal",
legend.direction = "horizontal",
legend.key.height = unit(0.5, "cm"),
legend.key.width = unit(0.5, "cm"),
legend.box.spacing = unit(0.3, "cm")
) +
guides(fill = guide_legend(ncol = 2))
# Combine Plots into Multi-Panel
combined <- ggarrange(
bx, ln, vn,
labels = c("A", "B", "C"),
ncol = 2, nrow = 2,
heights = c(1, 1),
widths = c(1, 1),
align = "hv"
)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Graphs cannot be vertically aligned unless the axis parameter is set.
## Placing graphs unaligned.
## Warning: Graphs cannot be horizontally aligned unless the axis parameter is
## set. Placing graphs unaligned.
# Add Title to Combined Plot
grid.arrange(
combined,
top = textGrob(
"Carbon Dioxide Uptake in Grass Plants by Origin and in Chilled and Nonchilled Conditions",
gp = gpar(fontsize = 10, fontface = "bold")
)
)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.