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, cor…
## $ rep   <int> 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, …
## $ P     <int> 0, 0, 40, 40, 80, 80, 120, 120, 160, 160, 200, 200, 240, 240, 2…
## $ K     <int> 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, …
## $ yield <dbl> 24.50, 6.20, 26.70, 29.60, 22.10, 30.60, 44.20, 21.90, 12.00, 3…

Exercise 4.2