knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(palmerpenguins)
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:viridis':
##
## viridis_pal
data("penguins")
p1<- ggplot(penguins, aes(x = island, fill=species)) +
geom_bar(show.legend = FALSE) +
labs(title = "Penguins Live in Different Places", x= "Island", y = "Count") +
scale_fill_viridis(discrete = TRUE)+
theme_minimal()
ggplotly(p1)
Gentoo penguins live only on Biscoe Island and Chinstrap penguins live only on Dream Island. Adelie penguins are the only type to live on all three islands.Although there are more Adelie penguns overall, they are spread evenly across the three islands and are in the minority on two of them.
p2 <- ggplot(penguins, aes( x = sex, fill = species)) +
geom_bar(show.legend = FALSE) +
labs(title = "Sex is Distributed Evenly", x= "Sex by Island", y = "Count") +
facet_wrap(~island) +
theme_minimal() +
scale_fill_viridis(discrete = TRUE)
ggplotly(p2)
On each island and for each species, there were almost equal numbers of male and female penguins.
p3 <- ggplot(penguins, aes( x = bill_depth_mm, fill = species)) +
geom_density(show.legend = FALSE) +
labs(title = "Gentoo Penguins have Shallow Bills",
x = "Bill Depth (mm)",
y = " Frequency") +
facet_wrap(~island) +
theme_minimal() +
scale_fill_viridis(discrete = TRUE)
ggplotly(p3)
Gentoo penguins tend to have bills that are less deep than those of Chinstrap and Adelie penguins.
p4 <- ggplot(penguins, aes( x = bill_length_mm, fill = species)) +
geom_density() +
labs(title = "Adelie Penguins have Short Bills",
x = "Bill Length (mm)",
y = " Frequency") +
facet_wrap(~island) +
theme_minimal() +
scale_fill_viridis(discrete = TRUE)
ggplotly(p4)
Adelie Penguins have shorter bills than Chinstrap and Gentoo penguins. This means that Adelie penguin bills are short and deep, Chinstrap penguin bills are long and deep, and Gentoo Penguin bills are long and shallow
p5 <- ggplot(penguins, aes( x = flipper_length_mm, fill = species)) +
geom_density() +
labs(title = "Gentoo Penguins have Long Flippers",
x = "Flipper Length (mm)",
y = " Frequency") +
facet_wrap(~island) +
theme_minimal() +
scale_fill_viridis(discrete = TRUE)
ggplotly(p5)
Gentoo penguins have much longer flippers than Adelie or Chinstrap penguins, on average. Chinstrap penguins had slightly longer flippers than Adelie on average, but many of them have flippers that are just as short.
A few conclusions are the same across all graphs. First, female penguins are usually smaller than male penguins of the same sex. Second, within each species, the heavier a penguin is, the larger any physical trait is likely to be.
p9 <- ggplot(data = penguins, aes(x = body_mass_g, y = bill_depth_mm, color = sex))+
geom_point()+
labs(title = "Bill Depth As a Function of Body Mass Across Species and Sex",
x = "Body Mass (g)",
y = "Bill Depth (mm)") +
facet_wrap(~species) +
theme_minimal() +
scale_fill_viridis()
(p9)
Gentoo penguins are heavier than other penguins on average, but also have more shallow bills. This is curious because it is the opposite of the trend within each species: that larger individual penguins tend to have larger physical traits.
p7 <- ggplot(data = penguins, aes(x = body_mass_g, y = bill_length_mm, color = sex))+
geom_point()+
labs(title = "Bill Length As a Function of Body Mass Across Species and Sex",
x = "Body Mass (g)",
y = "Bill Length (mm)") +
facet_wrap(~species) +
theme_minimal() +
scale_fill_viridis()
(p7)
Chinstrap penguins have less heavy bodies than Gentoo penguins and longer bills than Adelie penguins. This means they probably have bills that appear larger than normal compared to their body size.
p8 <- ggplot(data = penguins, aes(x = body_mass_g, y = flipper_length_mm, color = sex))+
geom_point()+
labs(title = "Flipper Length as a Function of Body Mass Across Species and Sex",
x = "Body Mass (g)",
y = "Flipper Length (mm)") +
facet_wrap(~species) +
theme_minimal() +
scale_fill_viridis()
(p8)
Gentoo penguins, which are the heaviest, also had the largest flippers.
Essentially, I wanted to create a picture of what types of penguins were living on which island, and what they looked like. For this reason, I facet-wrapped most of the graphs by island and colored the data by penguin species Even though it turns out that physical traits don’t vary much within species across islands, I think it is still interesting to keep the context of which penguins are living together on each graph, and how they compare.
I included the code to make sure the results were replicable. As a design choice, I think it takes away from the minimalist aesthetic, but I think that transparency is more important.
I wanted to use various geometries to test different features of ggplot using a data set that is intuitive and easy to use. I chose the bar graphs to give an overview of the demographic characteristics of each island. After that, I used density graphs to visualize what the most common values of three characteristics are. Then, I used scatter plots to show how those values relate to mass for each species of penguin.
I used color to distinguish penguin species for the first set of graphs and sex for the last three. I tried to keep the use of colors consistent, but changing the use of color added more information to the last three graphs.
I used the minimalist ggplot theme because–especially when integrated with plot_ly, it offered the cleanest, most visually-appealing graphs. There were some ways that plot_ly changed my ggplot graphs–pushing the unit labels closer to the titles on the axes, making the titles hard to read, etc.) I chose the minimalist theme because it worked the best with plot_ly without my having to change plot_ly settings. When the graphs just wouldn’t play nice (like the last three) I chose to exclude plot_ly to make the aesthetic more appealing.
I had a lot of fun with these. Initially, I had only used the floating table of contents, but I added tabs while preparing to submit the assignment. I think they look great and allow for more intuitive grouping of similar graphs. If I had known about the tabs ahead of time, I might have organized the graphs differently.