MODULE 2:Practice Assignment

MODULE 3: Box and Violin

MODULE 4: Bar Plots and Error Bars

MODULE 5:Depictions of Frequency

MODULE 6:Multipanel Graphics

###Figure 2
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

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
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

library(ggpubr)
library(grid)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
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
## `geom_smooth()` using formula = 'y ~ x'

##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")
## `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.
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"))
)