self notes: export a figure as a pdf, eps, or png: ggexport(figure, filename, “figure1.pdf”, nrow = 2, ncol = 1)

to save a plot in r: save_plot(“plot name.pdf”, plot name, ncol = x, nrow = 2, base_aspect_ratio = 1.4) # always 1.4 to add room for legend

Mod 6 chunk 1 chick plot

# load libraries
library(ggplot2)   # facet_grid(), facet_wrap() for creating multiple figures with the same axis  
library(cowplot)   # plot_grid() and draw_plot(plot, x = , y = , width = , height = )
library(gridExtra) # grid.arrange()
library(ggpubr)    # ggarrange() easiest to use
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:cowplot':
## 
##     get_legend
library(patchwork) # plot_layout()
## 
## Attaching package: 'patchwork'
## The following object is masked from 'package:cowplot':
## 
##     align_plots
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# load dataset 

data(ChickWeight)
head(ChickWeight)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1
# summary statistics
summary(ChickWeight)
##      weight           Time           Chick     Diet   
##  Min.   : 35.0   Min.   : 0.00   13     : 12   1:220  
##  1st Qu.: 63.0   1st Qu.: 4.00   9      : 12   2:120  
##  Median :103.0   Median :10.00   20     : 12   3:120  
##  Mean   :121.8   Mean   :10.72   10     : 12   4:118  
##  3rd Qu.:163.8   3rd Qu.:16.00   17     : 12          
##  Max.   :373.0   Max.   :21.00   19     : 12          
##                                  (Other):506
# create plot   
p <- ggplot(ChickWeight, aes(x = Time, y = weight, color = Chick, alpha = 0.1)) + geom_line() +

# regressChickWeight# regression line 
geom_smooth(color = "black", size = 1.2) +
  
# labels 
labs(title = "Chick Growth by Diet Type", x = "Time (Days)", y = "Weight (Grams)") +
  
# theme and legend
theme_minimal() + theme(legend.position = "none") +
  
# facet wrap 
facet_wrap(vars(Diet), ncol = 4, nrow = 1) 

Output of Chick Plot

plot(p)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Multipanel Figure Plot

# load packages 
library(dplyr)
library(ggplot2)   # facet_grid(), facet_wrap() for creating multiple figures with the same axis  
library(cowplot)   # plot_grid() and draw_plot(plot, x = , y = , width = , height = )
library(gridExtra) # grid.arrange()
library(ggpubr)    # ggarrange() easiest to use
library(patchwork) # plot_layout()
# load data befor each plot  
data(CO2)
head(CO2)
## Grouped Data: uptake ~ conc | Plant
##   Plant   Type  Treatment conc uptake
## 1   Qn1 Quebec nonchilled   95   16.0
## 2   Qn1 Quebec nonchilled  175   30.4
## 3   Qn1 Quebec nonchilled  250   34.8
## 4   Qn1 Quebec nonchilled  350   37.2
## 5   Qn1 Quebec nonchilled  500   35.3
## 6   Qn1 Quebec nonchilled  675   39.2
# 1. violin plot
v <- ggplot(CO2, aes(x = Treatment, y = uptake, alpha = 0.4, fill = Type)) +
  geom_jitter(size = 3, alpha = NA, position = position_jitterdodge(jitter.width = 0.2, dodge.width = 1)) +
  geom_violin(position = position_dodge(1), aes(alpha = 0.2)) + 
  geom_boxplot(width = 0.3, position = position_dodge(1), aes(color_fill_manual = "grey", alpha = 0.2)) + 
# i don't know why i can't change the fill of my boxplots to be grey

  # labels 
labs(title = "CO2 Uptake of Different Plant Type by Treatment", x = "Treatment", y = "CO2 Uptake (μmol/m^2 sec)") + 
  
  # theme # theme Type
theme_classic() + theme(plot.title = element_text(face = "bold"), 
                        axis.title.x = element_text(face = "bold"), 
                        axis.title.y = element_text(face = "bold")) +
  
# remove alpha legend
guides(alpha = "none")
v

# 2. Line plot 
l <- ggplot(CO2, aes(x = conc, y = uptake)) + geom_line(aes(color = Treatment, fill = Treatment, size = 2, alpha = 0.8)) +
  
  # facet 
facet_grid(cols = vars(Type)) +
  
# labels 
labs(title = "Ambient CO2 Uptake in Different Areas and CO2 Concentration by Treatment", x = "CO2 Concentration (mL/L)", y = "CO2 Uptake (μmol/m^2 sec)") +

# themes 
theme_minimal() + 
  theme(plot.title = element_text(face = "bold", hjust = 0.5), 
                        axis.title = element_text(face = "bold")) +

# remove alpha and size legend
guides(alpha = "none", size = "none")
l

# 3. density plot with a vertical dashed line at mean

# create the plot 
d <- ggplot(CO2, aes(x = uptake, fill = Treatment)) +
  geom_density(alpha = 0.5) +  
  
# add a vertical line at mean 
geom_vline(aes(xintercept = mean(uptake, na.rm = TRUE)), 
             linetype = "dashed", color = "black", size = 1) +  
  
# labels 
labs(title = "Density Plot of CO2 Uptake by Treatment with Mean Line", x = "CO2 Uptake", y = "Density") +

# theme 
  theme_minimal() + theme(plot.title = element_text(face = "bold"), 
                          title.axis = element_text(face = "bold"))

d

# combine plots into one figure 

multipanel_plot <- plot_grid(
  v + theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank()),
  l + theme(plot.title = element_blank(), axis.title = element_blank()),
  d + theme(plot.title = element_blank(), axis.title = element_blank()),
  ncol = 1,                        # Arrange in 1 column
  labels = c("A", "B", "C"),       # Add labels to each plot
  label_size = 13, 
  label_y = c(1.03, 1.05, 1.12), 
  rel_heights = c(1.2, 1, 0.9))

Output of Multipanel Plot

plot(multipanel_plot)

Notes: -I don’t know why I can’t change the fill of my boxplots to be grey using the color_fill_manual in aes() like i did in mod 3

-that’s the best way I can get all 3 graphs on the same page, they’re quite large and I didn’t find a better way to do it.