0
0
R Programmingprogramming~20 mins

Line plots (geom_line) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Line Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ggplot2 line plot code?

Consider the following R code using ggplot2. What will the plot display?

R Programming
library(ggplot2)
data <- data.frame(x = 1:5, y = c(3, 5, 2, 8, 7))
ggplot(data, aes(x = x, y = y)) + geom_line()
AA line plot connecting points (1,3), (2,5), (3,2), (4,8), (5,7) in order
BA scatter plot with points only, no lines
CA bar plot with bars at x positions 1 to 5 with heights 3,5,2,8,7
DAn error because geom_line requires grouping
Attempts:
2 left
💡 Hint

Think about what geom_line() does with x and y aesthetics.

🧠 Conceptual
intermediate
2:00remaining
Why might you need to use the group aesthetic in geom_line()?

In ggplot2, when plotting multiple lines with geom_line(), why is the group aesthetic important?

ATo change the color of the lines automatically
BTo tell ggplot which points belong to the same line so lines don't connect across groups
CTo add labels to the lines
DTo sort the data points before plotting
Attempts:
2 left
💡 Hint

Think about what happens if ggplot doesn't know which points belong together.

🔧 Debug
advanced
2:00remaining
What error does this code produce?

Examine this R code snippet:

library(ggplot2)
data <- data.frame(time = 1:4, value = c(2, 4, 6, 8))
ggplot(data, aes(x = time, y = value)) + geom_line(color = 5)

What error or warning will this code produce?

AWarning: ignoring unknown parameter 'color'
BNo error; line color will be set to color number 5
CError: 'color' must be a character string or a valid color name
DError: object 'color' not found
Attempts:
2 left
💡 Hint

Check what type of value color expects in geom_line().

📝 Syntax
advanced
2:00remaining
Which option produces a valid line plot with two groups?

Given this data frame:

df <- data.frame(
  time = rep(1:3, 2),
  value = c(2, 3, 5, 1, 4, 6),
  group = rep(c('A', 'B'), each = 3)
)

Which ggplot2 code correctly plots lines for each group?

Aggplot(df, aes(x = time, y = value, group = group)) + geom_line()
Bggplot(df, aes(x = time, y = value)) + geom_line()
Cggplot(df, aes(x = time, y = value, group = 1)) + geom_line()
Dggplot(df, aes(x = time, y = value, color = group)) + geom_line()
Attempts:
2 left
💡 Hint

Think about how to show separate lines by group with color.

🚀 Application
expert
2:00remaining
How many lines will this plot display?

Given this data frame and plot code:

df <- data.frame(
  day = rep(1:4, 3),
  score = c(5, 6, 7, 8, 3, 4, 5, 6, 9, 10, 11, 12),
  player = rep(c('X', 'Y', 'Z'), each = 4)
)
ggplot(df, aes(x = day, y = score, group = player)) + geom_line()

How many separate lines will appear in the plot?

A3
B1
C4
D12
Attempts:
2 left
💡 Hint

Count the unique groups defined by player.