Consider the following R code using ggplot2. What will be the color of the plot title?
library(ggplot2) p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + ggtitle("Car Data") + theme(plot.title = element_text(color = "blue")) print(p)
Look at the theme() function and the element_text() color argument.
The theme() function changes the plot appearance. Here, plot.title = element_text(color = "blue") sets the title color to blue.
In ggplot2, which theme element should you modify to change the background color inside the plotting area (where data points appear)?
Think about the area inside the axes where the data is drawn.
The panel.background controls the background of the plotting panel where data points are shown. plot.background controls the entire plot's background including titles and labels.
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?
Check the spelling of the color argument in element_text(): it should be color.
The element_text() function expects color = (American spelling). colour = is not recognized by ggplot2 theme elements, so the color does not change. Use color = "red".
Choose the correct syntax to move the legend to the bottom of a ggplot2 plot.
Legend position expects a string value.
The legend position must be a string like "bottom". Option A correctly uses quotes. Other options are invalid syntax or wrong types.
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" )
Count each unique theme element name inside theme().
The theme modifies 7 elements: plot.title, axis.title.x, axis.title.y, panel.background, panel.grid.major, panel.grid.minor, and legend.position.