0
0
R Programmingprogramming~10 mins

Line plots (geom_line) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic line plot using ggplot2.

R Programming
library(ggplot2)
data <- data.frame(x = 1:5, y = c(3, 5, 2, 8, 7))
ggplot(data, aes(x = x, y = y)) + [1]()
Drag options to blanks, or click blank then click option'
Ageom_bar
Bgeom_point
Cgeom_line
Dgeom_histogram
Attempts:
3 left
💡 Hint
Common Mistakes
Using geom_point() instead of geom_line() will show points, not lines.
Using geom_bar() or geom_histogram() is for bar charts, not line plots.
2fill in blank
medium

Complete the code to map the color aesthetic to the variable 'group' in the line plot.

R Programming
library(ggplot2)
data <- data.frame(x = rep(1:5, 2), y = c(3,5,2,8,7,4,6,3,7,9), group = rep(c('A', 'B'), each = 5))
ggplot(data, aes(x = x, y = y, color = [1])) + geom_line()
Drag options to blanks, or click blank then click option'
Agroup
By
Cx
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x' or 'y' for color will not group lines by category.
Using 'color' as a string literal is incorrect; it should be a variable name.
3fill in blank
hard

Fix the error in the code to correctly plot lines grouped by 'group'.

R Programming
library(ggplot2)
data <- data.frame(x = rep(1:5, 2), y = c(3,5,2,8,7,4,6,3,7,9), group = rep(c('A', 'B'), each = 5))
ggplot(data, aes(x = x, y = y, color = group)) + geom_line(aes(group = [1]))
Drag options to blanks, or click blank then click option'
Ay
Bcolor
Cx
Dgroup
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' or 'x' for group aesthetic causes lines to connect incorrectly.
Not specifying group causes all points to connect in one line.
4fill in blank
hard

Fill both blanks to create a line plot with points and set line type by 'group'.

R Programming
library(ggplot2)
data <- data.frame(x = rep(1:4, 2), y = c(2,4,3,5,3,5,4,6), group = rep(c('X', 'Y'), each = 4))
ggplot(data, aes(x = x, y = y, [1] = group)) + geom_line() + geom_point(aes([2] = group))
Drag options to blanks, or click blank then click option'
Alinetype
Bcolor
Cshape
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' for both line and points changes colors, not line style or shape.
Using 'size' changes thickness or size, not line type or shape.
5fill in blank
hard

Fill all three blanks to create a line plot with customized color, line type, and add a title.

R Programming
library(ggplot2)
data <- data.frame(time = 1:6, value = c(5,7,6,8,7,9), category = rep(c('A', 'B'), each = 3))
ggplot(data, aes(x = time, y = value, color = [1], linetype = [2])) + geom_line() + ggtitle([3])
Drag options to blanks, or click blank then click option'
Acategory
Btime
C"My Line Plot"
D"Line Plot"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'time' for color or linetype does not group lines properly.
Not putting the title text inside quotes causes errors.