Homework

Practice Task 1

Getting Started & Installing Packages

install.packages('ggplot2') #install ggplot
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(ggplot2) #load in ggplot
install.packages('patchwork') #install patckwork
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(patchwork) #load in patchwork
install.packages('palmerpenguins') #install palmerpenguins package
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(palmerpenguins) #load in palmerpenguins
## 
## Attaching package: 'palmerpenguins'
## The following objects are masked from 'package:datasets':
## 
##     penguins, penguins_raw
data(penguins) #load in penguin data

Wrapped Plot

peng_w <- ggplot(penguins, aes(x= species, y= year, color= island)) #specify dataset name, set x/y axes and specify color grouping by island
peng_w <- peng_w + geom_point() #create scatterplot
peng_w <- peng_w + facet_wrap(~island) #use facet_wrap command, by island
peng_w <- peng_w + labs(title = 'Scatter Plot: facet wrap by island', #title
                  x = 'Species', #x axis name
                  y = 'Year') #y axis name
peng_w <- peng_w + theme_bw() #clean theme
peng_w #load/view the plot

Grid Plot

peng_g <- ggplot(penguins, aes(x= island, y= body_mass_g, color=island)) #specify dataset name, set x/y axes and specify color grouping by island
peng_g <- peng_g + geom_point(na.rm=TRUE) #create scatterplot and remove N/A data
peng_g <- peng_g + facet_grid(species~sex) #use facet_grid command, by species~sex
peng_g <- peng_g + labs(title = 'Facet grid: species vs. sex', #title
                  x = 'Island', #x axis name
                  y = 'Body Mass(g)') #y axis name
peng_g <- peng_g + theme_bw() #clean theme
peng_g #load/view plot

Practice Task 2

Creating 3 Plots:

library(tidyverse) #load in tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.1     ✔ tidyr     1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
dat <- palmerpenguins::penguins %>% filter(!is.na(sex)) #defining 'dat' variable

scatter_plot <- dat %>% #name plot 
  ggplot(aes(x = bill_length_mm, y = bill_depth_mm, fill = species))+ #set x/y axes and fill by species
  geom_jitter(size=2, alpha= 0.6, shape = 16) #create scatterplot, adjust size, opacity, and shape

bar_plot <- dat %>% #name plot
  ggplot(aes(x= species, fill= species))+ #set x/y axes and fill by species
  geom_bar() #make it a barplot

density_plot <- dat %>% #name plot
  ggplot(aes(x=body_mass_g, fill= species))+ #set x/y axes and fill by species
  geom_density() #make it a density plot

Side-by-Side:

library(patchwork) #ensure patchwork package is installed to combine the graphs
scatter_plot + bar_plot + density_plot #use + symbols for side-by-side (default) layout

Stacked Layout:

scatter_plot / bar_plot / density_plot #use slashes to indicate stacked layout

Different Sizes:

scatter_plot / bar_plot / density_plot +
  plot_layout(height = c(1, 3, 1)) #specify height

Practice Task 3

Exporting:

patch_plot <- scatter_plot + bar_plot + density_plot #save plot as its own variable
ggsave("Async_49_Figure.png", plot = patch_plot, dpi = 300) #save as 300 dpi png
## Saving 7 x 5 in image
ggsave("Async_49_Figure.pdf", plot = patch_plot) #save as pdf
## Saving 7 x 5 in image
write_csv(dat, "penguins_clean.csv") #save and name the csv
ggsave(
  "penguin_plot.png", #save as png
  plot = scatter_plot + bar_plot + density_plot, #what is being plotted
  width = 5, 
  height = 8,
  units = "in",
  dpi = 300
)