Homework 4

Add Labels and Make Notes on ggplot; Advanced ggplot Charts

Author

Byeong-Hak Choe

Published

April 12, 2026

Modified

April 12, 2026

📌 Directions

  • Submit one Quarto document (.qmd) to Brightspace:

    • danl-310-hw4-LASTNAME-FIRSTNAME.qmd
      (e.g., danl-310-hw4-choe-byeonghak.qmd)
  • Due: April 20, 2026, 11:59 P.M. (ET)


✅ Setup

# install the `ggridges` package.
install.packages("ggridges")
library(ggridges)

library(tidyverse)
library(ggthemes)
library(hrbrthemes)
library(RColorBrewer)
library(ggrepel)
library(ggtext)
library(skimr)



Question 1

Use the following data.frame for Question 1.


  • formula is available in geom_smooth()
    • formula = y ~ log(x): geom_smooth() fits with y ~ log(x).

Question 2

Use the data.frame ncdc_temp and ggridges::geom_density_ridges() for Question 2.

ncdc_temp <- read_csv(
  'https://bcdanl.github.io/data/ncdc_temp_cleaned.csv')



Question 3

Use the following data.frame for Question 3.

starbucks <- read_csv(
  'https://bcdanl.github.io/data/starbucks.csv')

Variable description

  • Product_Name: Product Name
  • Size: Size of drink (short, tall, grande, venti)
  • Milk: Milk Type type of milk used
    • 0 none
    • 1 nonfat
    • 2 2%
    • 3 soy
    • 4 coconut
    • 5 whole
  • Whip: Whip added or not (binary 0/1)
  • Serv_Size_mL: Serving size in ml
  • Calories: KCal
  • Total_Fat_g: Total fat grams
  • Saturated_Fat_g: Saturated fat grams
  • Trans_Fat_g: Trans fat grams
  • Cholesterol_mg: Cholesterol mg
  • Sodium_mg: Sodium milligrams
  • Total_Carbs_g: Total Carbs grams
  • Fiber_g: Fiber grams
  • Sugar_g: Sugar grams
  • Caffeine_mg: Caffeine in milligrams

Q3a

  • Add the following two variables to starbucks data.frame
    • caffeine_mgml: Caffeine in milligrams per mL
    • calories_kcml: Calories KCal per mL



Q3b

  • Calculate a mean caffeine_mgml and a mean calories_kcml for each product_name.



Q3c

  • For the top 10 product_name in terms of caffeine_mgml and the top 10 product_name in terms of calories_kcml, replicate the ggplot below.

  • Use the following commands for showing texts in the plot:

# install.packages("showtext")
library(showtext)
showtext_auto()
font_add_google("Annie Use Your Telescope", "annie")
  • Use the following annotate() geom to insert the starbucks image in the plot:
# install.packages("ggtext")
library(ggtext)
img_href <- "<img src='https://bcdanl.github.io/lec_figs/starbucks.png' width='100'/>"

annotate("richtext", 
           x =  , 
           y =  , 
           label = img_href, 
           fill =  ,
           size =  , 
           color =  )
  • Use the following geom_text_repel() geom to use the annie font
geom_text_repel(size =  ,
                max.overlaps = ,
                box.padding =  ,
                family = "annie")
  • Use the following colors for labels
caffeine_10 <- c(
  "#1E3932",
  "#006241",
  "#00754A",
  "#0A7F5A",
  "#198C6A",
  "#2E9B7A",
  "#49AA8A",
  "#6ABA9C",
  "#8CC9AF",
  "#AED8C2"
)

calories_10 <- c(
  "#7F5539",
  "#9C6644",
  "#B5651D",
  "#BC6C25",
  "#C97A34",
  "#D4915D",
  "#DDA15E",
  "#E6B980",
  "#EBC799",
  "#EFD2AD"
)
  • Use the color, "#00704A", for the title.

Answer:


✅ End of Homework 4

Back to top