Example of R codes

r
code
analysis
dataviz
visualization
Author

Below is an example to illustrate how to include executable code.

R Code:

Code
1 + 1
[1] 2

Data Visualizations

Generate Example graphs using the penguins data from palmerpenguins using R and GGPlot2

About the data

Data were collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica LTER, a member of the Long Term Ecological Research Network.

The dataset contain data for 344 penguins. There are 3 different species of penguins in this dataset, collected from 3 islands in the Palmer Archipelago, Antarctica.

Artwork

You are invited to use this accompanying artwork when you use or teach with palmerpenguins! Please cite with "Artwork by @allison_horst".

Meet the Palmer penguins

Bill dimensions

The culmen is the upper ridge of a bird’s bill. In the simplified penguins data, culmen length and depth are renamed as variables bill_length_mm and bill_depth_mm to be more intuitive.

For this penguin data, the culmen (bill) length and depth are measured as shown below (thanks Kristen Gorman for clarifying!):

Installation

You can install the released version of palmerpenguins from CRAN with:

install.packages("palmerpenguins")

To install the development version from GitHub use:

install.packages("remotes") 
remotes::install_github("allisonhorst/palmerpenguins")
OR
remotes::install_github("mohitshrestha/automaton")

Code
# To install the released version from CRAN use:
# install.packages("ggplot2")
# install.packages("palmerpenguins")

# To install the development version from GitHub use:
# install.packages("remotes")
# remotes::install_github("allisonhorst/palmerpenguins")
# remotes::install_github("mohitshrestha/automaton")

library(ggplot2)
library(palmerpenguins)
ggplot2::theme_set(ggplot2::theme_minimal())

Dataset Penguins are fun to Summarize and Visualize!

Code
library(tidyverse)
library(gt)

penguins |> 
    count(species) |> 
    gt()
species n
Adelie 152
Chinstrap 68
Gentoo 124
Code
penguins |>
    summarize(
        across(where(is.numeric), mean, na.rm = TRUE),
        .by = c(species)
    ) |> 
    gt() |> 
    fmt_number(
        columns = contains("mm") | contains("g")
    ) |> 
    fmt_integer(
        columns = contains("year"),
        sep_mark = ""
    )
species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g year
Adelie 38.79 18.35 189.95 3,700.66 2008
Gentoo 47.50 14.98 217.19 5,076.02 2008
Chinstrap 48.83 18.42 195.82 3,733.09 2008

Generate Visualizations

Penguin body mass versus flipper length

Code
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

Penguin flipper length versus bill length

Code
flipper_bill <- ggplot(data = penguins,
                         aes(x = flipper_length_mm,
                             y = bill_length_mm)) +
  geom_point(aes(color = species, 
                 shape = species),
             size = 3,
             alpha = 0.8) +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(title = "Flipper and bill length",
       subtitle = "Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
       x = "Flipper length (mm)",
       y = "Bill length (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")

flipper_bill

Penguin bill length versus depth

Code
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

Histogram: Penguin flipper lengths

Code
flipper_hist <- ggplot(data = penguins, aes(x = flipper_length_mm)) +
  geom_histogram(aes(fill = species), 
                 alpha = 0.5, 
                 position = "identity",
                 bins = 30) +
  scale_fill_manual(values = c("darkorange","purple","cyan4")) +
  labs(x = "Flipper length (mm)",
       y = "Frequency",
       title = "Penguin flipper lengths")

flipper_hist

BoxPlot & Jitter: Penguin flipper lengths

Code
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)",
       title = "Penguin flipper lengths")

flipper_box

Histogram: Penguin Body mass

Code
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

Scatterplot: Penguin flipper and body mass by sex

Code
flipper_length_body_mass <- 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)

flipper_length_body_mass

Interactive Visualization

Plotly: Scatterplot Penguin flipper and body mass by sex

Code
library(plotly)
library(ggplot2)
library(palmerpenguins)
ggplot2::theme_set(ggplot2::theme_minimal())

flipper_length_body_mass <- 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)

ggplotly(flipper_length_body_mass,
         tooltip = c("x", "y", "color"))

Citation

Code
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/},
  }

Reuse

Citation

BibTeX citation:
@online{shrestha,
  author = {Shrestha, Mohit},
  title = {Example of {R} Codes},
  url = {https://mohitshrestha.github.io/posts/example_r_codes},
  langid = {en}
}
For attribution, please cite this work as:
Shrestha, Mohit. n.d. “Example of R Codes.” https://mohitshrestha.github.io/posts/example_r_codes.