# rmarkdown::run("plots.Rmd") library(learnr) library(tidyverse) library(tutor) library(patchwork) df <- read_table(tutor_examples("exo_fit.txt"))
We will work with the well known table mtcars
included in R:
str(mtcars)
Modify the following code to add a color depending on the gear
column:
mtcars %>% # we work on the mtcars dataset, send it to ggplot ggplot(aes(x = wt, y = mpg))+ # define the x and y variables of the plot, and also the color geom_point() # plot with points
mtcars %>% ggplot(aes(x = wt, y = mpg, color=gear)) + geom_point()
grade_code()
What happens if you use factor(gear)
instead?
mtcars %>% # we work on the mtcars dataset, send it to ggplot ggplot(aes(x = wt, y = mpg, color = gear))+ # define the x and y variables of the plot, and also the color geom_point() # plot with points
mtcars %>% ggplot(aes(x = wt, y = mpg, color = factor(gear))) + geom_point()
grade_code()
Modify the following code to place each carb
in a different facet. Also add a color, but remove the legend.
mtcars %>% # we work on the mtcars dataset, send it to ggplot ggplot(aes(x = wt, y = mpg))+ # define the x and y variables of the plot, and also the color geom_point() + # plot with points facet____(___) + # add a faceting theme(___) # remove the legend
mtcars %>% ggplot(aes(x = wt, y = mpg, color = factor(gear)))+ geom_point() + facet_wrap(~carb)+ theme(legend.position = "none")
grade_code()
Modify the following code to arrange mpg
vs wt
plots on a grid showing gear
vs carb
. Also add a color depending on cyl
. Also, try adding a free x
scale range, or a free y
scale range, or free x
and y
scale ranges.
mtcars %>% # we work on the mtcars dataset, send it to ggplot ggplot(aes(x = ___, y = ___))+ # define the x and y variables of the plot, and also the color geom_point() + # plot with points facet____(___) # add a faceting
mtcars %>% ggplot(aes(x = wt, y = mpg, color = factor(cyl)))+ geom_point() + facet_grid(paste("gear = ", gear) ~ paste("carb = ", carb), scales = "free")
grade_code()
We will look at data loaded into df
.
df
Using ggplot
, plot y
as a function of x
with points and save it into Py
:
Py <- df %>% ___ Py
Py <- df %>% ggplot(aes(x = x, y = y)) + geom_point() Py
grade_code()
Add a straight line in Py
resulting from a linear fit:
Py <- Py + geom_smooth(___) Py
Py <- Py + geom_smooth(method = "lm") Py
grade_code()
Using ggplot
, plot z
as a function of x
with a red line and save it into Pz
:
Pz <- df %>% ___ Pz
Pz <- df %>% ggplot(aes(x = x, y = z)) + geom_line(color = "red") Pz
grade_code()
Using ggplot
, plot a histogram of w
with transparent blue bars surrounded by a red line, and save it into Pw
. You can play with the number of bins too.
Pw <- df %>% ___ Pw
Pw <- df %>% ggplot(aes(x = w)) + geom_histogram(fill = "blue", alpha=.5, color="red", bins=50) Pw
grade_code()
Using ggplot
, plot a density of u
with a transparent blue area surrounded by a red line, and save it into Pu
. Play with the bw
parameter so that you see many peaks.
Pu <- df %>% ___ Pu
Pu <- df %>% ggplot(aes(x = u)) + geom_density(fill = "blue", alpha=.5, color="red", bw=.02) Pu
grade_code()
Using patchwork
, gather the previous plots on a 2x2 grid.
Py Pz Pw Pu
library(patchwork) Py+Pz+Pw+Pu
grade_code()
Using patchwork
, gather the previous plots on a grid with 3 plots in the 1st row, and one large plot in the 2nd row. Using plot_annotation()
, add tags such as (a), (b)...
Py Pz Pw Pu
library(patchwork) (Py + Pz + Pw) / Pu + plot_annotation(tag_levels = "a", tag_prefix="(", tag_suffix=")")
grade_code()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.