Module 1: GGplot Basics

Loading Packages and Data

#Loading all of the packages I might need during this project
packages<-c("ggplot2","readr","tidyverse","dplyr","ggpubr","see")

check_install_packages<-function(pkg){
  if (!require(pkg, character.only = TRUE)){
    install.packages(pkg,dependencies = TRUE)
    library(pkg,character.only = TRUE)
  }
}

sapply(packages,check_install_packages)
## $ggplot2
## NULL
## 
## $readr
## NULL
## 
## $tidyverse
## NULL
## 
## $dplyr
## NULL
## 
## $ggpubr
## NULL
## 
## $see
## NULL
#learning how to retrieve data sets from with in r
data("USArrests")
head(USArrests)
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
?USArrests
#The USArrests data looks at four different statistics related to arrests in six states. The different variables that can be used are Murder, Assault, UrbanPop, and Rape. Assault, Murder, and Rape are in number of arrest per 100,000 residents. UrbanPop is the percentage of the population living in urban areas. All variables are numerical.

GGplot Graphic Code: Basics

Part 1

#Coding basics
ggplot(mtcars, aes(x=mpg, y=hp, color=cyl))+
  geom_point(size=2.4, shape=8)+
  ggtitle("Effect of Horsepower on Fuel Efficiency", subtitle="Categorized by Number of Cylinders")+
  ylab("Fuel Efficiency (MPG)")+
  xlab("Horsepower")+
  theme_minimal()+
  theme(
    legend.position = "bottom",
    legend.box = "horizontal",
    plot.margin = margin(t = 10, r = 10, b = 10, l = 10)
  )

Part 2: Playing Around

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
?ChickWeight

#Basics of code, part 2: it gets more complicated now
ggplot(ChickWeight, aes(x=weight, y=Time, color=Diet))+
  geom_point(shape=16, size=2)+
  scale_color_manual(values = c("#56B4E9", "#CC79A7", "#E69F00","#009E73"))+
  ggtitle("Effect of Time on Fuel Chick Weight", subtitle="Categorized by Diet")+
  ylab("Chick Weight (gm)")+
  xlab("Time (days since hatch)")+
  theme_minimal()+
  theme(
    legend.position = "bottom",
    legend.box = "horizontal",
    plot.margin = margin(t = 10, r = 10, b = 10, l = 10)
  )

Module 2:Practice Assignment

Task 1: Scatter Plot with Regression Line

data("USArrests")
head(USArrests)
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
?USArrests

library(ggplot2)

#Scatter Plot with Regression line, yay!
ggplot(USArrests, aes(x=Murder,y=Assault))+
  geom_point(color="#660000", size=2)+
  geom_smooth(aes(color="#660000"),method=lm)+
  ggtitle("Scatter Plot of Assault vs. Murder Rate")+
  ylab("Assault Rate")+
  xlab("Murder Rate")+
  theme_minimal()+
  theme(legend.position="none")

Task 2: Line PLot

library(dplyr)

##Mean: creating mean
USArrests$state <- rownames(USArrests)

USArrests$AVGCrimeRate <- rowMeans(USArrests[, c("Murder", "Assault", "Rape")])

head(USArrests)
##            Murder Assault UrbanPop Rape      state AVGCrimeRate
## Alabama      13.2     236       58 21.2    Alabama     90.13333
## Alaska       10.0     263       48 44.5     Alaska    105.83333
## Arizona       8.1     294       80 31.0    Arizona    111.03333
## Arkansas      8.8     190       50 19.5   Arkansas     72.76667
## California    9.0     276       91 40.6 California    108.53333
## Colorado      7.9     204       78 38.7   Colorado     83.53333
##Line Plot
ggplot(data=USArrests, aes(x=state,y=AVGCrimeRate, group=1))+
  geom_line(size=1, color="black")+
  geom_point(color = "steelblue", size = 2.5, shape = 16)+
  ggtitle("Line Plot of Average Crime Rate by State")+
  xlab("State")+
  ylab("Average Crime Rate")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Module 3: Box and Violin Plot

Downloading Data

#Using data from the outside world
CAM<-read.csv("Violin_Plot_Data.csv")
print(CAM)
##         F1Performance  Repeat1  Repeat2  Repeat3  Repeat4  Repeat5  Repeat6
## 1  SVMWithGradCAMMaps 0.670051 0.701571 0.680628 0.710660 0.648649 0.715686
## 2 SVMWithDeepShapMaps 0.673913 0.610390 0.630872 0.618357 0.662577 0.608696
##    Repeat7  Repeat8  Repeat9 Repeat10 Repeat11 Repeat12 Repeat13 Repeat14
## 1 0.713568 0.684932 0.699029 0.687500 0.720812 0.716418 0.666667 0.683417
## 2 0.623529 0.642857 0.607477 0.645833 0.631579 0.660099 0.662420 0.610778
##   Repeat15 Repeat16 Repeat17 Repeat18 Repeat19 Repeat20
## 1 0.666667 0.663317 0.691943 0.680412 0.686869 0.686551
## 2 0.701754 0.659091 0.577540 0.666667 0.678571 0.596685
##Installing new packages
packages<-c("ggplot2","readr","tidyverse","dplyr","ggpubr","see")
install.packages("ggplot2")
library(ggplot2)
library(readr)
library(tidyverse)
library(dplyr)
library(ggpubr)
install.packages("see")
library(see)

#Setting up the data so it can be used properly
head(CAM)
##         F1Performance  Repeat1  Repeat2  Repeat3  Repeat4  Repeat5  Repeat6
## 1  SVMWithGradCAMMaps 0.670051 0.701571 0.680628 0.710660 0.648649 0.715686
## 2 SVMWithDeepShapMaps 0.673913 0.610390 0.630872 0.618357 0.662577 0.608696
##    Repeat7  Repeat8  Repeat9 Repeat10 Repeat11 Repeat12 Repeat13 Repeat14
## 1 0.713568 0.684932 0.699029 0.687500 0.720812 0.716418 0.666667 0.683417
## 2 0.623529 0.642857 0.607477 0.645833 0.631579 0.660099 0.662420 0.610778
##   Repeat15 Repeat16 Repeat17 Repeat18 Repeat19 Repeat20
## 1 0.666667 0.663317 0.691943 0.680412 0.686869 0.686551
## 2 0.701754 0.659091 0.577540 0.666667 0.678571 0.596685
data_long <- CAM %>%
  pivot_longer(
    cols = starts_with("Repeat"),
    names_to = "Repeat", 
    values_to = "values")
head(data_long)
## # A tibble: 6 × 3
##   F1Performance      Repeat  values
##   <chr>              <chr>    <dbl>
## 1 SVMWithGradCAMMaps Repeat1  0.670
## 2 SVMWithGradCAMMaps Repeat2  0.702
## 3 SVMWithGradCAMMaps Repeat3  0.681
## 4 SVMWithGradCAMMaps Repeat4  0.711
## 5 SVMWithGradCAMMaps Repeat5  0.649
## 6 SVMWithGradCAMMaps Repeat6  0.716

Figure Recreation

#Figure recreation: the most difficult thing I've ever done
ggplot(data_long, aes(x=F1Performance, y=values,fill=F1Performance))+
  geom_jitter(position=position_jitter(0.1),aes(color = F1Performance), size =6, alpha = 0.8) +
  geom_violin(size=2, alpha=0.5,draw_quantiles = c(0.25, 0.5, 0.75), quantile.size = 2)+
  coord_flip()+
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(fun=median,geom="point",shape=21,size=3,fill="white",color="black",stroke=1.5)+
  scale_y_continuous(limits = c(min(data_long$values), max(data_long$values)),breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),labels = scales::number_format(accuracy = 0.02)) +
  theme_minimal()+
  theme(legend.title = element_text(face = "bold", size = 14),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.title.y = element_blank(), axis.line.x = element_line(size = 2, color = "black"), plot.title = element_text(hjust=0.5,face="bold"),panel.grid.major.y = element_blank(), panel.grid.minor.x=element_blank(),panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),legend.position = "none")+
  geom_text(aes(x="SVMWithGradCAMMaps",label="SVM + GRAD-CAM++",y=0.64),vjust=-4.5,color="darkorange2",size=4.5)+
  geom_text(aes(x = "SVMWithDeepShapMaps", y = 0.6, label = "SVM + Deep SHAP"),vjust = -3.5, color = "magenta4", size = 4.5)+
  ylab("F1")+
  ggtitle("Fig.7.Grad-CAM++saliency maps capture unique predictive information.")

Half-Violin

#Half-Violin!
ggplot(data_long, aes(x=F1Performance, y=values,fill=F1Performance))+
  geom_jitter(position=position_jitter(0.1),aes(color = F1Performance), size =6, alpha = 0.8) +
  geom_violinhalf(size=2, alpha=0.5,draw_quantiles = c(0.25, 0.5, 0.75), quantile.size = 2)+
  coord_flip()+
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(fun=median,geom="point",shape=21,size=3,fill="white",color="black",stroke=1.5)+
  scale_y_continuous(limits = c(min(data_long$values), max(data_long$values)),breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),labels = scales::number_format(accuracy = 0.02)) +
  theme_minimal()+
  theme(legend.title = element_text(face = "bold", size = 14),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.title.y = element_blank(), axis.line.x = element_line(size = 2, color = "black"), plot.title = element_text(hjust=0.5,face="bold"),panel.grid.major.y = element_blank(), panel.grid.minor.x=element_blank(),panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),legend.position = "none")+
  geom_text(aes(x="SVMWithGradCAMMaps",label="SVM + GRAD-CAM++",y=0.64),vjust=-4.5,color="darkorange2",size=4.5)+
  geom_text(aes(x = "SVMWithDeepShapMaps", y = 0.6, label = "SVM + Deep SHAP"),vjust = -3.5, color = "magenta4", size = 4.5)+
  ylab("F1")+
  ggtitle("Fig.7.Grad-CAM++saliency maps capture unique predictive information.")

Box+Half-Violin Plot

#Half-violin and box remix
ggplot(data_long, aes(x=F1Performance, y=values,fill=F1Performance))+
  geom_jitter(position=position_jitter(0.1),aes(color = F1Performance), size =6, alpha = 0.8) +
  geom_violinhalf(size=2, alpha=0.5,draw_quantiles = c(0.25, 0.5, 0.75), quantile.size = 2)+
  geom_boxplot(aes(color = F1Performance), width = 0.4, alpha = 0.3, outlier.shape = NA) +
  coord_flip()+
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(fun=median,geom="point",shape=21,size=3,fill="white",color="black",stroke=1.5)+
  scale_y_continuous(limits = c(min(data_long$values), max(data_long$values)),breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),labels = scales::number_format(accuracy = 0.02)) +
  theme_minimal()+
  theme(legend.title = element_text(face = "bold", size = 14),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.title.y = element_blank(), axis.line.x = element_line(size = 2, color = "black"), plot.title = element_text(hjust=0.5,face="bold"),panel.grid.major.y = element_blank(), panel.grid.minor.x=element_blank(),panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),legend.position = "none")+
  geom_text(aes(x="SVMWithGradCAMMaps",label="SVM + GRAD-CAM++",y=0.64),vjust=-4.5,color="darkorange2",size=4.5)+
  geom_text(aes(x = "SVMWithDeepShapMaps", y = 0.6, label = "SVM + Deep SHAP"),vjust = -3.5, color = "magenta4", size = 4.5)+
  ylab("F1")+
  ggtitle("Fig.7.Grad-CAM++saliency maps capture unique predictive information.")

Module 4: Barplots and Error Bars

Challenge Yourself 2, Task 1: Data Prep.

library(ggplot2)
library(dplyr)

data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#just summarizing the data, no big deal
mtcars_summary <- mtcars %>%
  group_by(cyl) %>%
  summarise(mean_mpg = mean(mpg),
            se_mpg = sd(mpg) / sqrt(n()))

head(mtcars_summary)
## # A tibble: 3 × 3
##     cyl mean_mpg se_mpg
##   <dbl>    <dbl>  <dbl>
## 1     4     26.7  1.36 
## 2     6     19.7  0.549
## 3     8     15.1  0.684

Task 2: Bar Plot Creation

ggplot(mtcars_summary,aes(x=factor(cyl),y=mean_mpg))+
  geom_bar(stat="identity")+
  geom_errorbar(aes(ymin=mean_mpg, ymax=mean_mpg+se_mpg), width=0.3)

Task 3: Customize the Plot

#Making it pretty
ggplot(mtcars_summary,aes(x=factor(cyl),y=mean_mpg))+
  geom_bar(stat="identity",fill="skyblue", color="black", width=0.9)+
  geom_errorbar(aes(ymin=mean_mpg-se_mpg, ymax=mean_mpg+se_mpg), width=0.4, color="red")+
  ggtitle("Bar Plot of Mean MPG by Cylinders")+
  xlab("Number of Cylinders")+
  ylab("Mean MPG")+
  theme_minimal()

Module 5: Depictions of Frequency

Downloading the Data:2D Density Plot

#Pulling more data
population_data<-read.csv("log_population_data.csv")
head(population_data)
##   Log10_Current_Population Log10_Past_Population
## 1                 4.288032              5.674204
## 2                 3.817497              5.908109
## 3                 4.671286              6.095078
## 4                 3.538305              5.200114
## 5                 4.602143              6.388435
## 6                 4.839555              6.187712
#Density Plot: shout out to viridis
ggplot(population_data, aes(x = Log10_Current_Population, y = Log10_Past_Population)) +
  stat_density_2d(geom = "polygon", aes(fill = ..level..), color = "white") +
  scale_fill_viridis_c(option = "plasma")+
  ggtitle("2D Density Plot of Population Sizes")+
  xlab("Log10(Current population size N0")+
  ylab("Log10(Past population size N1")+
  theme_minimal()

Adding Density to Margins

library(ggExtra)
library(tidyverse)
library(dplyr)
library(ggplot2)

#Pulling and formating more data
longevity_data<-read.csv("longevity_data.csv")
head(longevity_data)
##                     species    class           order maximum_lifespan_yr mass_g
## 1 Dicrostonyx_groenlandicus Mammalia        Rodentia                 3.3   66.0
## 2      Didelphis_virginiana Mammalia Didelphimorphia                 6.6 3000.0
## 3         Diphylla_ecaudata Mammalia      Chiroptera                 8.0   28.0
## 4     Dipodillus_campestris Mammalia        Rodentia                 7.3   28.4
## 5        Dipodomys_merriami Mammalia        Rodentia                 9.7   42.0
## 6   Dendrolagus_goodfellowi Mammalia   Diprotodontia                23.6 7400.0
##     volancy fossoriallity foraging_environment daily_activity
## 1 nonvolant semifossorial          terrestrial     cathemeral
## 2 nonvolant  nonfossorial         semiarboreal      nocturnal
## 3    volant  nonfossorial          terrestrial      nocturnal
## 4 nonvolant semifossorial          terrestrial      nocturnal
## 5 nonvolant semifossorial          terrestrial      nocturnal
## 6 nonvolant  nonfossorial         semiarboreal     cathemeral
long <- longevity_data %>%
  mutate( 
    log_mass = log10(mass_g),                          
    log_lifespan = log10(maximum_lifespan_yr))  %>%          
   group_by(order) %>%          
  mutate(order_size = n())

head(long)
## # A tibble: 6 × 12
## # Groups:   order [4]
##   species           class order maximum_lifespan_yr mass_g volancy fossoriallity
##   <chr>             <chr> <chr>               <dbl>  <dbl> <chr>   <chr>        
## 1 Dicrostonyx_groe… Mamm… Rode…                 3.3   66   nonvol… semifossorial
## 2 Didelphis_virgin… Mamm… Dide…                 6.6 3000   nonvol… nonfossorial 
## 3 Diphylla_ecaudata Mamm… Chir…                 8     28   volant  nonfossorial 
## 4 Dipodillus_campe… Mamm… Rode…                 7.3   28.4 nonvol… semifossorial
## 5 Dipodomys_merria… Mamm… Rode…                 9.7   42   nonvol… semifossorial
## 6 Dendrolagus_good… Mamm… Dipr…                23.6 7400   nonvol… nonfossorial 
## # ℹ 5 more variables: foraging_environment <chr>, daily_activity <chr>,
## #   log_mass <dbl>, log_lifespan <dbl>, order_size <int>
#Density on the sides of the figure, cool stuff!
p=ggplot(long, aes(x=log_mass,y=log_lifespan, color=class))+
  geom_point(aes(size=order_size),alpha=0.3)+
  geom_smooth(method = "lm", aes(group = class), se = FALSE, linetype = "solid")+
  scale_color_manual(values = c("lightgreen", "darkslategray"))+
  ggtitle("Bubble Chart of Longevity and Body Mass")+
  xlab("Log(Body Mass[g])")+
  ylab("Log(Maximum Lifespan [yr])")+
  theme_minimal()+
   theme(
    legend.position = "none",plot.title = element_text(size = 14, face = "bold"),axis.title = element_text(size = 12, face = "bold"))+
  annotate("text", x=5.5, y=1.9, label="Aves",color="lightgreen", size=5, fontface= "bold")+
  annotate("text", x=6, y=1.2, label="Mammals",color="darkslategray", size=5,fontface= "bold")

ggExtra::ggMarginal(p, type = "density", groupFill = TRUE, alpha = 0.4)

##Interpretation Questions
#1.Density plots are useful in the margins of your graphics to show exactly where density is occurring. In our figure we can see that the mammals density is highest at approximately x=4 and y=1.3.

#2.We are able to depict 6 different measures in a single graphic because we able to depict the two logs, maximum lifespan and body mass, as well as showing those values for both Aves and Mammals. We also made a new category by calculating the sample size of each order (order_size), which we also displayed in the figure. We also displayed the density using ggExtra.

#3.The relationship between Longevity and Body mass is positive, body mass increasing as the animals gets older. This relationship is more extreme in Aves, displayed by the steeper slope of it's related line.

#4.I believe the bias depends on which type of organism you're looking at. Mammals have a biased for larger, longer lived individuals, this is shown by the clumping together of dots closer to the line in that area while the dots get farther from the line when the body mass is smaller. Aves bias seems to favor those that are smaller and long lived birds which can be seen with the line but also with the density portion of the figure.

#5.The only element that I could think to add would be a key for the size variation, but I assume that type of thing could be added into the caption when it would be added to the paper, though you should be able to read a figure without a caption.

Design it Yourself: Body condition, Inc. Duration, Treatment

scelop<-read.csv("Scelop_Data.csv")
head(scelop)
##   Egg  OvipDate Cage Treatment Temperature Moisture EggMass HatchingSuccess
## 1   1 6/10/2022    5         1        High     High   0.377               1
## 2  10 6/10/2022    5         2         Low     High   0.391               1
## 3  11 6/10/2022    5         3        High      Low   0.358               1
## 4  16 6/15/2022    9         4         Low      Low   0.364               1
## 5  17 6/15/2022    9         1        High     High   0.309               0
## 6  19 6/15/2022    9         3        High      Low   0.330               0
##   HatchDate IncDuration Sex SVL TL HatchMass BodyConPCA OffMassPCA
## 1 7/27/2022          47   M  26 30     0.502  1.4168605  0.9954916
## 2 8/12/2022          63   F  22 24     0.543  0.5651503  1.6654549
## 3 7/26/2022          46   M  25 29     0.501  1.0706865  0.7334526
## 4 8/15/2022          61   F  22 22     0.331 -1.9458048 -1.2010403
## 5                    NA      NA NA        NA         NA         NA
## 6                    NA      NA NA        NA         NA         NA
scelop$Treatment <- factor(scelop$Treatment)
levels(scelop$Treatment)
## [1] "1" "2" "3" "4"
liz<-ggplot(scelop, aes(x=BodyConPCA,y=IncDuration, color=Treatment))+
  geom_point(alpha=0.4,aes(size=4))+
  scale_color_manual(values = c("blue3", "steelblue1", "darkorange4", "goldenrod2")) +
  ggtitle("Body Condition and Incubation Duration by Treatment")+
  xlab("Body Condition")+
  ylab("Incubation Duration (days)")+
  theme_minimal()+
  theme(legend.position = "left",plot.title = element_text(size = 14, face = "bold"),axis.title = element_text(size = 12, face = "bold"))+
  guides(size = "none")

ggExtra::ggMarginal(liz, type = "density", groupFill = TRUE, alpha = 0.4)

Module 6: Multipanel Graphics

Figure 1: Facet Wrap

data("esoph")
head(esoph)
##   agegp     alcgp    tobgp ncases ncontrols
## 1 25-34 0-39g/day 0-9g/day      0        40
## 2 25-34 0-39g/day    10-19      0        10
## 3 25-34 0-39g/day    20-29      0         6
## 4 25-34 0-39g/day      30+      0         5
## 5 25-34     40-79 0-9g/day      0        27
## 6 25-34     40-79    10-19      0         7
?esoph

#Learning some "facet_wrap()"
alc<-ggplot(esoph, aes(x = agegp, y = ncases, group = alcgp, color = alcgp)) +
  geom_line(size=1) +
  facet_wrap(~ alcgp) +
  ggtitle("Line Plot of Esophageal Cancer Cases by Age and Alcohol Consumption") +
  xlab("Age Group") +
  ylab("Number of Cases") +
  theme_minimal()
alc

Figure 2: Facet Wrap

head(ChickWeight)
## Grouped Data: weight ~ Time | Chick
##   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
?ChickWeight

#Learning more "facet_wrap()" but add some more stuff like more lines and se
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)+
  ggtitle("Chick Growth by Diet Type") +
  xlab("Time (Days)") +
  ylab("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"))
chi

Figure 3: Adding Seperate Plots to one Figure

library(ggpubr)
library(grid)
library(gridExtra)

#The actual most difficult thing I've ever had to do
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
?CO2
###Multi Panel data("CO2") head(CO2) ?CO2
CO2$TreatmentType <- interaction(CO2$Treatment, CO2$Type)
CO2$PlantType <- interaction(CO2$Plant, CO2$Type)

##Violin, uptake(uptake) by treatment, grouped by type 
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("cyan3", "darkorange1", "goldenrod2", "tan4")) + scale_color_manual(values = c("cyan3", "darkorange1", "goldenrod2", "tan4")) + theme(legend.position="right",plot.title.position = "plot",plot.margin = margin(t = 50, r = 20, b = 20, l = 20))+ xlab("Uptake Rates (μmol/m2sec)")+ ggtitle("CO2 Uptake of Grass Plants in Chilled and Unchilled Conditions")

vn

##Line plot, ambient CO2(conc) uptake, colored and grouped by treatment 
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("tomato", "steelblue2")) + scale_color_manual(values = c("tomato", "steelblue2")) + ggtitle("Ambient CO2 Concentration by Uptake Rates and Treatment")+ ylab("Uptake Rates(μmol/m2sec)")+ xlab("C02 Concentration (mL/L")+ theme_minimal()

ln

##Additional plot of your choice that shows a new element: Boxplot
bx<-ggplot(CO2, aes(x = Treatment, y = uptake, fill = Treatment)) + geom_boxplot() + ggtitle("CO2 Uptake Rates by Treatment Type")+ xlab("Treatment")+ ylab("Uptake Rates (μmol/m2sec)") + scale_fill_manual(values = c("nonchilled" = "tomato", "chilled" = "steelblue2")) + theme_minimal()

bx

##Add them together! 
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))

combined<-ggarrange(bx, ln, vn, labels = c("A", "B", "C"),ncol = 2, nrow = 2,heights = c(1, 1), widths = c(1, 1), align = "hv")

grid.arrange(
  combined, 
  top = textGrob("Carbon Dioxide Uptake in Grass Plants by Orgin and in Chilled and Nonchilled Conditions", 
                 gp = gpar(fontsize = 10, fontface = "bold"))
)

Final Project: Going Crazy

Figure 1: Hatch Success & Treatment

##The data!
##Same data set as used in Mod. 5: Design it yourself
head(scelop)
##   Egg  OvipDate Cage Treatment Temperature Moisture EggMass HatchingSuccess
## 1   1 6/10/2022    5         1        High     High   0.377               1
## 2  10 6/10/2022    5         2         Low     High   0.391               1
## 3  11 6/10/2022    5         3        High      Low   0.358               1
## 4  16 6/15/2022    9         4         Low      Low   0.364               1
## 5  17 6/15/2022    9         1        High     High   0.309               0
## 6  19 6/15/2022    9         3        High      Low   0.330               0
##   HatchDate IncDuration Sex SVL TL HatchMass BodyConPCA OffMassPCA
## 1 7/27/2022          47   M  26 30     0.502  1.4168605  0.9954916
## 2 8/12/2022          63   F  22 24     0.543  0.5651503  1.6654549
## 3 7/26/2022          46   M  25 29     0.501  1.0706865  0.7334526
## 4 8/15/2022          61   F  22 22     0.331 -1.9458048 -1.2010403
## 5                    NA      NA NA        NA         NA         NA
## 6                    NA      NA NA        NA         NA         NA
#Hatching Success by Treatment: Density
first<-ggplot(scelop, aes(x = HatchingSuccess, fill = Treatment)) +
  geom_density(alpha = 0.6) +
  facet_wrap(~Treatment, ncol= 4)+
  scale_fill_manual(values = c("#56B4E9", "#009E73", "#F0E442", "#D55E00")) +
  ggtitle("Density Plot of Hatching Success by Treatment") +
  xlab("Hatching Success") +
  ylab("Density") +
  theme(legend.position="none", axis.text.x = element_text(size = 6),plot.title = element_text(size = 12, face = "bold"))
first

Figure 2: Clutch size & body con., Treatment & Body con., Inc Dur. & Body Con.

##Still don't know how clutch size is calculated for this first part

##Treatment and Body Con (Box plot)
BCT<-ggplot(scelop, aes(x = Treatment, y = BodyConPCA, fill = Treatment)) +
  geom_violin(color = "black") +
  geom_jitter(width = 0.2, alpha = 0.5) +
  labs(x = "Treatment", y = "Body  Condition") +
  scale_fill_manual(values = c("#56B4E9","#009E73","#F0E442","#D55E00"))+
  theme(legend.position="bottom")
BCT

##IncDur and Body Con (point with lines for treatment)
IDBC<-ggplot(scelop, aes(x=IncDuration,y=BodyConPCA, color=Treatment))+
  geom_point(size=2)+
  geom_smooth(method=lm,aes(group = Treatment), se=FALSE)+
  scale_color_manual(values = c("#56B4E9","#009E73","#F0E442","#D55E00")) +
  labs(y="Body Condition",x="Incubation Duration")+
  theme(legend.position="bottom")
IDBC

##Put it together!!
two<-ggarrange(BCT,IDBC, labels = c("A", "B"),ncol = 2, nrow = 1,heights = c(1, 1), widths = c(1, 1), align = "hv")

second<-grid.arrange(two, top = textGrob("Body Condition by Treatment and Incubation Duration", gp = gpar(fontsize = 12, fontface = "bold")))

Figure 3: Inc. Dur. & Treatment, SVL & Treatment

#box-plot, incubation duration by treatment

scelop$Treatment <- as.factor(scelop$Treatment)

IncTreat<-ggplot(scelop, aes(x = Treatment, y = IncDuration, fill = Treatment)) +
  geom_boxplot(color = "black") +
  geom_jitter(width = 0.2, alpha = 0.5) +
  labs(x = "Treatment", y = "Incubation Duration (Days)") +
  scale_fill_manual(values = c("#56B4E9","#009E73","#F0E442","#D55E00")) +
  theme(legend.position="bottom")
IncTreat

#line-histogram combo situation, svl by treatment
LnSVL<-ggplot(scelop, aes(x = SVL, y = IncDuration, group = Treatment, color = Treatment)) +
  geom_point(size=2)+
  geom_smooth(method = "lm", aes(group = Treatment), se = FALSE, linetype = "solid")+
  labs(x= "Snout Vent Length", y="Incubation Duration") +
  scale_color_manual(values = c("#56B4E9","#009E73","#F0E442","#D55E00")) +
  theme(legend.position = "bottom")

HLS<-ggExtra::ggMarginal(LnSVL, type = "histogram", groupFill = TRUE, alpha = 0.4)
HLS

#multipanel that shit!
three<-ggarrange(IncTreat,HLS, labels = c("A", "B"),ncol = 2, nrow = 1,heights = c(1, 1), widths = c(1, 1), align = "hv")

third<-grid.arrange(three, top = textGrob("Incubation Duration by Treatment Group and SVL", gp = gpar(fontsize = 12, fontface = "bold")))

Figure 4.HatchMass & IncDuration by Treatment

four<-ggplot(scelop, aes(x=HatchMass,y=IncDuration, color=Treatment))+
  geom_point(size=5, alpha=0.8)+
  scale_color_manual(values = c("#56B4E9","#009E73","#F0E442","#D55E00")) +
  labs(title="Hatchling Mass by Incubation Duration",y="Incubation Duration",x="Hatchling Mass")+
  theme(legend.position="bottom", plot.title = element_text(size = 12, face = "bold"))

fourth<-ggExtra::ggMarginal(four, type = "density", groupFill = TRUE, alpha = 0.4)
fourth