### Get file paths for bundled CSVs Source: https://allisonhorst.github.io/palmerpenguins/reference/path_to_file.html Use `path_to_file()` to list available example CSV files. Provide a filename like `"penguins_raw.csv"` to get its absolute path. ```R path_to_file() #> [1] "penguins.csv" "penguins_raw.csv" ``` ```R path_to_file("penguins_raw.csv") #> [1] "/home/runner/work/_temp/Library/palmerpenguins/extdata/penguins_raw.csv" ``` ```R head(read.csv(path_to_file("penguins.csv"))) #> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g #> 1 Adelie Torgersen 39.1 18.7 181 3750 #> 2 Adelie Torgersen 39.5 17.4 186 3800 #> 3 Adelie Torgersen 40.3 18.0 195 3250 #> 4 Adelie Torgersen NA NA NA NA #> 5 Adelie Torgersen 36.7 19.3 193 3450 #> 6 Adelie Torgersen 39.3 20.6 190 3650 #> sex year #> 1 male 2007 #> 2 female 2007 #> 3 female 2007 #> 4 2007 #> 5 female 2007 #> 6 male 2007 ``` -------------------------------- ### Load required R packages Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Initial setup to load all necessary libraries for the analysis. ```R library(palmerpenguins) library(corrr) library(GGally) library(recipes) library(tidytext) library(dplyr) library(tidyr) library(ggplot2) theme_set(theme_minimal()) ``` -------------------------------- ### Install palmerpenguins Development Version from GitHub Source: https://allisonhorst.github.io/palmerpenguins/index.html Install the development version of the palmerpenguins package directly from GitHub using the remotes package. ```R # install.packages("remotes") remotes::install_github("allisonhorst/palmerpenguins") ``` -------------------------------- ### Install palmerpenguins from CRAN Source: https://allisonhorst.github.io/palmerpenguins/index.html Use this command to install the released version of the palmerpenguins package from CRAN. ```R install.packages("palmerpenguins") ``` -------------------------------- ### Load Libraries and Set Theme Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Loads the palmerpenguins and ggplot2 libraries and sets a minimal theme for plots. This is a common setup for creating visualizations with this dataset. ```r library(palmerpenguins) library(ggplot2) ggplot2::theme_set(ggplot2::theme_minimal()) ``` -------------------------------- ### Load palmerpenguins Package Source: https://allisonhorst.github.io/palmerpenguins/articles/art.html Use this snippet to load the palmerpenguins package into your R session. Ensure the package is installed before running. ```r library(palmerpenguins) ``` -------------------------------- ### GET /penguins Source: https://allisonhorst.github.io/palmerpenguins/reference/penguins.html Retrieves the full dataset of penguin measurements including species, island, bill dimensions, flipper length, body mass, sex, and year. ```APIDOC ## GET /penguins ### Description Retrieves a tibble containing 344 rows of penguin size measurements. ### Method GET ### Endpoint /penguins ### Response #### Success Response (200) - **species** (factor) - Penguin species (Adélie, Chinstrap, Gentoo) - **island** (factor) - Island in Palmer Archipelago (Biscoe, Dream, Torgersen) - **bill_length_mm** (number) - Bill length in millimeters - **bill_depth_mm** (number) - Bill depth in millimeters - **flipper_length_mm** (integer) - Flipper length in millimeters - **body_mass_g** (integer) - Body mass in grams - **sex** (factor) - Penguin sex (female, male) - **year** (integer) - Study year (2007, 2008, 2009) ``` -------------------------------- ### Perform PCA with recipes Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Preprocess data and execute PCA using the recipes package workflow. ```R library(recipes) penguin_recipe <- recipe(~., data = penguins) %>% update_role(species, island, sex, year, new_role = "id") %>% step_naomit(all_predictors()) %>% step_normalize(all_predictors()) %>% step_pca(all_predictors(), id = "pca") %>% prep() penguin_pca <- penguin_recipe %>% tidy(id = "pca") penguin_pca ``` -------------------------------- ### Download palmerpenguins Data from EDI Source: https://allisonhorst.github.io/palmerpenguins/articles/download.html Downloads penguin data for Adelie, Gentoo, and Chinstrap species directly from the Environmental Data Initiative (EDI) using provided URIs. It then combines the data into a single tibble. Requires 'purrr' and 'readr' packages. ```r # Adelie penguin data from: https://doi.org/10.6073/pasta/abc50eed9138b75f54eaada0841b9b86 uri_adelie <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.219.3&entityid=002f3893385f710df69eeebe893144ff" # Gentoo penguin data from: https://doi.org/10.6073/pasta/2b1cff60f81640f182433d23e68541ce uri_gentoo <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.220.3&entityid=e03b43c924f226486f2f0ab6709d2381" # Chinstrap penguin data from: https://doi.org/10.6073/pasta/409c808f8fc9899d02401bdb04580af7 uri_chinstrap <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.221.2&entityid=fe853aa8f7a59aa84cdd3197619ef462" # Combining the URIs uris <- c(uri_adelie, uri_gentoo, uri_chinstrap) # Downloading and importing data library(purrr) library(readr) penguins_lter <- map_dfr(uris, read_csv) ``` -------------------------------- ### Load palmerpenguins Datasets Source: https://allisonhorst.github.io/palmerpenguins/index.html Load the palmerpenguins package and its datasets into your R environment. This makes the 'penguins' and 'penguins_raw' datasets available for use. ```R library(palmerpenguins) data(package = 'palmerpenguins') ``` -------------------------------- ### View First Few Rows of the 'penguins' Dataset Source: https://allisonhorst.github.io/palmerpenguins/index.html Displays the first 6 rows of the 'penguins' dataset, showing its structure and sample data. Use `?penguins` for more information. ```R head(penguins) #> # A tibble: 6 × 8 #> species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex #> #> 1 Adelie Torge… 39.1 18.7 181 3750 male #> 2 Adelie Torge… 39.5 17.4 186 3800 fema… #> 3 Adelie Torge… 40.3 18 195 3250 fema… #> 4 Adelie Torge… NA NA NA NA #> 5 Adelie Torge… 36.7 19.3 193 3450 fema… #> 6 Adelie Torge… 39.3 20.6 190 3650 male #> # … with 1 more variable: year ``` -------------------------------- ### Perform PCA with stats::prcomp Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Alternative method to calculate PCA rotations using base R stats. ```R penguins %>% dplyr::select(body_mass_g, ends_with("_mm")) %>% tidyr::drop_na() %>% scale() %>% prcomp() %>% .$rotation ``` -------------------------------- ### Create PCA Scores Plot with Loadings Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Generates a scatterplot of PCA scores (PC1 vs. PC2) with overlaid PCA loadings as arrows and text labels. Requires 'recipes::juice()' to extract scores from a recipe object and defines custom arrow styles. ```R # define arrow style arrow_style <- arrow(length = unit(.05, "inches"), type = "closed") pca_plot <- juice(penguin_recipe) %>% ggplot(aes(PC1, PC2)) + geom_point(aes(color = species, shape = species), alpha = 0.8, size = 2) + scale_colour_manual(values = c("darkorange","purple","cyan4")) pca_plot + geom_segment(data = pca_wider, aes(xend = PC1, yend = PC2), x = 0, y = 0, arrow = arrow_style) + geom_text(data = pca_wider, aes(x = PC1, y = PC2, label = terms), hjust = 0, vjust = 1, size = 5, color = '#0A537D') ``` -------------------------------- ### View First Few Rows of the 'penguins_raw' Dataset Source: https://allisonhorst.github.io/palmerpenguins/index.html Displays the first 6 rows of the 'penguins_raw' dataset, which contains all variables and original names. Use `?penguins_raw` for more information. ```R head(penguins_raw) #> # A tibble: 6 × 17 #> studyName `Sample Number` Species Region Island Stage `Individual ID` #> #> 1 PAL0708 1 Adelie Penguin … Anvers Torge… Adul… N1A1 #> 2 PAL0708 2 Adelie Penguin … Anvers Torge… Adul… N1A2 #> 3 PAL0708 3 Adelie Penguin … Anvers Torge… Adul… N2A1 #> 4 PAL0708 4 Adelie Penguin … Anvers Torge… Adul… N2A2 #> 5 PAL0708 5 Adelie Penguin … Anvers Torge… Adul… N3A1 #> 6 PAL0708 6 Adelie Penguin … Anvers Torge… Adul… N3A2 #> # … with 10 more variables: `Clutch Completion` , `Date Egg` , #> # `Culmen Length (mm)` , `Culmen Depth (mm)` , #> # `Flipper Length (mm)` , `Body Mass (g)` , Sex , #> # `Delta 15 N (o/oo)` , `Delta 13 C (o/oo)` , Comments ``` -------------------------------- ### Plot PCA Loadings Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Visualizes PCA loadings using ggplot2. Requires the 'tidytext' package for reordering terms. The plot facets by component and uses custom fill colors. ```R library(ggplot2) penguin_pca %>% mutate(terms = tidytext::reorder_within(terms, abs(value), component)) %>% ggplot(aes(abs(value), terms, fill = value > 0)) + geom_col() + facet_wrap(~component, scales = "free_y") + tidytext::scale_y_reordered() + scale_fill_manual(values = c("#b6dfe2", "#0A537D")) + labs( x = "Absolute value of contribution", y = NULL, fill = "Positive?" ) ``` -------------------------------- ### Create a histogram of penguin flipper lengths Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Generates a histogram showing the distribution of flipper lengths across different penguin species. ```R flipper_hist <- ggplot(data = penguins, aes(x = flipper_length_mm)) + geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") + scale_fill_manual(values = c("darkorange","purple","cyan4")) + labs(x = "Flipper length (mm)", y = "Frequency", title = "Penguin flipper lengths") flipper_hist ``` -------------------------------- ### Create a histogram of penguin body mass Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Generates a histogram showing the distribution of body mass across different penguin species. ```R mass_hist <- ggplot(data = penguins, aes(x = body_mass_g)) + geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") + scale_fill_manual(values = c("darkorange","purple","cyan4")) + labs(x = "Body mass (g)", y = "Frequency", title = "Penguin body mass") mass_hist ``` -------------------------------- ### Load libraries for data manipulation and visualization Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Loads the palmerpenguins, dplyr, and ggplot2 libraries, and sets the default ggplot theme to minimal. These are common libraries for data analysis in R. ```r library(palmerpenguins) library(dplyr) library(ggplot2) theme_set(theme_minimal()) ``` -------------------------------- ### Citation for palmerpenguins R Package Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Provides the recommended citation for the palmerpenguins R package, including a BibTeX entry for LaTeX users. ```r citation("palmerpenguins") #> To cite palmerpenguins in publications use: #> #> Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer #> Archipelago (Antarctica) penguin data. R package version 0.1.0. #> https://allisonhorst.github.io/palmerpenguins/. doi: #> 10.5281/zenodo.3960218. #> #> A BibTeX entry for LaTeX users is #> #> @Manual{ #> title = {palmerpenguins: Palmer Archipelago (Antarctica) penguin data}, #> author = {Allison Marie Horst and Alison Presmanes Hill and Kristen B Gorman}, #> year = {2020}, #> note = {R package version 0.1.0}, #> doi = {10.5281/zenodo.3960218}, #> url = {https://allisonhorst.github.io/palmerpenguins/}, #> } ``` -------------------------------- ### Create a faceted scatter plot of penguin dimensions Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Visualizes the relationship between flipper length and body mass, faceted by species and colored by sex. ```R ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = sex)) + scale_color_manual(values = c("darkorange","cyan4"), na.translate = FALSE) + labs(title = "Penguin flipper and body mass", subtitle = "Dimensions for male and female Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER", x = "Flipper length (mm)", y = "Body mass (g)", color = "Penguin sex") + theme(legend.position = "bottom", plot.title.position = "plot", plot.caption = element_text(hjust = 0, face= "italic"), plot.caption.position = "plot") + facet_wrap(~species) ``` -------------------------------- ### Accessing Data Files Source: https://allisonhorst.github.io/palmerpenguins/reference/index.html Functions to retrieve the file paths for the raw and processed penguin datasets. ```APIDOC ## path_to_file() ### Description Get file path to `penguins.csv` and `penguins_raw.csv` files. ### Method GET ### Endpoint /data/path ### Parameters #### Query Parameters - **file_type** (string) - Required - Specifies whether to get the path for 'penguins.csv' or 'penguins_raw.csv'. ### Response #### Success Response (200) - **filePath** (string) - The absolute path to the requested CSV file. ### Response Example ```json { "filePath": "/path/to/your/data/penguins.csv" } ``` ``` -------------------------------- ### Visualize penguin counts by sex and species Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Creates a bar plot showing the counts of penguins for each sex, filled by species. Uses custom colors and facets for better visualization. ```r ggplot(penguins, aes(x = sex, fill = species)) + geom_bar(alpha = 0.8) + scale_fill_manual(values = c("darkorange","purple","cyan4"), guide = FALSE) + theme_minimal() + facet_wrap(~species, ncol = 1) + coord_flip() ``` -------------------------------- ### Accessing Palmer Penguins Data Files Source: https://allisonhorst.github.io/palmerpenguins/reference/path_to_file.html This function provides the file path to the `penguins.csv` and `penguins_raw.csv` datasets bundled with the palmerpenguins package. It can list all available files or return the path to a specific file. ```APIDOC ## Get file path to `penguins.csv` and `penguins_raw.csv` files ### Description This function makes it easy to access the `penguins.csv` and `penguins_raw.csv` files, which are bundled in the package's `inst/extdata` directory. The data in each file is the same as the `penguins` and `penguins_raw` data frames, but without the variable types. ### Method `path_to_file` ### Parameters #### Arguments - **path** (character or NULL) - Optional - Name of the file in quotes with extension (e.g., `"penguins.csv"` or `"penguins_raw.csv"`). If `NULL`, the function will list all available example files. ### Examples __```R # List all available files path_to_file() #> [1] "penguins.csv" "penguins_raw.csv" # Get the path to a specific file path_to_file("penguins_raw.csv") #> [1] "/home/runner/work/_temp/Library/palmerpenguins/extdata/penguins_raw.csv" # Read the penguins.csv file into a data frame head(read.csv(path_to_file("penguins.csv"))) #> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g #> 1 Adelie Torgersen 39.1 18.7 181 3750 #> 2 Adelie Torgersen 39.5 17.4 186 3800 #> 3 Adelie Torgersen 40.3 18.0 195 3250 #> 4 Adelie Torgersen NA NA NA NA #> 5 Adelie Torgersen 36.7 19.3 193 3450 #> 6 Adelie Torgersen 39.3 20.6 190 3650 #> sex year #> 1 male 2007 #> 2 female 2007 #> 3 female 2007 #> 4 2007 #> 5 female 2007 #> 6 male 2007 ``` ``` -------------------------------- ### Dataset Descriptions Source: https://allisonhorst.github.io/palmerpenguins/reference/index.html Descriptions of the available penguin datasets. ```APIDOC ## penguins ### Description Size measurements for adult foraging penguins near Palmer Station, Antarctica. ### Data Fields - **species** (string) - Penguin species (Adelie, Chinstrap, or Gentoo). - **island** (string) - Name of the island the penguin was observed on (Torgersen, Biscoe, or Dream). - **bill_length_mm** (numeric) - Bill length in millimeters. - **bill_depth_mm** (numeric) - Bill depth in millimeters. - **flipper_length_mm** (numeric) - Flipper length in millimeters. - **body_mass_g** (numeric) - Body mass in grams. - **sex** (string) - Sex of the penguin (Male or Female). - **year** (integer) - Year of observation (2007, 2008, or 2009). ## penguins_raw ### Description Penguin size, clutch, and blood isotope data for foraging adults near Palmer Station, Antarctica. ### Data Fields - **species** (string) - Penguin species (Adelie, Chinstrap, or Gentoo). - **island** (string) - Name of the island the penguin was observed on (Torgersen, Biscoe, or Dream). - **bill_length_mm** (numeric) - Bill length in millimeters. - **bill_depth_mm** (numeric) - Bill depth in millimeters. - **flipper_length_mm** (numeric) - Flipper length in millimeters. - **body_mass_g** (numeric) - Body mass in grams. - **sex** (string) - Sex of the penguin (Male or Female). - **year** (integer) - Year of observation (2007, 2008, or 2009). - **culmen_length_mm** (numeric) - Culmen length in millimeters. - **culmen_depth_mm** (numeric) - Culmen depth in millimeters. - **datum** (string) - Date of observation. - **sex** (string) - Sex of the penguin (Male or Female). - **delta_15_N** (numeric) - Estimated value of nitrogen isotope delta 15N. - **delta_13_C** (numeric) - Estimated value of carbon isotope delta 13C. - **comments** (string) - Any relevant comments about the observation. - **id** (integer) - Unique identifier for the observation. ``` -------------------------------- ### Visualize penguin counts by island and species Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Creates a bar plot showing the counts of penguins for each island, filled by species. Uses custom colors and facets for better visualization. ```r ggplot(penguins, aes(x = island, fill = species)) + geom_bar(alpha = 0.8) + scale_fill_manual(values = c("darkorange","purple","cyan4"), guide = FALSE) + theme_minimal() + facet_wrap(~species, ncol = 1) + coord_flip() ``` -------------------------------- ### Visualize pairwise plot matrix Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Create a scatterplot matrix colored by species using GGally. ```R penguins %>% select(species, body_mass_g, ends_with("_mm")) %>% GGally::ggpairs(aes(color = species), columns = c("flipper_length_mm", "body_mass_g", "bill_length_mm", "bill_depth_mm")) + scale_colour_manual(values = c("darkorange","purple","cyan4")) + scale_fill_manual(values = c("darkorange","purple","cyan4")) ``` -------------------------------- ### Visualize PCA variance Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Plot the percentage of total variance explained by each principal component. ```R penguin_recipe %>% tidy(id = "pca", type = "variance") %>% dplyr::filter(terms == "percent variance") %>% ggplot(aes(x = component, y = value)) + geom_col(fill = "#b6dfe2") + xlim(c(0, 5)) + ylab("% of total variance") ``` -------------------------------- ### Structure of the 'penguins' Dataset Source: https://allisonhorst.github.io/palmerpenguins/index.html Shows the internal structure of the 'penguins' dataset, including data types and dimensions. This is useful for understanding the variables available. ```R str(penguins) #> tibble [344 × 8] (S3: tbl_df/tbl/data.frame) #> $ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ... #> $ island : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ... #> $ bill_length_mm : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ... #> $ bill_depth_mm : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ... #> $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ... #> $ body_mass_g : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ... #> $ sex : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ... #> $ year : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ... ``` -------------------------------- ### Calculate correlation matrix Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Compute and rearrange the correlation matrix for penguin size measurements. ```R library(corrr) penguins_corr <- penguins %>% dplyr::select(body_mass_g, ends_with("_mm")) %>% correlate() %>% rearrange() penguins_corr ``` -------------------------------- ### Scatterplot with Facets: Flipper Length vs. Body Mass by Species Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Displays a scatterplot of flipper length versus body mass, with points colored by sex and the plot faceted by species. The color legend for sex does not include NA values. ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = sex)) + scale_color_manual(values = c("darkorange","cyan4"), na.translate = FALSE) + facet_wrap(~species) ``` -------------------------------- ### Create a boxplot with jitter for flipper lengths Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Combines boxplots and jittered points to visualize the distribution of flipper lengths per species. ```R flipper_box <- ggplot(data = penguins, aes(x = species, y = flipper_length_mm)) + geom_boxplot(aes(color = species), width = 0.3, show.legend = FALSE) + geom_jitter(aes(color = species), alpha = 0.5, show.legend = FALSE, position = position_jitter(width = 0.2, seed = 0)) + scale_color_manual(values = c("darkorange","purple","cyan4")) + labs(x = "Species", y = "Flipper length (mm)") flipper_box ``` -------------------------------- ### Scatterplot: Flipper Length vs. Body Mass Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Creates a scatterplot showing the relationship between penguin flipper length and body mass, colored and shaped by species. ```r ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = species, shape = species), size = 2) + scale_color_manual(values = c("darkorange","darkorchid","cyan4")) ``` -------------------------------- ### Summarize Mean Flipper Length and Body Mass by Species Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Calculates the mean flipper length and body mass for each penguin species, ignoring missing values. This helps confirm observations from PCA plots. ```R penguins %>% group_by(species) %>% summarize(across(c(flipper_length_mm, body_mass_g), mean, na.rm = TRUE)) ``` -------------------------------- ### Plot PCA Loadings for PC2 and PC3 Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Plots PCA loadings for the second and third principal components, similar to the PC1/PC2 plot. It uses the same arrow style and text labeling for loadings. ```R pca_plot %+% aes(PC2, PC3) + geom_segment(data = pca_wider, aes(xend = PC2, yend = PC3), x = 0, y = 0, arrow = arrow_style) + geom_text(data = pca_wider, aes(x = PC2, y = PC3, label = terms), hjust = 0, vjust = 1, size = 5, color = '#0A537D') ``` -------------------------------- ### Summarize Mean Bill Depth and Length by Species Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Calculates the mean bill depth and bill length for each penguin species, ignoring missing values. This aids in understanding species separation along PC2. ```R penguins %>% group_by(species) %>% summarize(across(c(bill_depth_mm, bill_length_mm), mean, na.rm = TRUE)) ``` -------------------------------- ### Visualize missing data in penguins dataset Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Uses the vis_dat function to visualize missing data patterns within the 'penguins' dataset. This helps in identifying where data might be incomplete. ```r visdat::vis_dat(penguins) ``` -------------------------------- ### Display tidy penguins data Source: https://allisonhorst.github.io/palmerpenguins/articles/download.html Shows the first few rows of the `penguins` tibble, which contains a simplified and tidied version of the penguin data. This is the recommended dataset for most analyses. ```r head(penguins) #> # A tibble: 6 × 8 #> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g #> #> 1 Adelie Torgersen 39.1 18.7 181 3750 #> 2 Adelie Torgersen 39.5 17.4 186 3800 #> 3 Adelie Torgersen 40.3 18 195 3250 #> 4 Adelie Torgersen NA NA NA NA #> 5 Adelie Torgersen 36.7 19.3 193 3450 #> 6 Adelie Torgersen 39.3 20.6 190 3650 #> # ℹ 2 more variables: sex , year ``` -------------------------------- ### Histogram: Flipper Length by Species Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Displays histograms of flipper lengths for each penguin species, with bars filled by species. The histograms are overlaid using transparency to compare distributions. ```r ggplot(data = penguins, aes(x = flipper_length_mm)) + geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") + scale_fill_manual(values = c("darkorange","darkorchid","cyan4")) ``` -------------------------------- ### Scatterplot of Flipper Length vs. Body Mass Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Creates a scatterplot of flipper length against body mass, colored by species. This plot visually confirms the separation of Gentoo penguins seen in PCA. ```R ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, colour = species)) + geom_point() + scale_colour_manual(values = c("darkorange","purple","cyan4")) ``` -------------------------------- ### Explore factor variables in penguins dataset Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Selects and glimpses all factor variables from the 'penguins' dataset. This is useful for understanding categorical data. ```r penguins %>% dplyr::select(where(is.factor)) %>% glimpse() #> Rows: 344 #> Columns: 3 #> $ species Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie… #> $ island Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Torgers… #> $ sex male, female, female, NA, female, male, female, male, NA, NA, … ``` -------------------------------- ### Select and Glimpse Penguin Data Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Selects continuous variables related to body mass and measurements, then displays a summary of the selected data frame. ```r penguins %>% dplyr::select(body_mass_g, ends_with("_mm")) %>% glimpse() #> Rows: 344 #> Columns: 4 #> $ body_mass_g 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, … #> $ bill_length_mm 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, … #> $ bill_depth_mm 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, … #> $ flipper_length_mm 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186… ``` -------------------------------- ### Scatterplot of Bill Length vs. Bill Depth Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Creates a scatterplot of bill length against bill depth, colored by species. This plot visually demonstrates how bill dimensions can differentiate penguin species. ```R ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) + geom_point() + scale_colour_manual(values = c("darkorange","purple","cyan4")) ``` -------------------------------- ### Calculate Mean Numeric Variables by Species Source: https://allisonhorst.github.io/palmerpenguins/index.html Calculates the mean of all numeric variables for each penguin species, ignoring missing values. Requires the 'tidyverse' package. ```R library(tidyverse) penguins %>% group_by(species) %>% summarize(across(where(is.numeric), mean, na.rm = TRUE)) #> # A tibble: 3 × 6 #> species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g year #> #> 1 Adelie 38.8 18.3 190. 3701. 2008. #> 2 Chinstrap 48.8 18.4 196. 3733. 2008. #> 3 Gentoo 47.5 15.0 217. 5076. 2008. ``` -------------------------------- ### Summarize Penguin Counts by Species Source: https://allisonhorst.github.io/palmerpenguins/index.html Counts the number of penguins for each species in the dataset. Requires the 'tidyverse' package. ```R library(tidyverse) penguins %>% count(species) #> # A tibble: 3 × 2 #> species n #> #> 1 Adelie 152 #> 2 Chinstrap 68 #> 3 Gentoo 124 ``` -------------------------------- ### Penguin Mass vs. Flipper Length Scatter Plot Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Creates a scatter plot to visualize the relationship between penguin flipper length and body mass, with points colored and shaped by species. Includes custom labels and theme adjustments. Note the warning about deprecated theme arguments. ```r mass_flipper <- ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = species, shape = species), size = 3, alpha = 0.8) + scale_color_manual(values = c("darkorange","purple","cyan4")) + labs(title = "Penguin size, Palmer Station LTER", subtitle = "Flipper length and body mass for Adelie, Chinstrap and Gentoo Penguins", x = "Flipper length (mm)", y = "Body mass (g)", color = "Penguin species", shape = "Penguin species") + theme(legend.position = c(0.2, 0.7), plot.title.position = "plot", plot.caption = element_text(hjust = 0, face= "italic"), plot.caption.position = "plot") mass_flipper ``` -------------------------------- ### Pairwise Correlation Plot of Penguin Data Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Creates a matrix of scatterplots and density plots for selected continuous variables, colored by species, to visualize pairwise relationships and distributions. ```r penguins %>% select(species, body_mass_g, ends_with("_mm")) %>% GGally::ggpairs(aes(color = species)) + scale_colour_manual(values = c("darkorange","purple","cyan4")) + scale_fill_manual(values = c("darkorange","purple","cyan4")) ``` -------------------------------- ### Palmer Penguins Citation in BibTeX Source: https://allisonhorst.github.io/palmerpenguins/authors.html This is the citation for the palmerpenguins R package in BibTeX format, as found in the inst/CITATION file. Use this for academic references. ```bibtex @Manual{ title = {palmerpenguins: Palmer Archipelago (Antarctica) penguin data}, author = {Allison Marie Horst and Alison Presmanes Hill and Kristen B Gorman}, year = {2020}, note = {R package version 0.1.0}, doi = {10.5281/zenodo.3960218}, url = {https://allisonhorst.github.io/palmerpenguins/}, } ``` -------------------------------- ### Prepare PCA Loadings for Wider Format Source: https://allisonhorst.github.io/palmerpenguins/articles/pca.html Reshapes the PCA loadings data frame into a wider format using tidyr::pivot_wider. This is necessary for plotting PCA scores with loadings. ```R # get pca loadings into wider format pca_wider <- penguin_pca %>% tidyr::pivot_wider(names_from = component, id_cols = terms) ``` -------------------------------- ### Count penguins by species and sex Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Counts the number of penguins for each combination of species and sex. The `.drop = FALSE` argument ensures all combinations are shown, including NA values for sex. ```r penguins %>% count(species, sex, .drop = FALSE) #> # A tibble: 8 × 3 #> species sex n #> #> 1 Adelie female 73 #> 2 Adelie male 73 #> 3 Adelie NA 6 #> 4 Chinstrap female 34 #> 5 Chinstrap male 34 #> 6 Gentoo female 58 #> 7 Gentoo male 61 #> 8 Gentoo NA 5 ``` -------------------------------- ### Bill Length vs. Bill Depth Scatter Plot with Regression Line Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Visualizes the relationship between penguin bill length and bill depth, including a linear regression line for each species. Points are colored and shaped by species. Note warnings about removed rows with non-finite or missing values. ```r bill_len_dep <- ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm, group = species)) + geom_point(aes(color = species, shape = species), size = 3, alpha = 0.8) + geom_smooth(method = "lm", se = FALSE, aes(color = species)) + scale_color_manual(values = c("darkorange","purple","cyan4")) + labs(title = "Penguin bill dimensions", subtitle = "Bill length and depth for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER", x = "Bill length (mm)", y = "Bill depth (mm)", color = "Penguin species", shape = "Penguin species") + theme(legend.position = c(0.85, 0.15), plot.title.position = "plot", plot.caption = element_text(hjust = 0, face= "italic"), plot.caption.position = "plot") bill_len_dep ``` -------------------------------- ### Display raw penguins data Source: https://allisonhorst.github.io/palmerpenguins/articles/download.html Shows the first few rows of the `penguins_raw` tibble, which contains the original, less processed data. This is useful for understanding the raw structure before tidying. ```r head(penguins_raw) #> # A tibble: 6 × 17 #> studyName `Sample Number` Species Region Island Stage `Individual ID` #> #> 1 PAL0708 1 Adelie Penguin … Anvers Torge… Adul… N1A1 #> 2 PAL0708 2 Adelie Penguin … Anvers Torge… Adul… N1A2 #> 3 PAL0708 3 Adelie Penguin … Anvers Torge… Adul… N2A1 #> 4 PAL0708 4 Adelie Penguin … Anvers Torge… Adul… N2A2 #> 5 PAL0708 5 Adelie Penguin … Anvers Torge… Adul… N3A1 #> 6 PAL0708 6 Adelie Penguin … Anvers Torge… Adul… N3A2 #> # ℹ 10 more variables: `Clutch Completion` , `Date Egg` , #> # `Culmen Length (mm)` , `Culmen Depth (mm)` , #> # `Flipper Length (mm)` , `Body Mass (g)` , Sex , #> # `Delta 15 N (o/oo)` , `Delta 13 C (o/oo)` , Comments ``` -------------------------------- ### Count penguins by species and island Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Counts the number of penguins for each combination of species and island. The `.drop = FALSE` argument ensures all combinations are shown, even if the count is zero. ```r penguins %>% count(species, island, .drop = FALSE) #> # A tibble: 9 × 3 #> species island n #> #> 1 Adelie Biscoe 44 #> 2 Adelie Dream 56 #> 3 Adelie Torgersen 52 #> 4 Chinstrap Biscoe 0 #> 5 Chinstrap Dream 68 #> 6 Chinstrap Torgersen 0 #> 7 Gentoo Biscoe 124 #> 8 Gentoo Dream 0 #> 9 Gentoo Torgersen 0 ``` -------------------------------- ### Bill Length vs. Bill Depth Scatter Plot (Omitting Species) Source: https://allisonhorst.github.io/palmerpenguins/articles/examples.html Displays a scatter plot of penguin bill length versus bill depth without differentiating by species, showing an overall linear trend. Includes a single gray regression line. Note warnings about removed rows with non-finite or missing values. ```r bill_no_species <- ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm)) + geom_point() + scale_color_manual(values = c("darkorange","purple","cyan4")) + labs(title = "Penguin bill dimensions (omit species)", subtitle = "Palmer Station LTER", x = "Bill length (mm)", y = "Bill depth (mm)") + theme(plot.title.position = "plot", plot.caption = element_text(hjust = 0, face= "italic"), plot.caption.position = "plot") + geom_smooth(method = "lm", se = FALSE, color = "gray50") bill_no_species ``` -------------------------------- ### Jitter Plot: Bill Length by Species Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html Visualizes the distribution of bill lengths for each penguin species using a jitter plot, with points colored by species. The jitter is applied horizontally to prevent overplotting. ```r ggplot(data = penguins, aes(x = species, y = bill_length_mm)) + geom_jitter(aes(color = species), width = 0.1, alpha = 0.7, show.legend = FALSE) + scale_color_manual(values = c("darkorange","darkorchid","cyan4")) ```