Load Required Packages:
Warning: package 'ggplot2' was built under R version 4.5.2
Exercise 1: Modify Plot Background and Grid
Dataset: mtcars (available within R).
Task: Create a scatter plot of mpg against hp. Adjust theme to: (1) set plot background to lightblue, (2) change major grid to white, (3) remove minor grid and axis ticks.
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (mtcars, aes (x = hp, y = mpg)) +
geom_point () +
theme (
plot.background = element_rect (fill = "lightblue" ),
panel.grid.major = element_line (color = "white" ),
panel.grid.minor = element_blank (),
axis.ticks = element_blank ()
)
)
Exercise 2: Change Title and Axis Text Styles
Dataset: diamonds (ggplot2).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (diamonds, aes (x = carat, y = price)) +
geom_point (alpha = 0.5 ) +
ggtitle ("Price vs Carat" ) +
theme (
plot.title = element_text (face = "bold" , color = "red" ),
axis.text = element_text (face = "italic" )
)
)
Exercise 3: Alter Legend Properties
Dataset: iris (available within R).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (iris, aes (x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point () +
theme (
legend.position = "bottom" ,
legend.background = element_rect (fill = "grey90" ),
legend.title = element_text (face = "bold" , color = "blue" )
)
)
Exercise 4: Adjust Axis Line Appearance and Title Position
Dataset: Fictitious dataset of monthly sales (sales_data created in setup).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (sales_data, aes (x = month, y = sales)) +
geom_line (group = 1 ) +
geom_point () +
ggtitle ("Monthly Sales" ) +
theme (
axis.line = element_line (size = 2 ),
plot.title.position = "plot"
)
)
Exercise 5: Adjusting Strip Text in Facets
Dataset: mpg (ggplot2).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (mpg, aes (x = displ, y = hwy)) +
geom_point () +
facet_wrap (~ class) +
theme (
strip.text = element_text (color = "white" ),
strip.background = element_rect (fill = "darkblue" )
)
)
Exercise 6: Modify Legend Key Shapes
Dataset: iris (available within R).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (iris, aes (x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point () +
theme (
legend.key = element_rect ()
) +
guides (
color = guide_legend (override.aes = list (shape = 15 ))
)
)
Exercise 7: Custom Axis Ticks and Labels
Dataset: diamonds (ggplot2).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (diamonds, aes (x = carat, y = price)) +
geom_point (alpha = 0.5 ) +
theme (
axis.ticks.x = element_line (size = 2 ),
axis.text.y = element_text (color = "red" )
)
)
Exercise 8: Modify Plot Margins
Dataset: airquality (available within R).
Work here
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Answer
Code
(
ggplot (airquality, aes (x = Wind, y = Temp)) +
geom_point () +
theme (
plot.margin = margin (2 , 2 , 2 , 2 , "cm" )
)
)
Exercise 9: Remove Plot Labels
Dataset: Fictitious dataset of weekly sales.
Task: Customize the theme to remove axis title and plot title.
Create Dataset:
Please enable JavaScript to experience the dynamic code cell content on this page.
Please enable JavaScript to experience the dynamic code cell content on this page.
Please enable JavaScript to experience the dynamic code cell content on this page.
Here is the output of successfully created plot:
Please enable JavaScript to experience the dynamic code cell content on this page.
Code
(
ggplot (sales_weekly, aes (x = week, y = sales)) +
geom_line () +
ggtitle ("Weekly Sales" ) +
theme (
plot.title = element_blank (),
axis.title.x = element_blank (),
axis.title.y = element_blank ()
)
)