library(learnr) library(ggplot2) library(bst290) library(dplyr) library(magrittr) data(ess) knitr::opts_chunk$set(echo = FALSE)
You should know the drill by now: You get a few code chunks, all of them are supposed to produce ggplot2
graphs but all of them have some problem(s) in them that generate only error messages. Your job is to fix the bugs and make the code work properly.
Here the task is to create a scatterplot with geom_point()
, but the points are not shown. This should not be hard to figure out, however:
ggplot(data = ess, aes(x = height, y = weight)) geom_point()
The problem was a lacking plus sign (+
) after the first line. Without the +
, R
does not understand that the two functions (ggplot()
& geom_point()
) belong together and therefore gets confused.
Once you add the +
, everything works:
ggplot(data = ess, aes(x = height, y = weight)) + geom_point()
You are still working with the same scatterplot, and now you are trying to make the graph publication-ready by adding informative labels. Unfortunately, R
does not seem to like the code that it gets here. Can you find and fix this bug?
ggplot(data = ess, aes(x = height, y = weight)) + geom_point() + labs(x = Body height (cm), y = Body weight (kg)) + theme_bw()
The issue: The text you wanted to add with xlab()
and ylab()
was not put in quotation marks. Therefore, R
thought that Body height (cm) and Body weight (kg) would refer to some functions it is supposed to run --- but which obviously do not exist.
Once you put the text in quotation marks, the graph displays as intended:
ggplot(data = ess, aes(x = height, y = weight)) + geom_point() + labs(x = "Body height (cm)", y = "Body weight (kg)") + theme_bw()
Here the task is to create a different type of graph, a bar graph showing the number of male and female respondents in the ess
dataset --- but the right geom_
still needs to be specified.
ggplot(data = ess, aes(x = gndr))
You obviously needed to add geom_bar()
as an additional graph layer. Since you want it to represent the overall numbers --- or 'counts' --- of male and female respondents, you could also be explicit about it and add stat = "count"
--- but it works also without:
ggplot(data = ess, aes(x = gndr)) + geom_bar(stat = "count")
To solve this last problem, you need to combine your data cleaning and management skills with your knowledge about data visualization.
You want to create a bar graph that shows the average ages of respondents, depending on whether or not they were born in Norway. The relevant variables are:
agea
, which measures respondents' ages;brncntr
, which records whether or not respondents were born in Norway;Heads up: This one might take a while to solve...
ess %>% group_by() %>% summarize(mean_age = ) %>% ggplot(aes(x = , y = mean_age)) + geom_
Could you figure it out? If yes, congrats, this one was not that beginner-friendly!
You needed to add:
group_by()
: brncntr
summarize()
the agea
variable: mean_age = mean(agea, na.rm = T)
geom_bar()
, obviously, and...stat = "identity"
Everything together should look like this:
ess %>% group_by(brncntr) %>% summarize(mean_age = mean(agea, na.rm = T)) %>% ggplot(aes(x = brncntr, y = mean_age)) + geom_bar(stat = "identity")
Great job, you are done with these de-bugging exercises. Now you know the fundamentals of visualizing data with ggplot2
in R
!
If you like, you can still polish the last graph a bit to make it publication-ready --- otherwise, get ready for the next part of this course: Statistical inference.
ess %>% group_by(brncntr) %>% summarize(mean_age = mean(agea, na.rm = T)) %>% ggplot(aes(x = brncntr, y = mean_age)) + geom_bar(stat = "identity")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.