0
0
R Programmingprogramming~20 mins

Themes and theme customization in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Theme Mastery Badge
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 theme customization?

Consider the following R code using ggplot2. What will be the color of the plot title?

R Programming
library(ggplot2)

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  ggtitle("Car Data") +
  theme(plot.title = element_text(color = "blue"))

print(p)
AThe plot title will be red.
BThe plot title will be blue.
CThe plot title will be invisible.
DThe plot title will be black (default).
Attempts:
2 left
💡 Hint

Look at the theme() function and the element_text() color argument.

🧠 Conceptual
intermediate
1:30remaining
Which theme element controls the background color of the plot panel?

In ggplot2, which theme element should you modify to change the background color inside the plotting area (where data points appear)?

A<code>panel.background</code>
B<code>panel.grid</code>
C<code>plot.background</code>
D<code>axis.text</code>
Attempts:
2 left
💡 Hint

Think about the area inside the axes where the data is drawn.

🔧 Debug
advanced
2:00remaining
Why does this theme customization not change the axis text color?

Look at this code snippet:

library(ggplot2)

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme(axis.text = element_text(color = "red"))

print(p)

Why does the axis text color remain black?

ABecause <code>geom_point()</code> overrides the theme settings.
BBecause <code>axis.text</code> is not a valid theme element; it should be <code>axis.text.x</code> or <code>axis.text.y</code>.
CBecause the color argument should be <code>color</code> not <code>colour</code>.
DBecause the theme must be added before <code>geom_point()</code>.
Attempts:
2 left
💡 Hint

Check the spelling of the color argument in element_text(): it should be color.

📝 Syntax
advanced
1:30remaining
Which option correctly sets the legend position to the bottom?

Choose the correct syntax to move the legend to the bottom of a ggplot2 plot.

Atheme(legend.position = "bottom")
Btheme(legend.position = bottom)
Ctheme(legend.position = bottom())
Dtheme(legend.position = c("bottom"))
Attempts:
2 left
💡 Hint

Legend position expects a string value.

🚀 Application
expert
2:30remaining
How many theme elements are changed by this custom theme?

Given this custom theme code, how many distinct theme elements are modified?

custom_theme <- theme(
  plot.title = element_text(size = 16, face = "bold"),
  axis.title.x = element_text(color = "darkgreen"),
  axis.title.y = element_text(color = "darkgreen"),
  panel.background = element_rect(fill = "lightyellow"),
  panel.grid.major = element_line(color = "gray"),
  panel.grid.minor = element_line(color = "lightgray"),
  legend.position = "top"
)
A6
B8
C5
D7
Attempts:
2 left
💡 Hint

Count each unique theme element name inside theme().