R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring:

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:

library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(palmerpenguins)
library(corrplot)
## corrplot 0.92 loaded
summary(penguins)
##       species          island    bill_length_mm  bill_depth_mm  
##  Adelie   :152   Biscoe   :168   Min.   :32.10   Min.   :13.10  
##  Chinstrap: 68   Dream    :124   1st Qu.:39.23   1st Qu.:15.60  
##  Gentoo   :124   Torgersen: 52   Median :44.45   Median :17.30  
##                                  Mean   :43.92   Mean   :17.15  
##                                  3rd Qu.:48.50   3rd Qu.:18.70  
##                                  Max.   :59.60   Max.   :21.50  
##                                  NA's   :2       NA's   :2      
##  flipper_length_mm  body_mass_g       sex           year     
##  Min.   :172.0     Min.   :2700   female:165   Min.   :2007  
##  1st Qu.:190.0     1st Qu.:3550   male  :168   1st Qu.:2007  
##  Median :197.0     Median :4050   NA's  : 11   Median :2008  
##  Mean   :200.9     Mean   :4202                Mean   :2008  
##  3rd Qu.:213.0     3rd Qu.:4750                3rd Qu.:2009  
##  Max.   :231.0     Max.   :6300                Max.   :2009  
##  NA's   :2         NA's   :2

Clean the Data and Prepare for Visualization

colnames(penguins)[7] <- "gender"
penguins <- penguins %>% drop_na()
df_num <- penguins %>% select(bill_depth_mm, bill_length_mm, body_mass_g, flipper_length_mm) %>% drop_na()

df_num <- penguins %>%
  select(bill_depth_mm, bill_length_mm, body_mass_g, flipper_length_mm) %>% 
  drop_na()

corr_mat <- cor(as.matrix(df_num))
colnames(corr_mat) <- c("Bill Depth (mm)", "Bill Length (mm)", "Body Mass (g)", "Flipper Length (mm)")
rownames(corr_mat) <- colnames(corr_mat)

df_species <- penguins %>% 
  group_by(species) %>% 
  drop_na() %>% # drop nulls before summarizing
  summarize(avg_bill_length_mm = mean(bill_length_mm), avg_bill_depth_mm = mean(bill_depth_mm),avg_flipper_length_mm = mean(flipper_length_mm), avg_body_mass_g = mean(body_mass_g))

tail(df_species)
## # A tibble: 3 × 5
##   species   avg_bill_length_mm avg_bill_depth_mm avg_flipper_length_mm
##   <fct>                  <dbl>             <dbl>                 <dbl>
## 1 Adelie                  38.8              18.3                  190.
## 2 Chinstrap               48.8              18.4                  196.
## 3 Gentoo                  47.6              15.0                  217.
## # ℹ 1 more variable: avg_body_mass_g <dbl>
# -- island -- #
df_island <- penguins %>% 
  group_by(island) %>% 
  drop_na() %>% # drop nulls before summarizing
  summarize(avg_bill_length_mm = mean(bill_length_mm), avg_bill_depth_mm = mean(bill_depth_mm), avg_flipper_length_mm = mean(flipper_length_mm), avg_body_mass_g = mean(body_mass_g))

tail(df_island)
## # A tibble: 3 × 5
##   island    avg_bill_length_mm avg_bill_depth_mm avg_flipper_length_mm
##   <fct>                  <dbl>             <dbl>                 <dbl>
## 1 Biscoe                  45.2              15.9                  210.
## 2 Dream                   44.2              18.3                  193.
## 3 Torgersen               39.0              18.5                  192.
## # ℹ 1 more variable: avg_body_mass_g <dbl>
# -- gender -- #
df_gender <- penguins %>% 
  group_by(gender) %>% 
  drop_na() %>% # drop nulls before summarizing
  summarize(avg_bill_length_mm = mean(bill_length_mm), avg_bill_depth_mm = mean(bill_depth_mm), avg_flipper_length_mm = mean(flipper_length_mm), avg_body_mass_g = mean(body_mass_g))

tail(df_gender)
## # A tibble: 2 × 5
##   gender avg_bill_length_mm avg_bill_depth_mm avg_flipper_length_mm
##   <fct>               <dbl>             <dbl>                 <dbl>
## 1 female               42.1              16.4                  197.
## 2 male                 45.9              17.9                  205.
## # ℹ 1 more variable: avg_body_mass_g <dbl>

Including Plots

You can also embed plots, Plot over here: Plot this way

For example:

## NULL
## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.