Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Modifying ggplot2 Themes: Understanding Elements and Overriding Defaults, Study notes of Advanced Computer Programming

An in-depth exploration of ggplot2 themes, focusing on various elements such as lines, rectangles, text, titles, axes, legends, and panels. It covers the use of the theme() function to modify these elements and override default settings. Understanding how to customize these themes can help create more visually appealing and informative data visualizations.

What you will learn

  • How do I change the axis title and label settings in ggplot2 themes?
  • What are the different text elements in ggplot2 themes and how can I customize them?
  • How can I modify the line elements in ggplot2 themes?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

bairloy
bairloy 🇺🇸

4.2

(6)

247 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Modifying ggplot2 themes
theme() with options to override default
Overall
line
all line elements (element_line)
rect
all rectangular elements (element_rect)
text
all text elements (element_text)
title
all title elements: plot, axes, legends (element_text; inherits from text)
aspect.ratio
aspect ratio of the panel
Axes
axis.title
label of axes (element_text; inherits from text)
axis.title.x
x axis label (element_text; inherits from axis.title)
axis.title.x.top
x axis label on top axis (element_text; inherits from axis.title.x)
axis.title.y
y axis label (element_text; inherits from axis.title)
axis.title.y.right
y axis label on right axis (element_text; inherits from axis.title.y)
axis.text
tick labels along axes (element_text; inherits from text)
axis.text.x
x axis tick labels (element_text; inherits from axis.text)
axis.text.x.top
x axis tick labels on top axis (element_text; inherits from axis.text.x)
axis.text.y
y axis tick labels (element_text; inherits from axis.text)
axis.text.y.right
y axis tick labels on right axis (element_text; inherits from axis.text.y)
axis.ticks
tick marks along axes (element_line; inherits from line)
axis.ticks.x
x axis tick marks (element_line; inherits from axis.ticks)
axis.ticks.y
y axis tick marks (element_line; inherits from axis.ticks)
axis.ticks.length
length of tick marks (unit)
axis.line
lines along axes (element_line; inherits from line)
axis.line.x
line along x axis (element_line; inherits from axis.line)
axis.line.y
line along y axis (element_line; inherits from axis.line)
Legend
legend.background
background of legend (element_rect; inherits from rect)
legend.margin
the margin around each legend (margin)
legend.spacing
the spacing between legends (unit)
legend.spacing.x
the horizontal spacing between legends (unit); inherits
from legend.spacing
legend.spacing.y
the horizontal spacing between legends (unit); inherits
from legend.spacing
legend.key
background underneath legend keys (element_rect; inherits from rect)
legend.key.size
size of legend keys (unit)
legend.key.height
key background height (unit; inherits from legend.key.size)
legend.key.width
key background width (unit; inherits from legend.key.size)
legend.text
legend item labels (element_text; inherits from text)
legend.text.align
alignment of legend labels (number from 0 (left) to 1 (right))
pf3
pf4
pf5

Partial preview of the text

Download Modifying ggplot2 Themes: Understanding Elements and Overriding Defaults and more Study notes Advanced Computer Programming in PDF only on Docsity!

Modifying ggplot2 themes

theme() with options to override default

Overall line (^) all line elements (element_line)

rect (^) all rectangular elements (element_rect)

text (^) all text elements (element_text)

title (^) all title elements: plot, axes, legends (element_text; inherits from text)

aspect.ratio (^) aspect ratio of the panel

Axes axis.title (^) label of axes (element_text; inherits from text)

axis.title.x (^) x axis label (element_text; inherits from axis.title) axis.title.x.top (^) x axis label on top axis (element_text; inherits from axis.title.x)

axis.title.y (^) y axis label (element_text; inherits from axis.title)

axis.title.y.right (^) y axis label on right axis (element_text; inherits from axis.title.y)

axis.text (^) tick labels along axes (element_text; inherits from text)

axis.text.x (^) x axis tick labels (element_text; inherits from axis.text)

axis.text.x.top (^) x axis tick labels on top axis (element_text; inherits from axis.text.x)

axis.text.y (^) y axis tick labels (element_text; inherits from axis.text)

axis.text.y.right (^) y axis tick labels on right axis (element_text; inherits from axis.text.y)

axis.ticks (^) tick marks along axes (element_line; inherits from line)

axis.ticks.x (^) x axis tick marks (element_line; inherits from axis.ticks) axis.ticks.y (^) y axis tick marks (element_line; inherits from axis.ticks)

axis.ticks.length (^) length of tick marks (unit)

axis.line (^) lines along axes (element_line; inherits from line)

axis.line.x (^) line along x axis (element_line; inherits from axis.line)

axis.line.y (^) line along y axis (element_line; inherits from axis.line)

Legend legend.background (^) background of legend (element_rect; inherits from rect)

legend.margin (^) the margin around each legend (margin)

legend.spacing (^) the spacing between legends (unit) legend.spacing.x (^) the horizontal spacing between legends (unit); inherits

from legend.spacing legend.spacing.y (^) the horizontal spacing between legends (unit); inherits from legend.spacing legend.key (^) background underneath legend keys (element_rect; inherits from rect)

legend.key.size (^) size of legend keys (unit)

legend.key.height (^) key background height (unit; inherits from legend.key.size)

legend.key.width (^) key background width (unit; inherits from legend.key.size)

legend.text (^) legend item labels (element_text; inherits from text)

legend.text.align (^) alignment of legend labels (number from 0 (left) to 1 (right))

legend.title (^) title of legend (element_text; inherits from title) legend.title.align (^) alignment of legend title (number from 0 (left) to 1 (right))

legend.position (^) the position of legends ("none", "left", "right", "bottom", "top", or two-

element numeric vector) legend.direction (^) layout of items in legends ("horizontal" or "vertical")

legend.justification (^) anchor point for positioning legend inside plot ("center" or two-element numeric vector) or the justification according to the plot area when positioned outside the plot legend.box (^) arrangement of multiple legends ("horizontal" or "vertical")

legend.box.just (^) justification of each legend within the overall bounding box, when there

are multiple legends ("top", "bottom", "left", or "right") legend.box.margin (^) margins around the full legend area, as specified using margin

legend.box.background (^) background of legend area (element_rect; inherits from rect)

legend.box.spacing (^) The spacing between the plotting area and the legend box (unit)

Panel panel.background (^) background of plotting area, drawn underneath plot (element_rect;

inherits from rect) panel.border (^) border around plotting area, drawn on top of plot so that it covers tick marks and grid lines. This should be used with fill=NA (element_rect; inherits from rect) panel.spacing (^) spacing between facet panels (unit)

panel.spacing.x (^) horizontal spacing between facet panels (unit; inherits

from panel.spacing) panel.spacing.y (^) vertical spacing between facet panels (unit; inherits from panel.spacing)

panel.grid (^) grid lines (element_line; inherits from line)

panel.grid.major (^) major grid lines (element_line; inherits from panel.grid) panel.grid.minor (^) minor grid lines (element_line; inherits from panel.grid)

panel.grid.major.x (^) vertical major grid lines (element_line; inherits from panel.grid.major)

panel.grid.major.y (^) horizontal major grid lines (element_line; inherits from panel.grid.major)

panel.grid.minor.x (^) vertical minor grid lines (element_line; inherits from panel.grid.minor)

panel.grid.minor.y (^) horizontal minor grid lines (element_line; inherits from panel.grid.minor)

panel.ontop (^) option to place the panel (background, gridlines) over the data layers. Usually used with a transparent or blank panel.background. (logical) Plot plot.background (^) background of the entire plot (element_rect; inherits from rect)

plot.title (^) plot title (text appearance) (element_text; inherits from title) left-aligned

by default plot.subtitle (^) plot subtitle (text appearance) (element_text; inherits from title) left-

aligned by default plot.caption (^) caption below the plot (text appearance) (element_text; inherits from title) right-aligned by default plot.margin (^) margin around entire plot (unit with the sizes of the top, right, bottom,

and left margins) Strips strip.background (^) background of facet labels (element_rect; inherits from rect)

rel(x)

t, r, b, l (^) Dimensions of each margin. (To remember order, think trouble). unit (^) Default units of dimensions. Defaults to "pt" so it can be most easily scaled with the text. fill (^) Fill color. color, color (^) Line/border color. Color is an alias for color. size (^) Line/border size in mm; text size in pts. linetype (^) Line type. An integer (0:8), a name (blank, solid, dashed, dotted, dotdash, longdash, twodash), or a string with an even number (up to eight) of hexadecimal digits which give the lengths in consecutive positions in the string. inherit.blank (^) Should this element inherit the existence of an element_blank among its parents? If TRUE the existence of a blank element among its parents will cause this element to be blank as well. If FALSE any blank parent element will be ignored when calculating final element state. lineend (^) Line end Line end style (round, butt, square) arrow (^) Arrow specification, as created by arrow family (^) Font family face (^) Font face ("plain", "italic", "bold", "bold.italic") hjust (^) Horizontal justification (in [0, 1]) vjust (^) Vertical justification (in [0, 1]) angle (^) Angle (in [0, 360]) lineheight (^) Line height margin (^) Margins around the text. See margin for more details. When creating a theme, the margins should be placed on the side of the text facing towards the center of the plot. debug (^) If TRUE, aids visual debugging by drawing a solid rectangle behind the complete text area, and a point where each label is anchored. x (^) A single number specifying size relative to parent element.

Using System Fonts (On Windows)

install.packages("extrafont")

library(extrafont) font_import() # import system fonts – only supports TrueType fonts

this can take a while

You may see some warnings, but you should be able to ignore them. After the fonts are imported, you can view the available fonts by running fonts() or fonttable():

fonts()

[1] "Andale Mono" "AppleMyungjo"

[3] "Arial Black" "Arial"

[5] "Arial Narrow" "Arial Rounded MT Bold"

[7] "Arial Unicode MS" "Bangla Sangam MN"

[9] "Brush Script MT" "Comic Sans MS"

[11] "Courier New" "Georgia"

[13] "Gujarati Sangam MN" "Impact"

...

This will show more detailed information about fonts

fonttable()

access system fonts

This registers fonts so that they can be used with the pdf, postscript,

or Windows bitmap output device. It must be run once in each R session.

loadfonts(device="win") # can be "pdf", "postscript", or "win"

example

ggplot(data=mtcars, aes(x=wt, y=mpg, color=factor(am))) + geom_point() + facet_wrap(~am) + theme(strip.background=element_rect(fill="skyblue", color="white"), strip.text=element_text(color="white", face="bold", hjust=.1, family="Rockwell"))