-
-
Save javieroot/30b9d029b42b94080cd277145c530694 to your computer and use it in GitHub Desktop.
ggplot2: Themes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install | |
install.packages('ggplot2') | |
install.packages('readr') | |
# load | |
library(ggplot2) | |
library(readr) | |
# Scatter Plot | |
p <- ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) | |
p | |
# Axis Title | |
p + theme(axis.title.x = element_text(color = "red", size = 10)) | |
# Axis Text | |
p + theme(axis.text.x = element_text(color = "red", size = 10)) | |
# Axis Font | |
p + theme(axis.text = element_text(color = "red", size = 10, face = "italic")) | |
# Axis Ticks | |
p + theme(axis.ticks = element_line(color = 'blue', size = 1.25, linetype = 2), | |
axis.ticks.length = unit(1, "cm")) | |
# Axis Line | |
p + theme(axis.line = element_line(color = 'red', size = 1.5, linetype = 3)) | |
# Legend | |
p <- ggplot(mtcars) + | |
geom_point(aes(disp, mpg, color = factor(cyl), shape = factor(gear))) | |
p | |
# Background | |
p + theme(legend.background = element_rect(fill = 'gray', linetype = 3, | |
color = "black")) | |
# Margin | |
p + theme(legend.margin = margin(4, 8, 4, 8)) | |
# Spacing | |
p + theme(legend.spacing = unit(1.5, "cm")) | |
# Key | |
p + theme(legend.key = element_rect(fill = 'red'), | |
legend.key.size = unit(0.4, "cm")) | |
# Text | |
p + theme(legend.text = element_text(color = 'green', face = 'italic')) | |
# Title | |
p + theme(legend.title = element_text(color = 'blue', face = 'bold'), | |
legend.title.align = 0.1) | |
# Position | |
p + theme(legend.position = "top") | |
# Direction | |
p + theme(legend.direction = "horizontal") | |
# Box | |
p + theme(legend.box = "horizontal", | |
legend.box.background = element_rect(fill = "gray"), | |
legend.box.spacing = unit(1, "cm"), legend.box.just = "left", | |
legend.box.margin = margin(4, 8, 4, 8)) | |
# Panel | |
p <- ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) | |
p | |
# Panel Background | |
p + theme(panel.background = element_rect(fill = 'gray')) | |
# Panel Border | |
p + theme(panel.border = element_rect(fill = NA, color = 'blue', | |
linetype = 1, size = 2)) | |
# Panel Grid | |
p + theme(panel.grid = element_line(color = 'blue', linetype = 2, size = 0.5)) | |
# Panel Grid (Major) | |
p + theme(panel.grid.major.x = element_line(color = 'blue', | |
linetype = 2, size = 0.5)) | |
# Panel Grid (Minor) | |
p + theme(panel.grid.minor.y = element_line(color = 'red', | |
linetype = 3, size = 0.2)) | |
# Plot Area | |
p <- ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
ggtitle('Theme Modification') | |
p | |
# Plot Background | |
p + theme(plot.background = element_rect(color = 'blue', | |
fill = '#4682B4')) | |
# Plot Title | |
p + theme(plot.title = element_text(color = 'red')) | |
# Plot Margin | |
p + theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) | |
# Facets | |
p <- ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
facet_grid(~cyl) | |
p | |
# Strip Background | |
p + theme(strip.background = element_rect(color = 'red', | |
fill = '#4682B4')) | |
# Strip Text | |
p + theme(strip.text = element_text(face = 'italic', size = 8, | |
color = 'red')) | |
# Classic Dark on Light | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_bw() | |
# Default Gray | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_gray() | |
# Light | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_light() | |
# Minimal | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_minimal() | |
# Dark | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_dark() | |
# Classic | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_classic() | |
# Void (Empty) | |
ggplot(mtcars) + | |
geom_point(aes(disp, mpg)) + | |
theme_void() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment