### Principal Component Analysis (PCA) with Palmer Penguins (R) Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Illustrates how to perform Principal Component Analysis (PCA) on the penguins dataset using the recipes package from tidymodels. This example shows how physical measurements can be used to differentiate between the three penguin species. ```r library(palmerpenguins) library(recipes) library(dplyr) library(tidyr) library(ggplot2) ``` -------------------------------- ### Install palmerpenguins R Package Source: https://github.com/allisonhorst/palmerpenguins/blob/main/README.md Provides R code for installing the palmerpenguins package from CRAN and the development version from GitHub. ```R install.packages("palmerpenguins") ``` ```R # install.packages("remotes") remotes::install_github("allisonhorst/palmerpenguins") ``` -------------------------------- ### Data Visualization with ggplot2 using Palmer Penguins (R) Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Demonstrates creating various visualizations from the penguins dataset using ggplot2. Examples include scatterplots of size measurements, histograms, and faceted plots to explore relationships and differences between penguin species and sexes. ```r library(palmerpenguins) library(ggplot2) # Scatterplot: Flipper length vs body mass by species 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_minimal() # Bill dimensions scatterplot with linear regression 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", x = "Bill length (mm)", y = "Bill depth (mm)") + theme_minimal() # Histogram of flipper lengths by species 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") + theme_minimal() # Faceted plot by species showing sex differences 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", x = "Flipper length (mm)", y = "Body mass (g)", color = "Penguin sex") + facet_wrap(~species) + theme_minimal() ``` -------------------------------- ### Load and Inspect palmerpenguins Data Source: https://github.com/allisonhorst/palmerpenguins/blob/main/README.md Demonstrates how to load the datasets from the palmerpenguins package and view their structure and first few rows. ```R library(palmerpenguins) data(package = 'palmerpenguins') head(penguins) head(penguins_raw) str(penguins) ``` -------------------------------- ### Create and Extract PCA using tidymodels Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt This code snippet demonstrates how to create a Principal Component Analysis (PCA) recipe using the tidymodels framework. It preprocesses the penguin data by handling missing values and normalizing predictors, then applies PCA. Finally, it extracts and displays the PCA loadings. ```r 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() # Extract PCA loadings penguin_pca <- penguin_recipe %>% tidy(id = "pca") print(penguin_pca) ``` -------------------------------- ### Retrieve Package Citation Source: https://github.com/allisonhorst/palmerpenguins/blob/main/README.md Shows how to generate the formal citation for the palmerpenguins package within an R environment. ```r citation("palmerpenguins") ``` -------------------------------- ### Accessing Palmer Penguins CSV Files (R) Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt The path_to_file() function in the palmerpenguins package provides access to the bundled CSV files. It can list available files or return the full system path for a specified file, enabling direct reading into R data frames. ```r library(palmerpenguins) # List available CSV files path_to_file() #> [1] "penguins.csv" "penguins_raw.csv" # Get path to specific CSV file path_to_file("penguins.csv") #> [1] "/path/to/palmerpenguins/extdata/penguins.csv" # Read CSV directly using the path penguins_csv <- read.csv(path_to_file("penguins.csv")) head(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 #> ... # Read raw data CSV penguins_raw_csv <- read.csv(path_to_file("penguins_raw.csv")) ``` -------------------------------- ### Summarize Penguin Data with Tidyverse Source: https://github.com/allisonhorst/palmerpenguins/blob/main/README.md Demonstrates how to count penguin species and calculate mean values for numeric variables grouped by species using the tidyverse package. ```r library(tidyverse) penguins %>% count(species) penguins %>% group_by(species) %>% summarize(across(where(is.numeric), mean, na.rm = TRUE)) ``` -------------------------------- ### Load and Inspect penguins_raw Dataset in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Loads the comprehensive 'penguins_raw' dataset from the palmerpenguins package and displays the first few rows using head(). This dataset contains more variables than the simplified 'penguins' dataset. ```r library(palmerpenguins) data("penguins_raw") head(penguins_raw) ``` -------------------------------- ### Perform PCA using Base R prcomp Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt This code snippet shows an alternative method for performing PCA using base R's `prcomp` function. It selects relevant numeric columns, removes missing values, scales the data, and then computes the PCA. The resulting rotation matrix (loadings) is then displayed. ```r pca_result <- penguins %>% select(body_mass_g, ends_with("_mm")) %>% drop_na() %>% scale() %>% prcomp() print(pca_result$rotation) ``` -------------------------------- ### Load and Inspect penguins Dataset in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Loads the simplified 'penguins' dataset from the palmerpenguins package and displays its structure and a glimpse of the data. Requires the 'dplyr' package for data manipulation. ```r library(palmerpenguins) library(dplyr) data("penguins") glimpse(penguins) ``` -------------------------------- ### Download Penguin Data from EDI Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt This code snippet demonstrates how to download the Palmer Penguins dataset directly from the Environmental Data Initiative (EDI) using provided URLs. It utilizes the `purrr` and `readr` packages to map over the URLs, read the CSV data, and combine them into a single tibble. ```r library(purrr) library(readr) # URLs for each species dataset from EDI uri_adelie <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.219.3&entityid=002f3893385f710df69eeebe893144ff" uri_gentoo <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.220.3&entityid=e03b43c924f226486f2f0ab6709d2381" uri_chinstrap <- "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.221.2&entityid=fe853aa8f7a59aa84cdd3197619ef462" # Combine URIs uris <- c(uri_adelie, uri_gentoo, uri_chinstrap) # Download and combine all species data penguins_lter <- map_dfr(uris, read_csv) # Compare with package data print(identical(nrow(penguins_lter), nrow(penguins_raw))) ``` -------------------------------- ### View All Column Names of penguins_raw in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Displays all 17 column names present in the 'penguins_raw' dataset. This is useful for understanding the full scope of data available. ```r library(palmerpenguins) data("penguins_raw") names(penguins_raw) ``` -------------------------------- ### Correlation and Statistical Analysis with Palmer Penguins (R) Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Shows how to compute and visualize correlation matrices for numeric variables in the penguins dataset using the corrr and dplyr packages. It also demonstrates creating a pairs plot with GGally to visualize pairwise relationships. ```r library(palmerpenguins) library(corrr) library(dplyr) # Compute correlation matrix for numeric variables penguins_corr <- penguins %>% select(body_mass_g, ends_with("_mm")) %>% correlate() %>% rearrange() penguins_corr #> # A tibble: 4 x 5 #> rowname flipper_length_mm body_mass_g bill_length_mm bill_depth_mm #> #> 1 flipper_length_mm NA 0.871 0.656 -0.584 #> 2 body_mass_g 0.871 NA 0.595 -0.472 #> 3 bill_length_mm 0.656 0.595 NA -0.235 #> 4 bill_depth_mm -0.584 -0.472 -0.235 NA # Pairs plot with GGally library(GGally) penguins %>% select(species, body_mass_g, ends_with("_mm")) %>% 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")) ``` -------------------------------- ### Extract Isotope Data from penguins_raw in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Selects and displays the first few rows of Species, Delta 15 N, and Delta 13 C columns from the 'penguins_raw' dataset after removing rows with any missing values in these columns. Uses dplyr for data manipulation. ```r library(palmerpenguins) library(dplyr) data("penguins_raw") penguins_raw %>% select(Species, `Delta 15 N (o/oo)`, `Delta 13 C (o/oo)`) na.omit() %>% head() ``` -------------------------------- ### Summarize Penguin Statistics by Species in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Calculates the mean of bill length, bill depth, flipper length, and body mass for each penguin species using dplyr. It handles missing values by removing them before calculation. ```r library(palmerpenguins) library(dplyr) data("penguins") penguins %>% group_by(species) %>% summarize( across(c(flipper_length_mm, body_mass_g, bill_length_mm, bill_depth_mm), mean, na.rm = TRUE) ) ``` -------------------------------- ### Plot PCA Scores with ggplot2 Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt This code snippet visualizes the PCA results by plotting the principal component scores (PC1 vs. PC2). It uses ggplot2 to create a scatter plot, coloring and shaping the points by species, and includes labels and a minimal theme for clarity. ```r 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")) + labs(title = "PCA of Palmer Penguins", x = "Principal Component 1", y = "Principal Component 2") + theme_minimal() ``` -------------------------------- ### Count Penguins by Species and Island in R Source: https://context7.com/allisonhorst/palmerpenguins/llms.txt Uses dplyr to count the number of penguins for each combination of species and island from the 'penguins' dataset. The '.drop = FALSE' argument ensures all combinations are shown, even those with zero counts. ```r library(palmerpenguins) library(dplyr) data("penguins") penguins %>% count(species, island, .drop = FALSE) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.