# Loading needed packages
library(palmerpenguins)
library(tidyverse)
library(knitr)
library(ggplot2)
library(skimr)

Example: Palmer Penguins data

data(penguins, package = "palmerpenguins")

glimpse(penguins)
## Rows: 344
## Columns: 8
## $ species           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
## $ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
## $ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
## $ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
## $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
## $ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
## $ sex               <fct> male, female, female, NA, female, male, female, male…
## $ year              <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…

Plot in Layers

ggplot(data=penguins)

Mapping bill depth to x-axis

ggplot(data = penguins,
       mapping = aes(x = bill_depth_mm))

Adding bill length to Y-axis

ggplot(data = penguins,
       mapping = aes(x = bill_depth_mm,
                     y = bill_length_mm))

Creating scatter plot with points using geom_point() layer

ggplot(data = penguins,
       mapping = aes(x = bill_depth_mm,
                     y = bill_length_mm)) + 
  geom_point()

Asthetics

Adding color to plot based on species of penguin and customizing the title and sub-title using labs() function

ggplot(data = penguins,
       mapping = aes(x = bill_depth_mm,
                     y = bill_length_mm,
                     color = species)) + 
  geom_point() + 
  labs(title = "Penguin bill length by depth",
       subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo penguins")

Customizing the axis labels, legend title, and using a discrete color scale to make it better for viewers

ggplot(data = penguins, mapping = aes(bill_depth_mm,y=bill_length_mm,color =species))+
  geom_point()+
  labs(title = "Penguin bill length by depth",
       subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo penguins",caption = "Source: Palmer station LTER/palemerpenguins package",x="Bill depth (mm)",y="Bill length (mm)")+
         scale_color_viridis_d()

Mapping vs Setting

ggplot(penguins,
       aes(x = bill_depth_mm,
           y = bill_length_mm,
           size = body_mass_g, 
           alpha = flipper_length_mm)) + 
  geom_point()

ggplot(penguins,
       aes(x = bill_depth_mm,
           y = bill_length_mm)) + 
  geom_point(size = 2, alpha = 0.5)

Try on your own

Recreating the scatter plot penguin bill length by depth

Modified the shape of points based on the island the penguin is from using the shape aesthetic.

Using the size aesthetic, modified the size of each point based on the body mass of each penguin.

Transperency of each point is modified based on the penguin’s flipper length using alpha aesthetic.

The size aesthetic was set to size of 2.

ggplot(data = penguins, mapping = aes(bill_depth_mm,y=bill_length_mm,color =species,size=body_mass_g,shape=island,alpha=flipper_length_mm))+
  geom_point(size=2)+
  labs(title = "Penguin bill length by depth",
       subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo penguins",caption = "Source: Palmer station LTER/palemerpenguins package",x="Bill depth (mm)",y="Bill length (mm)")+
         scale_color_viridis_d()

Yes, there was some missing data in the dataset as shown below using the skim() function.The plot was removing the missing data from the output.

skim(penguins)
Data summary
Name penguins
Number of rows 344
Number of columns 8
_______________________
Column type frequency:
factor 3
numeric 5
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
species 0 1.00 FALSE 3 Ade: 152, Gen: 124, Chi: 68
island 0 1.00 FALSE 3 Bis: 168, Dre: 124, Tor: 52
sex 11 0.97 FALSE 2 mal: 168, fem: 165

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
bill_length_mm 2 0.99 43.92 5.46 32.1 39.23 44.45 48.5 59.6 ▃▇▇▆▁
bill_depth_mm 2 0.99 17.15 1.97 13.1 15.60 17.30 18.7 21.5 ▅▅▇▇▂
flipper_length_mm 2 0.99 200.92 14.06 172.0 190.00 197.00 213.0 231.0 ▂▇▃▅▂
body_mass_g 2 0.99 4201.75 801.95 2700.0 3550.00 4050.00 4750.0 6300.0 ▃▇▆▃▂
year 0 1.00 2008.03 0.82 2007.0 2007.00 2008.00 2009.0 2009.0 ▇▁▇▁▇

This scatter plot displays the relationship between each penguin’s flipper length(mm) and its bill depth(mm)

ggplot(data = penguins, mapping = aes(x=bill_depth_mm,y=flipper_length_mm,color =species,size=body_mass_g,shape=island,alpha=flipper_length_mm))+
  geom_point()+
  labs(title = "Penguin flipper length by bill depth",
       subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo penguins",caption = "Source: Palmer station LTER/palemerpenguins package",x="Bill depth (mm)",y="Bill length (mm)")+
         scale_color_viridis_d()