library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = FALSE)
tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr)

require(tidyverse)
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")

We are going to use the penguins data to produce a variety of bar plots using the base R function barplot().

head(penguins)

Exercise 1

Produce a bar plot to work out which species of penguin is most common:


barplot(table(penguins$species))
ex() %>% {
  check_error(.)
  check_function(., "table") %>% check_result(.) %>% check_equal(.)
  check_function(., "barplot") %>% check_arg(., "height") %>% check_equal(.)
}

Exercise 2

Produce a bar plot that will compare the distribution of species within each island:


barplot(table(penguins$species, penguins$island), beside=TRUE, legend.text = TRUE)
ex() %>% {
  check_error(.)
  check_function(., "table") %>% check_result(.) %>% check_equal(.)
  check_function(., "barplot") %>% {
    check_arg(., "height") %>% check_equal(.)
    check_arg(., "beside", arg_not_specified_msg="It's easier to visualise what's happening if the bars are not stacked") %>% check_equal(., incorrect_msg="It's easier to visualise what's happening if the bars are not stacked")
    check_arg(., "legend.text", arg_not_specified_msg = "Interpretation will be easier with a legend")
  }
}

Exercise 3

Calculate the maximum bill length for each species of penguin and plot these on a bar plot (Tip: You may need to exclude missing values)


barplot(tapply(penguins$bill_length_mm, list(penguins$species), max, na.rm=TRUE))
ex() %>%{
  check_error(.) 
  check_function(., "tapply")%>% check_result(.) %>% check_equal(.)
  check_function(., "barplot") %>% check_arg(., "height") %>% check_equal(.)
}


kate-pyper/MM916ProgrammingExercises documentation built on Oct. 15, 2020, 10:40 p.m.