# Don't forget to load the ggplot2 library library(ggplot2)
2024
ggplot2
Library# Don't forget to load the ggplot2 library library(ggplot2)
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + labs(title = "Engine Displacement vs. Highway MPG", x = "Displacement (L)", y = "Highway MPG", color = "Vehicle Class")
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + geom_point() + scale_color_brewer(palette = "Set1")
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + theme(legend.position = "top", axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + labs(title = "Engine Displacement vs. Highway MPG", x = "Displacement (L)", y = "Highway MPG", color = "Vehicle Class") + theme(axis.title.x = element_text(face = "bold"), plot.title = element_text(hjust = 0.5))
theme
text
: General text appearance (applies to all text unless overridden).plot.title
: The plot title text.plot.subtitle
: The plot subtitle text.plot.caption
: The caption text.axis.title.x
, axis.title.y
: X and Y axis titles.axis.text.x
, axis.text.y
: X and Y axis text.legend.title
: The legend title text.legend.text
: The legend item texts.legend.position
: Position of the legend (“none”, “left”, “right”, “bottom”, “top”, or two-element numeric vector).legend.background
: Background of the legend.legend.key
: Background of the legend keys.legend.box
: Arrangement of multiple legends (“horizontal” or “vertical”).panel.background
: Background of the plotting area.panel.grid.major
, panel.grid.minor
: Major and minor grid lines of the panel.panel.border
: Border around the plotting area.axis.line.x
, axis.line.y
: Lines along the X and Y axes.strip.background
: Background of the facet labels.strip.text.x
, strip.text.y
: Text of the facet labels along the X and Y direction.plot.margin
: Margins around the entire plot.
# Basic scatter plot
p <- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = class)) +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Displacement (liters)",
y = "Highway MPG")
# Customize the plot
p + theme(
# Customize plot title
plot.title = element_text(face = "bold", size = 14, color = "blue"),
# Customize panel background
panel.background = element_rect(fill = "lightblue", colour = "black", linewidth = 1),
# Customize axis lines
axis.line = element_line(color = "darkblue", linewidth = 1),
# Customize axis text
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
# Customize legend background
legend.background = element_rect(fill = "lightgray", linewidth= 0.5, linetype = "solid"),
# Customize legend title and text
legend.title = element_text(face = "bold", size = 10, color = "darkred"),
legend.text = element_text(size = 8)
)
# Minimal theme ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_minimal()
# Light theme ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_light()
# Dark theme ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_dark()
# Classic theme ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_classic()
Change the title of the following to “Impact of Engine Displacement on Highway MPG”, make the x-axis title bold, and set the legend position to the bottom of the plot.
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + geom_point() + labs(x = "Displacement (L)", y = "Highway MPG", color = "Vehicle Class")
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + geom_point() + labs(title = "Impact of Engine Displacement on Highway MPG", x = "Displacement (L)", y = "Highway MPG", color = "Vehicle Class") + theme(legend.position = "bottom", axis.title.x = element_text(face = "bold"))
Create a scatter plot using the theme_dark()
with customized color for the plot background to a slightly lighter shade of grey given by ““#3E3E3E”.
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_dark()
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_dark(base_size = 14) + theme(plot.background = element_rect(fill = "#3E3E3E"))
Apply the theme_minimal()
to the following plot and adjust the axis text to be italic.
ggplot(data = mpg, aes(x = class)) + geom_bar() + theme_minimal()
ggplot(data = mpg, aes(x = class)) + geom_bar(fill = "steelblue") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1, face = "italic"), axis.text.y = element_text(face = "italic"))