MODULE 2:Practice Assignment

MODULE 3: Box and Violin

MODULE 4: Bar Plots 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
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
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
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
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()
## Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

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


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>
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)