Bird
Raised Fist0
R Programmingprogramming~20 mins

Labels and titles in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Label and Title Master
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 plot title code?
Consider the following R code that creates a simple plot with a title and axis labels. What will be the title of the plot when this code runs?
R Programming
plot(1:5, 1:5, main = "My Plot Title", xlab = "X Axis", ylab = "Y Axis")
title(main = "Overridden Title")
AOverridden Title
BX Axis
CY Axis
DMy Plot Title
Attempts:
2 left
💡 Hint
The title() function can override the main title set in plot().
❓ Predict Output
intermediate
2:00remaining
What label appears on the x-axis?
What label will appear on the x-axis after running this R code?
R Programming
barplot(1:3, names.arg = c("A", "B", "C"), xlab = "Category")
AA, B, C
BCategory
C1, 2, 3
DNo label
Attempts:
2 left
💡 Hint
xlab sets the label for the x-axis, names.arg sets the names for bars.
🔧 Debug
advanced
2:00remaining
Why does this plot title not appear?
This R code is intended to add a title to the plot, but no title appears. What is the cause?
R Programming
plot(1:10, 1:10)
title(main = "My Title", sub = "Subtitle")
title(xlab = "X Label")
AThe xlab argument in title() does not set the x-axis label; it must be set in plot().
BThe title() function cannot add subtitles; sub is ignored.
CThe plot() function must include main argument to show any title.
DThe title() function calls overwrite each other, so only the last call works.
Attempts:
2 left
💡 Hint
Check which arguments title() accepts and which must be set in plot().
🧠 Conceptual
advanced
2:00remaining
How to add multiple lines in a plot title?
Which option correctly adds a multi-line main title to a plot in R?
Aplot(1:5, main = "Line 1\nLine 2")
Bplot(1:5, main = c("Line 1", "Line 2"))
Cplot(1:5); title(main = "Line 1\nLine 2")
Dplot(1:5); title(main = c("Line 1", "Line 2"))
Attempts:
2 left
💡 Hint
Use newline characters in strings or multiple calls to title().
❓ Predict Output
expert
3:00remaining
What is the output of this label and title code?
What will be the main title and axis labels of the plot after running this R code?
R Programming
plot(1:3, 1:3, main = "First Title", xlab = "X1", ylab = "Y1")
title(main = "Second Title", xlab = "X2")
title(ylab = "Y2")
AMain title: Second Title; X-axis label: X1; Y-axis label: Y1
BMain title: First Title; X-axis label: X1; Y-axis label: Y1
CMain title: First Title; X-axis label: X2; Y-axis label: Y2
DMain title: Second Title; X-axis label: X2; Y-axis label: Y2
Attempts:
2 left
💡 Hint
title() can override main and sub titles but not axis labels; check which labels are updated.

Practice

(1/5)
1. What is the purpose of using main in an R plot?
easy
A. To add a main title to the plot
B. To label the x-axis
C. To label the y-axis
D. To change the plot color

Solution

  1. Step 1: Understand the role of main in plotting

    The main argument in R's plot function is used to set the main title of the graph.
  2. Step 2: Differentiate from axis labels

    xlab and ylab are used for x-axis and y-axis labels respectively, not main.
  3. Final Answer:

    To add a main title to the plot -> Option A
  4. Quick Check:

    main = main title [OK]
Hint: Remember: main = title, xlab = x-axis, ylab = y-axis [OK]
Common Mistakes:
  • Confusing main with axis labels
  • Using main to change colors
  • Omitting labels entirely
2. Which of the following is the correct syntax to add a y-axis label "Height" in an R plot?
easy
A. plot(x, y, xlab = "Height")
B. plot(x, y, ylab = "Height")
C. plot(x, y, main = "Height")
D. plot(x, y, label = "Height")

Solution

  1. Step 1: Identify the argument for y-axis label

    In R, ylab is used to set the label for the y-axis.
  2. Step 2: Check the syntax correctness

    The syntax plot(x, y, ylab = "Height") correctly assigns "Height" as the y-axis label.
  3. Final Answer:

    plot(x, y, ylab = "Height") -> Option B
  4. Quick Check:

    ylab = y-axis label [OK]
Hint: Use ylab for y-axis label inside plot() [OK]
Common Mistakes:
  • Using main instead of ylab for y-axis label
  • Using xlab for y-axis label
  • Using a non-existent label argument
3. What will be the main title of the plot after running this code?
plot(1:5, 1:5, main = "Sales Over Time", xlab = "Month", ylab = "Sales")
medium
A. Month
B. No title
C. Sales
D. Sales Over Time

Solution

  1. Step 1: Identify the main title argument

    The argument main = "Sales Over Time" sets the main title of the plot.
  2. Step 2: Confirm other labels are axis labels

    xlab = "Month" and ylab = "Sales" set the x-axis and y-axis labels, not the main title.
  3. Final Answer:

    Sales Over Time -> Option D
  4. Quick Check:

    main = "Sales Over Time" [OK]
Hint: main sets the plot title, not axis labels [OK]
Common Mistakes:
  • Confusing xlab or ylab as main title
  • Ignoring the main argument
  • Assuming no title if not obvious
4. Identify the error in this R plot code:
plot(x, y, main = "Data", xlabel = "Time", ylab = "Value")
medium
A. The argument 'xlabel' is incorrect; it should be 'xlab'
B. The argument 'main' should be 'title'
C. The argument 'ylab' should be 'ylabel'
D. There is no error in the code

Solution

  1. Step 1: Check the axis label arguments

    In R, the correct argument for x-axis label is xlab, not xlabel.
  2. Step 2: Verify other arguments

    main is correct for the title, and ylab is correct for y-axis label.
  3. Final Answer:

    The argument 'xlabel' is incorrect; it should be 'xlab' -> Option A
  4. Quick Check:

    Use xlab, not xlabel [OK]
Hint: Use xlab and ylab, not xlabel or ylabel [OK]
Common Mistakes:
  • Using xlabel instead of xlab
  • Using ylabel instead of ylab
  • Changing main to title incorrectly
5. You want to create a plot with the main title "Temperature Trends", x-axis label "Year", and y-axis label "Temperature (°C)". Which code correctly achieves this?
hard
A. plot(years, temps, main = "Temperature Trends", xlab = "Year")
B. plot(years, temps, title = "Temperature Trends", xlabel = "Year", ylabel = "Temperature (°C)")
C. plot(years, temps, main = "Temperature Trends", xlab = "Year", ylab = "Temperature (°C)")
D. plot(years, temps, main = "Temperature Trends", xlabel = "Year", ylab = "Temperature (°C)")

Solution

  1. Step 1: Use correct arguments for labels and title

    The correct arguments are main for the title, xlab for the x-axis label, and ylab for the y-axis label.
  2. Step 2: Check each option for correctness

    plot(years, temps, main = "Temperature Trends", xlab = "Year", ylab = "Temperature (°C)") uses all correct arguments. plot(years, temps, title = "Temperature Trends", xlabel = "Year", ylabel = "Temperature (°C)") uses incorrect argument names. plot(years, temps, main = "Temperature Trends", xlabel = "Year", ylab = "Temperature (°C)") uses incorrect xlabel. plot(years, temps, main = "Temperature Trends", xlab = "Year") misses the y-axis label.
  3. Final Answer:

    plot(years, temps, main = "Temperature Trends", xlab = "Year", ylab = "Temperature (°C)") -> Option C
  4. Quick Check:

    main, xlab, ylab = correct labels [OK]
Hint: Always use main, xlab, ylab for titles and axis labels [OK]
Common Mistakes:
  • Using title instead of main
  • Using xlabel or ylabel instead of xlab or ylab
  • Forgetting one of the labels
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