Skip to content

Commit

Permalink
add hw solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyMarkowitz-NOAA committed Feb 18, 2021
1 parent a614b22 commit e639733
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ vignettes/*.pdf
.Renviron


code/3_homework_solutions_ggplot.R
# code/3_homework_solutions_ggplot.R
73 changes: 73 additions & 0 deletions code/3_homework_solutions_ggplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Lesson 7: Figures and tables homework solutions: example plots
# Created by: Caitlin Allen Akselrud
# Contact: caitlin.allen_akselrud@noaa.gov
# Created: 2020-01-08
# Modified: 2021-01-18

# Notes: the '# ~' is used to indicate the difference between data specification at different levels


# workflow ----------------------------------------------------------------

dirs = c("code", "data", "documentation", "figures", "functions", "output")

for(i in 1:length(dirs)){
if(dir.exists(dirs[i])==FALSE){
dir.create(dirs[i])
}
}

# packages ----------------------------------------------------------------

library(tidyverse)

library(tidytuesdayR)

library(gt)

library(here)

# data --------------------------------------------------------------------

penguins <- tidytuesdayR::tt_load('2020-07-28')
peng_dat <- penguins$penguins
peng_dat


# homework ----------------------------------------------------------------

# make a plot of peng_dat to share with the class

# here are some example plots:

ggplot(data = peng_dat) +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm, color = body_mass_g))


ggplot(data = peng_dat) +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm, color = sex))

ggplot(data = peng_dat) +
geom_histogram(aes(x = body_mass_g, color = island))

ggplot(data = peng_dat) +
geom_histogram(aes(x = body_mass_g, fill = island))

ggplot(data = peng_dat) +
geom_bar(aes(x = species, fill = sex), position = "fill") +
coord_flip() +
facet_grid(~ island)

# here's the one I want to save...
peng_plot <- ggplot(data = peng_dat) +
geom_bar(aes(x = island, fill = sex), position = "fill") +
coord_flip() +
facet_grid(~ species) +
labs(title = "Sex ratio of penguins by species and location",
x = "Number of penguins",
y = "Location",
fill = "Sex")

peng_plot

ggsave(peng_plot, filename = "plot_penguin_sexratio.png", path = here("output"))

0 comments on commit e639733

Please sign in to comment.