📖 Scenario: You are creating a simple bar chart to show sales data for different products. You want to make the chart look nicer by changing the theme colors and text styles.
🎯 Goal: Build a bar chart using ggplot2 with a customized theme that changes the background color, text size, and axis titles.
📋 What You'll Learn
Create a data frame called sales_data with products and their sales numbers
Create a variable called base_size to set the base font size for the theme
Use ggplot() to create a bar chart with geom_bar() using sales_data
Customize the plot theme using theme() to change background color, axis title size, and text color
Print the final plot
💡 Why This Matters
🌍 Real World
Customizing themes helps make charts clearer and more attractive for presentations and reports.
💼 Career
Data analysts and scientists often need to adjust chart styles to match company branding or improve readability.
Progress0 / 4 steps
1
Create the sales data
Create a data frame called sales_data with these exact entries: Product as 'A', 'B', 'C' and Sales as 30, 45, 25.
R Programming
Hint
Use data.frame() with Product = c('A', 'B', 'C') and Sales = c(30, 45, 25).
2
Set the base font size
Create a variable called base_size and set it to 14 to control the font size in the theme.
R Programming
Hint
Just assign 14 to base_size.
3
Create the bar chart with customized theme
Use ggplot() with sales_data, map Product to x and Sales to y, add geom_bar(stat = 'identity'), and customize the theme with theme() to set panel.background to light gray, axis title text size to base_size, and axis text color to blue.
R Programming
Hint
Use theme() to customize background and text styles.
4
Display the customized plot
Print the plot stored in the variable plot to display the customized bar chart.
R Programming
Hint
Use print(plot) to show the plot.
Practice
(1/5)
1. What is the main purpose of using theme() in ggplot2?
easy
A. To customize the appearance of the plot, like colors and fonts
B. To add data points to the plot
C. To change the data source of the plot
D. To save the plot as an image file
Solution
Step 1: Understand the role of theme()
The theme() function in ggplot2 is used to change how the plot looks, such as colors, fonts, and backgrounds.
Step 2: Differentiate from other plot functions
Functions like adding data points or saving files are not related to theme(). It only changes the visual style.
Final Answer:
To customize the appearance of the plot, like colors and fonts -> Option A
Quick Check:
Theme controls appearance = D [OK]
Hint: Remember: theme() changes look, not data or saving [OK]
Common Mistakes:
Confusing theme() with adding data layers
Thinking theme() saves the plot
Assuming theme() changes data source
2. Which of the following is the correct way to change the plot background to white using theme()?
easy
A. theme(panel.background = element_rect(fill = "white"))
B. theme(background = element_white())
C. theme(bgcolor = "white")
D. theme(background_color = "white")
Solution
Step 1: Recall correct syntax for background color
In ggplot2, the plot background is changed by setting panel.background to element_rect() with a fill color.
Step 2: Identify the correct option
theme(panel.background = element_rect(fill = "white")) uses panel.background = element_rect(fill = "white"), which is the correct syntax. Other options use incorrect argument names or functions.
Final Answer:
theme(panel.background = element_rect(fill = "white")) -> Option A
Quick Check:
panel.background + element_rect(fill) = A [OK]
Hint: Use panel.background with element_rect(fill=) for backgrounds [OK]
Common Mistakes:
Using wrong argument names like background or bgcolor
Using element_white() which does not exist
Setting background_color instead of panel.background
3. What will be the effect of this code on a ggplot object p?
p + theme(axis.text.x = element_text(angle = 45, hjust = 1))
medium
A. Remove x-axis text labels
B. Rotate y-axis text labels 45 degrees and align them to the left
C. Change x-axis text color to red
D. Rotate x-axis text labels 45 degrees and align them to the right
Solution
Step 1: Understand the theme modification
The code modifies axis.text.x, which means the x-axis text labels. It sets their angle to 45 degrees and horizontal justification to 1 (right aligned).
Step 2: Interpret the visual effect
This will rotate the x-axis labels diagonally and align them to the right side, improving readability for long labels.
Final Answer:
Rotate x-axis text labels 45 degrees and align them to the right -> Option D
Quick Check:
axis.text.x + angle 45 + hjust 1 = A [OK]
Hint: axis.text.x controls x labels; angle rotates, hjust aligns [OK]
Common Mistakes:
Confusing x-axis with y-axis text
Thinking it changes color instead of rotation
Assuming it removes labels
4. Identify the error in this theme customization code:
theme(axis.title = element_text(size = 14, face = "bold"))
medium
A. face argument cannot be "bold"
B. axis.title should be axis.title.x or axis.title.y
C. size argument must be a string, not a number
D. element_text() cannot be used inside theme()
Solution
Step 1: Check valid theme element names
In ggplot2, axis titles are specified separately as axis.title.x and axis.title.y. There is no general axis.title element.
Step 2: Verify other arguments
The face argument accepts "bold" correctly, size is numeric as expected, and element_text() is valid inside theme().
Final Answer:
axis.title should be axis.title.x or axis.title.y -> Option B
Quick Check:
axis.title invalid, use axis.title.x/y = B [OK]
Hint: Use axis.title.x or axis.title.y, not axis.title [OK]
Common Mistakes:
Using axis.title instead of axis.title.x or axis.title.y
Thinking face="bold" is invalid
Confusing size type requirements
5. You want to create a custom theme that sets the plot title font size to 20, makes axis text blue, and removes the legend background. Which of the following theme codes achieves this correctly?
hard
A. theme(plot.title.size = 20, axis.text.color = "blue", legend.background = NULL)
B. theme(title.size = 20, axis.text.color = "blue", legend.bg = element_blank())
The plot title font size is set by plot.title = element_text(size = 20). Axis text color is set by axis.text = element_text(color = "blue"). Legend background removal uses legend.background = element_blank().
Step 2: Identify incorrect options
Options A, B, and D use invalid element names or arguments. A uses plot.title.size and axis.text.color directly, B uses title.size and legend.bg, D uses fontsize and col.