Skip to content

Commit

Permalink
Merge pull request #51 from moj-analytical-services/alter_first_question
Browse files Browse the repository at this point in the history
Alter first question in exercises
  • Loading branch information
pbbgss authored Oct 31, 2024
2 parents 46b8107 + 97a6c2f commit 2d5c2e3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
78 changes: 39 additions & 39 deletions R_code_participant.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
## ----include=FALSE---------------------------------------------------------------------------------------------------------------
## ----include=FALSE---------------------------------------------------------------------------------------------------
knitr::opts_chunk$set(message = FALSE, warning = FALSE, cache = FALSE)



## ----fig.height=4, fig.width=5---------------------------------------------------------------------------------------------------
## ----fig.height=4, fig.width=5---------------------------------------------------------------------------------------
# Random number seed
set.seed(5)

# Scatter plot
plot(rnorm(20), rnorm(20))


## ----highlight=TRUE--------------------------------------------------------------------------------------------------------------
## ----highlight=TRUE--------------------------------------------------------------------------------------------------
import::from("ggplot2", "aes", "vars", "mpg")


## --------------------------------------------------------------------------------------------------------------------------------
## --------------------------------------------------------------------------------------------------------------------
# Examine the mpg dataset
dplyr::glimpse(mpg)


## ----fig.height=3, fig.width=4---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=4---------------------------------------------------------------------------------------
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point()


## ----fig.height=3, fig.width=4---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=4---------------------------------------------------------------------------------------
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::geom_point(aes(x = displ, y = cty), colour = "blue")


## ----eval = FALSE----------------------------------------------------------------------------------------------------------------
## ----eval = FALSE----------------------------------------------------------------------------------------------------
## # chart template
## ggplot(data = <DATA>, aes(<MAPPINGS>) ) +
## <GEOM_FUNCTION> +
Expand All @@ -41,31 +41,31 @@ ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
## <THEME FUNCTION>


## ----fig.height=3, fig.width=6---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=6---------------------------------------------------------------------------------------
# Aesthetics
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = class)) +
ggplot2::geom_point()


## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------------------
## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------
# ggplot2 looks at the data type to set a default colour scale
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = year)) +
ggplot2::geom_point()


## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------------------
## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------
# You can also use shapes to distinguish between categories
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, shape = class, colour = class)) +
ggplot2::geom_point()


## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------------------
## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------
# Some aesthetics are only suitable in certain situations
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, shape = class, colour = class)) +
ggplot2::geom_point()


## ----fig.height=3.5, fig.retina=2------------------------------------------------------------------------------------------------
## ----fig.height=3.5, fig.retina=2------------------------------------------------------------------------------------
# Use scale_ functions to set properties of the aesthetics
dplyr::filter(mpg, manufacturer %in% c('audi', 'volkswagen')) |>
ggplot2::ggplot(aes(x = displ, y = hwy, colour = manufacturer)) +
Expand All @@ -74,64 +74,64 @@ dplyr::filter(mpg, manufacturer %in% c('audi', 'volkswagen')) |>
ggplot2::scale_x_continuous(limits = c(1, 5))


## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------------------
## ----fig.height=4, fig.width=8---------------------------------------------------------------------------------------
# Set an aesthetic to a fixed value
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point(colour = "orange")


## ----fig.height=3, fig.width=5---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=5---------------------------------------------------------------------------------------
# What's wrong with this code? Why aren't the points blue?
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = "blue")) +
ggplot2::geom_point()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::facet_wrap(vars(class))


## --------------------------------------------------------------------------------------------------------------------------------
## --------------------------------------------------------------------------------------------------------------------
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::facet_grid(rows = vars(drv), cols = vars(class))


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# What is wrong with this plot?
ggplot2::ggplot(data = mpg, aes(x = cty, y = hwy)) +
ggplot2::geom_point()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Because there are multiple observations, some are **overplotted**. To correct this, you can add
# some random noise to the data with `position="jitter"` or `geom_jitter`.
ggplot2::ggplot(data = mpg, aes(x = cty, y = hwy)) +
ggplot2::geom_jitter(colour = 'red') +
ggplot2::geom_point()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_smooth()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# We can add the points as well, using the + operator
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_smooth() +
ggplot2::geom_point() +
ggplot2::geom_line(aes(x = 4.5))


## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------
# Bar charts counting rows in the data
ggplot2::ggplot(data = mpg, aes(x = class)) +
ggplot2::geom_bar()


## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------
# Bar charts from tabulated data
mpg |>
dplyr::group_by(class) |>
Expand All @@ -140,66 +140,66 @@ mpg |>
ggplot2::geom_col()


## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------------------
## ----fig.height=3, fig.width=9---------------------------------------------------------------------------------------
# Bar chart positioning
ggplot2::ggplot(data = mpg, aes(x = manufacturer, fill = class)) +
ggplot2::geom_bar(colour = 'black') +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0, hjust = 1))


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# By default bars are "stacked" on top of each other. This makes comparing proportions rather difficult.
# You can control this with the position argument.
ggplot2::ggplot(data = mpg, aes(x = manufacturer, fill = class)) +
ggplot2::geom_bar(colour = 'black', position = 'dodge') +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0, hjust = 1))


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Histogram
ggplot2::ggplot(data = mpg, aes(x = hwy)) +
ggplot2::geom_histogram(bins = 30)


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Density plot
ggplot2::ggplot(data = mpg, aes(x = hwy)) +
ggplot2::geom_density()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Overlapping density plots
ggplot2::ggplot(data = mpg, aes(x = hwy, fill = drv)) +
ggplot2::geom_density(alpha = 0.5)


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Box plots
ggplot2::ggplot(data = mpg, aes(x = drv, y = hwy)) +
ggplot2::geom_boxplot(fill = "red")


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Violin plots
ggplot2::ggplot(data = mpg, aes(x = drv, y = hwy)) +
ggplot2::geom_violin(aes(fill = drv))


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Line charts
ggplot2::economics |>
ggplot2::ggplot(aes(x = date, y = unemploy)) +
ggplot2::geom_line()


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Themes, titles, and multiple plots
ggplot2::ggplot(data = mpg, aes(x = class, y = ggplot2::after_stat(prop), group = 1)) +
ggplot2::geom_bar() +
ggplot2::labs(title = "Proportion of sample by class", x = "Class", y = "Proportion")


## --------------------------------------------------------------------------------------------------------------------------------
## --------------------------------------------------------------------------------------------------------------------
# Multiple plots
plot1 <-
ggplot2::ggplot(data = mpg, aes(x = cyl, y = ggplot2::after_stat(prop), group = 1)) +
Expand All @@ -218,20 +218,20 @@ plot2 <-



## --------------------------------------------------------------------------------------------------------------------------------
## --------------------------------------------------------------------------------------------------------------------
# gridExtra
gridExtra::grid.arrange(plot1, plot2, nrow = 1)



## ----fig.height=5, fig.width=8---------------------------------------------------------------------------------------------------
## ----fig.height=5, fig.width=8---------------------------------------------------------------------------------------
# Themes
gridExtra::grid.arrange(
plot2 + ggplot2::theme_bw(), plot2 + ggplot2::theme_classic(),
plot2 + ggplot2::theme_dark(), plot2 + ggplot2::theme_light())


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# You can also make your own custom themes
ugly_theme <-
ggplot2::theme(
Expand All @@ -243,27 +243,27 @@ ugly_theme <-



## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
plot2 + ugly_theme


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# MoJ colour scheme
ggplot2::ggplot(data = mpg, aes(x = class, fill = drv)) +
ggplot2::geom_bar(position = "dodge") +
mojchart::scale_fill_moj(n = 3) +
mojchart::theme_gss(xlabel = TRUE)


## ----fig.height=3----------------------------------------------------------------------------------------------------------------
## ----fig.height=3----------------------------------------------------------------------------------------------------
# Accessible colour scheme
ggplot2::ggplot(data = mpg, aes(x = class, fill = drv)) +
ggplot2::geom_bar(position = "dodge") +
mojchart::scale_fill_moj(n = 3, scheme = "govanal_bars") +
mojchart::theme_gss(xlabel = TRUE)


## ----eval=FALSE, results=FALSE---------------------------------------------------------------------------------------------------
## ----eval=FALSE, results=FALSE---------------------------------------------------------------------------------------
## ggplot2::ggplot(data = mpg) +
## ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl)))

2 changes: 1 addition & 1 deletion index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ The other functions are optional, with default values used if they are not speci

## Exercises: Section 1

1. Run `ggplot2::ggplot(data = mpg)` what do you see?
1. Run `ggplot2::ggplot(mpg, aes(x = displ, y = cty))`. Why is the data not plotted?
2. What does the `drv` variable describe? Read the help for mpg data set to find out by running `?mpg`
3. Make a scatter plot of `hwy` vs. `cty`
4. Make a scatter plot of urban fuel consumption vs engine size (displacement).
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3343,7 +3343,7 @@ <h3>Objectives:</h3>
</article></slide><slide class=""><hgroup><h2>Exercises: Section 1</h2></hgroup><article id="exercises-section-1" class="smaller ">

<ol>
<li>Run <code>ggplot2::ggplot(data = mpg)</code> what do you see?</li>
<li>Run <code>ggplot2::ggplot(mpg, aes(x = displ, y = cty))</code>. Why is the data not plotted?</li>
<li>What does the <code>drv</code> variable describe? Read the help for mpg data set to find out by running <code>?mpg</code></li>
<li>Make a scatter plot of <code>hwy</code> vs. <code>cty</code></li>
<li>Make a scatter plot of urban fuel consumption vs engine size (displacement).</li>
Expand Down
2 changes: 1 addition & 1 deletion solutions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import::from("ggplot2", "aes", "mpg")
### 1.1

```{r}
ggplot2::ggplot(data = mpg)
ggplot2::ggplot(mpg, aes(x = displ, y = cty))
```

### 1.2
Expand Down
Loading

0 comments on commit 2d5c2e3

Please sign in to comment.