library(learnr)
library(ggplot2)
library(bst290)
library(dplyr)
library(magrittr)

data(ess)

knitr::opts_chunk$set(echo = FALSE)

Data visualization

De-bugging exercises {data-progressive=TRUE}

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.

Problem No. 1

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()
**Hint:** You need a particular mathematical symbol to add new layers to `ggplot2` graphs.

Problem No. 1: Solution

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()

Problem No. 2

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()
**Hint:** When you add axis labels, you add text --- and text always goes between...what?

Problem No 2: Solution

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()

Problem No. 3

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))
**Hint:** Which geometric object or 'geom' produces a bar graph?

Problem No. 3: Solution

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")

Problem No 4.

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:

Heads up: This one might take a while to solve...

ess %>% 
  group_by() %>% 
  summarize(mean_age = ) %>% 
  ggplot(aes(x = , y = mean_age)) +
    geom_
**Hint:** You still need to add quite a bit of code. Have a look at Section 4 of the Tutorial.

Problem No 4: Solution

Could you figure it out? If yes, congrats, this one was not that beginner-friendly!

You needed to add:

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")

Conclusion

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")


cknotz/bst290 documentation built on Nov. 11, 2023, 1 p.m.