Push the knit button!

library(tidyverse) # contains ggplot2, dplyr, tidyr, etc
library(scales)
library(colorspace)
library(agridat) # for datasets

hazell.vegetables dataset

glimpse(hazell.vegetables)
## Rows: 6
## Columns: 5
## $ year     <fct> y1, y2, y3, y4, y5, y6
## $ carrot   <int> 292, 179, 114, 247, 426, 259
## $ celery   <int> -128, 560, 648, 544, 182, 850
## $ cucumber <int> 420, 187, 366, 249, 322, 159
## $ pepper   <int> 579, 639, 379, 924, 5, 569

Exercise 4.1

  • This is a jazzy plot version of Exercise 3.3 plot.
# fill all ... and change eval = FALSE to eval = TRUE when done
ggplot(hazell.vegetables, aes(year, celery, fill = celery > 0)) + 
  geom_col(show.legend = FALSE) + 
  geom_hline(yintercept = 0) + 
  scale_y_continuous(labels = dollar) + 
  labs(y = "Gross profit", x = "Year",
       title = "Celery", fill = "Profit") +
  scale_fill_manual(values = c("#ff1a1a", "#008000")) +
  scale_x_discrete(labels = 1:6) + 
  theme(axis.text = ...(size = 18),
        axis.text.y = ...(face = "italic"),
        axis.title = element_text(size = 22),
        plot.title = element_text(size = 30, face = "bold",
                                  margin = margin(b = 40),
                                  hjust = -0.8),
        panel.background = ...(fill = "transparent"),
        axis.line = ...(color = "black",
                                 size = 1.3))

heady.fertilizer dataset

glimpse(heady.fertilizer)
## Rows: 648
## Columns: 6
## $ crop  <fct> corn, corn, corn, corn, corn, corn, corn, corn, corn, corn, corn…
## $ rep   <int> 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2…
## $ P     <int> 0, 0, 40, 40, 80, 80, 120, 120, 160, 160, 200, 200, 240, 240, 28…
## $ K     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ N     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ yield <dbl> 24.50, 6.20, 26.70, 29.60, 22.10, 30.60, 44.20, 21.90, 12.00, 34…

Exercise 4.2

  • The plot background color is #CCDFDD.
# fill all ... and change eval = FALSE to eval = TRUE when done
ggplot(heady.fertilizer, aes(P, yield)) +
geom_point() + facet_wrap(~crop, scale = "free_y") + 
  geom_smooth() + 
  labs(x = "Phosphorous (pounds/acre)",
       y = "Yield") + 
  theme(... = element_rect(fill = "#CCDFDD", color = "black"),
        ... = element_rect(fill = "transparent"),
        ... = element_rect(fill = "transparent"))

wallace.iowaland dataset

glimpse(wallace.iowaland)
## Rows: 99
## Columns: 10
## $ county     <fct> adair, adams, allamakee, appanoose, audubon, benton, blackh…
## $ fips       <int> 19001, 19003, 19005, 19007, 19009, 19011, 19013, 19015, 190…
## $ lat        <dbl> 41.327, 41.033, 43.280, 40.741, 41.685, 42.080, 42.473, 42.…
## $ long       <dbl> -94.466, -94.702, -91.373, -92.869, -94.905, -92.066, -92.3…
## $ yield      <int> 36, 33, 42, 35, 40, 42, 38, 41, 39, 39, 43, 35, 44, 43, 37,…
## $ corn       <int> 32, 30, 13, 19, 34, 35, 32, 40, 27, 27, 40, 32, 41, 38, 34,…
## $ grain      <int> 16, 14, 15, 10, 21, 22, 25, 25, 24, 24, 28, 24, 33, 24, 24,…
## $ untillable <int> 19, 14, 44, 28, 9, 17, 17, 18, 28, 28, 12, 20, 9, 11, 9, 15…
## $ fedval     <int> 93, 90, 66, 68, 115, 143, 132, 151, 93, 93, 153, 118, 173, …
## $ stval      <int> 88, 89, 49, 60, 111, 125, 114, 141, 79, 79, 139, 98, 161, 1…

Exercise 4.3

  • Note: the diverging palette used is Green-Brown from colorspace with mid-point as average of the yield.
# fill all ... and change eval = FALSE to eval = TRUE when done
ggplot(wallace.iowaland, aes(long, lat, color = yield)) + 
  ...(size = 6) +
  scale_color_continuous_diverging(palette = "Green-Brown", rev = TRUE, mid = mean(wallace.iowaland$yield)) + 
  ...(y = "Latitude", x = "Longitude", color = "Yield") +
  theme(rect = ...(fill = "black"),
        text = ...(color = "white"),
        panel.background = ...(fill = "transparent"),
        panel.grid.minor = ...(linetype = "dashed"),
        axis.text = ...(color = "gray"))