Setting up my environment

Notes: setting up my R environment by loading the tidyverse and palmerpenguins package

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(palmerpenguins)

Creating a scatterplot

Notes: Start with a base scatter plot using the geom_point function and as for your (aesthetic) feature aes “add flipper_length_mm” as the x axis and “body_mass_g” as the y axis

ggplot(data = penguins) + 
  geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g))
## Warning: Removed 2 rows containing missing values (geom_point).

Adding Color

Notes: Color is part of our aesthetic. We want to differentiate the species of penguin by color. We are going to add a comma after the y=body_mass_g part of the code and enter color=species before the end of the parenthesis

ggplot(data = penguins) + 
  geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g,color=species))
## Warning: Removed 2 rows containing missing values (geom_point).

Titles

Notes: We are going to add a title and subtitle to our scatter-plot. To add another layer to our plot add + after the parenthesis and always press enter to start a new line of code.

Begin the label addition of code with labs(title=" to create a title for the plot which will be “Palmer Penguins: Body Mass vs. Filpper Length”. Then write a coma and add subtitle=" which will be “Sample of Three Penguin Species” Make sure your Title text begins and ends with a quoation mark.

ggplot(data=penguins)+
  geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g,color=species))+
  labs(title="Palmer Penguins: Body Mass vs. Flipper Length",subtitle="Sample of Three Penguin Species")
## Warning: Removed 2 rows containing missing values (geom_point).

Caption

Notes: Captions are a great way to cite a source for your data or add context. Within the label parenthesis labs add another comma and write caption= to add a source or text. This will add your text at the bottom right, below your plot.

ggplot(data=penguins)+
  geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g,color=species))+
  labs(title="Palmer Penguins: Body Mass vs. Flipper Length",subtitle="Sample of Three Penguin Species",caption="Data collected by Dr. Kristen Gorman")
## Warning: Removed 2 rows containing missing values (geom_point).

Annotations

Notes: Annotations are a great way to add clarification inside your plot. You can adjust the size, font, color, and angle all within the annotate("text", line. The next part of the code needs to be the x,y, coordinate of where the text will sit on the plot.

Then you will add your label in quotation marks with label=. Finally any other annotations you want to change such as color, size, font, or angle. So the annotation layer of code you will add will look like this annotate("text", x=' ',y=' ',label=" ",color=,' ',fontface=' ', size=' ',angle=' ')

ggplot(data=penguins)+
  geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g,color=species))+
  labs(title="Palmer Penguins: Body Mass vs. Flipper Length",subtitle="Sample of Three Penguin Species",caption="Data collected by Dr. Kristen Gorman")+
  annotate("text", x=220,y=3500,label="The Gentoos are the largest",color="purple",
           fontface="bold",size=4.5,angle=25)
## Warning: Removed 2 rows containing missing values (geom_point).