Bird
Raised Fist0
R Programmingprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using geom_line() in an R ggplot?
easy
A. To connect data points with lines to show trends or changes over time
B. To create bar charts showing counts of categories
C. To add labels to points on a scatter plot
D. To fill areas under curves with color

Solution

  1. Step 1: Understand the role of geom_line()

    geom_line() is used in ggplot2 to draw lines connecting data points, which helps visualize trends or changes over time.
  2. Step 2: Compare with other geoms

    Other geoms like geom_bar() create bar charts, and geom_text() adds labels, so they do not connect points with lines.
  3. Final Answer:

    To connect data points with lines to show trends or changes over time -> Option A
  4. Quick Check:

    Line plots connect points = A [OK]
Hint: Remember: geom_line connects points in order [OK]
Common Mistakes:
  • Confusing geom_line with geom_bar
  • Thinking geom_line adds labels
  • Using geom_line to fill areas
2. Which of the following is the correct syntax to add a line plot layer to a ggplot object named p?
easy
A. p + geom_line()
B. p + geom_point()
C. p + geom_bar()
D. p + geom_text()

Solution

  1. Step 1: Identify the correct geom for line plots

    geom_line() is the function used to add line plots in ggplot2.
  2. Step 2: Check the syntax for adding layers

    In ggplot2, layers are added using the + operator, so p + geom_line() correctly adds a line plot layer.
  3. Final Answer:

    p + geom_line() -> Option A
  4. Quick Check:

    Adding line plot layer = p + geom_line() [OK]
Hint: Add layers with + and use geom_line for lines [OK]
Common Mistakes:
  • Using geom_point instead of geom_line for lines
  • Forgetting the + operator
  • Using geom_bar for line plots
3. What will be the output of the following R code?
library(ggplot2)
data <- data.frame(x = 1:3, y = c(2, 5, 3))
ggplot(data, aes(x = x, y = y)) + geom_line()
medium
A. A scatter plot with points at (1,2), (2,5), (3,3)
B. A bar chart with bars at x=1,2,3 with heights 2,5,3
C. An error because geom_line requires grouping
D. A line plot connecting points (1,2), (2,5), and (3,3)

Solution

  1. Step 1: Understand the data and mapping

    The data frame has x values 1, 2, 3 and y values 2, 5, 3. The aes maps x and y for plotting.
  2. Step 2: What does geom_line() do here?

    geom_line() connects the points in order of x, so it draws a line through (1,2), (2,5), and (3,3).
  3. Final Answer:

    A line plot connecting points (1,2), (2,5), and (3,3) -> Option D
  4. Quick Check:

    geom_line connects points = C [OK]
Hint: geom_line connects points in order of x [OK]
Common Mistakes:
  • Thinking geom_line creates scatter plot
  • Expecting bar chart output
  • Assuming error without grouping for simple data
4. Consider this code snippet:
library(ggplot2)
data <- data.frame(time = c(1, 2, 1, 2), value = c(3, 4, 5, 6))
ggplot(data, aes(x = time, y = value)) + geom_line()

What is the main issue with this plot?
medium
A. The plot will be empty because data is invalid
B. The plot will show two lines overlapping because grouping is missing
C. The plot will show a single line connecting all points correctly
D. The code will produce a syntax error

Solution

  1. Step 1: Analyze the data structure

    The data has repeated time values (1 and 2) with different values, indicating multiple groups but no grouping variable specified.
  2. Step 2: Effect of missing group aesthetic

    Without specifying a group, geom_line() connects points in the order they appear, causing lines to overlap or connect incorrectly.
  3. Final Answer:

    The plot will show two lines overlapping because grouping is missing -> Option B
  4. Quick Check:

    Missing group causes overlapping lines = D [OK]
Hint: Add group aesthetic to separate lines [OK]
Common Mistakes:
  • Assuming syntax error instead of grouping issue
  • Expecting correct single line without group
  • Thinking data is invalid or empty
5. You have a dataset with daily sales for two stores over a week:
sales <- data.frame(
  day = rep(1:7, 2),
  store = rep(c('A', 'B'), each = 7),
  sales = c(10, 12, 15, 14, 13, 16, 18, 8, 9, 11, 10, 12, 13, 15)
)

How do you create a line plot showing sales trends for both stores with different colored lines?
hard
A. ggplot(sales, aes(x = day, y = sales)) + geom_line()
B. ggplot(sales, aes(x = day, y = sales, group = store)) + geom_line()
C. ggplot(sales, aes(x = day, y = sales, color = store)) + geom_line()
D. ggplot(sales, aes(x = day, y = sales, fill = store)) + geom_line()

Solution

  1. Step 1: Identify the need for separate lines by store

    To show sales trends for each store, lines must be grouped by store and visually distinct.
  2. Step 2: Use color aesthetic to differentiate stores

    Mapping color = store inside aes() creates separate colored lines for each store automatically grouping data.
  3. Final Answer:

    ggplot(sales, aes(x = day, y = sales, color = store)) + geom_line() -> Option C
  4. Quick Check:

    Color by store for separate lines = B [OK]
Hint: Use color aesthetic to separate lines by group [OK]
Common Mistakes:
  • Omitting color causes single line for all data
  • Using fill aesthetic instead of color for lines
  • Not grouping lines by store
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard