library(jpeg)
library(png)
library(tibble)
library(tidyverse)
library(palmerpenguins)
library(vtable)
library(ggplot2)
Workbook 3
LOADING PACKAGES
LOADING DATA
penguins
# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
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
7 Adelie Torgersen 38.9 17.8 181 3625
8 Adelie Torgersen 39.2 19.6 195 4675
9 Adelie Torgersen 34.1 18.1 193 3475
10 Adelie Torgersen 42 20.2 190 4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <int>
HISTOGRAMS
data("penguins")
%>%
penguins group_by(species) %>%
ggplot(aes(x=bill_length_mm, color=species, fill=species))+
geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).
BOXPLOT
# Create a boxplot comparing body mass across different penguin species
ggplot(penguins, aes(x= species, y = body_mass_g, fill = species)) +
geom_boxplot() +
labs(title = "Penguin Body Mass by Species",
x = "Species",
y = "Body Mass (g)") +
theme_minimal()
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).
GEOM BAR 1
%>%
penguins ggplot(aes(x = island,
color = island,
fill = island)) +
geom_bar(alpha = 0.6) +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 14))
GEOM BAR 2
# Create a bar plot for species count by year
%>%
penguins ggplot(aes(x = factor(year), # Treat year as a factor (categorical variable)
color = species, # Color bars by species
fill = species)) + # Fill bars by species
geom_bar(position = "dodge") + # Dodge bars to avoid stacking (side-by-side bars)
labs(title = "Penguin Species Count by Year", # Add plot title
x = "Year", # Label x-axis
y = "Count") + # Label y-axis
theme_minimal() + # Minimal theme for cleaner look
theme(axis.text = element_text(size = 14), # Adjust text size for axis labels
axis.title = element_text(size = 16), # Adjust text size for axis titles
plot.title = element_text(size = 18, face = "bold")) # Adjust plot title size and bold
GEOM BAR 3
library(tidyverse)
library(palmerpenguins)
# Create a bar plot for species count by island
%>%
penguins ggplot(aes(x = island, # X-axis is 'island'
color = species, # Color bars by species
fill = species)) + # Fill bars by species
geom_bar(position = "dodge", # Bars are dodged (side-by-side for each species)
alpha = 0.7) + # Set transparency for fill colors
labs(title = "Penguin Species Distribution by Island", # Plot title
x = "Island", # X-axis label
y = "Count") + # Y-axis label
theme_minimal() + # Minimal theme for cleaner look
theme(axis.text = element_text(size = 14), # Adjust text size for axis labels
axis.title = element_text(size = 16), # Adjust text size for axis titles
plot.title = element_text(size = 18, face = "bold"), # Plot title size and bold
legend.title = element_text(size = 14), # Legend title size
legend.text = element_text(size = 12)) # Legend text size
GEOM POINT 1
%>%
penguins ggplot(aes(x=bill_length_mm,
y = bill_depth_mm))+
geom_point()+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=16))
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
GEOM POINT 2
# Create a scatter plot of flipper length vs. body mass
%>%
penguins ggplot(aes(x = flipper_length_mm, # X-axis: Flipper Length
y = body_mass_g, # Y-axis: Body Mass
color = species, # Color points by species
shape = species)) + # Use different shapes for each species
geom_point(size = 3, alpha = 0.7) + # Scatter plot with point size and transparency
labs(title = "Flipper Length vs. Body Mass of Penguins", # Plot title
x = "Flipper Length (mm)", # X-axis label
y = "Body Mass (g)") + # Y-axis label
theme_minimal() + # Minimal theme for cleaner look
theme(axis.text = element_text(size = 14),
# Adjust text size for axis labels
axis.title = element_text(size = 16),
# Adjust text size for axis titles
plot.title = element_text(size = 18, face = "bold"),
# Plot title size and bold
legend.title = element_text(size = 14), # Legend title size
legend.text = element_text(size = 12)) # Legend text size
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
GEOM BOXPLOT 1
#| label: GEOM BOXPLOT 1
# Create a boxplot using penguin data
ggplot(penguins, aes(x = species,
y = body_mass_g,
fill = sex)) +
geom_boxplot(alpha = 0.7, outlier.size = 2) + # Adjust alpha and outlier size as needed
labs(title = "Boxplot of Body Mass by Penguin Species and Sex",
x = "Species",
y = "Body Mass (g)") +
theme_minimal() + # Use a minimal theme
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16),
legend.title = element_blank()) + # Optional: Remove legend title
scale_fill_manual(values = c("female" = "#FF9999", "male" = "#9999FF")) # Custom colors for sex
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).
GEOM BOXPLOT 2
# Create a boxplot for body mass by sex
ggplot(penguins, aes(x = sex,
y = body_mass_g,
fill = species)) +
geom_boxplot(alpha = 0.7, outlier.size = 2) + # Boxplot with adjusted transparency
labs(title = "Boxplot of Body Mass by Sex",
x = "Sex",
y = "Body Mass (g)") +
theme_minimal() + # Use a minimal theme
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16),
legend.position = "top") + # Position the legend at the top
scale_fill_brewer(palette = "Set2") # Use a color palette for species
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).
GEOM DENSITY
# Create density plots for penguin traits
%>%
penguins na.omit() %>%
pivot_longer(bill_length_mm:body_mass_g, names_to = "trait") %>%
ggplot(aes(x = value,
group = species,
fill = species,
color = species)) +
geom_density(alpha = 0.5, adjust = 1.5) + # Adjusted alpha for better visibility and bandwidth
facet_grid(~ trait, scales = "free_x") + # Free x-scales for each trait
labs(title = "Density Plots of Penguin Traits",
x = "Value",
y = "Density") + # Added labels
theme_minimal() + # Minimal theme
theme(axis.text = element_text(size = 16),
axis.title = element_text(size = 16),
legend.position = "top") + # Position legend at the top
scale_fill_brewer(palette = "Set2") + # Color palette for fill
scale_color_brewer(palette = "Set2") # Color palette for outline