Use theme_get
to get the current theme, and theme_set
to
completely override it. theme_update
and theme_replace
are
shorthands for changing individual elements in the current theme.
theme_update
uses the +
operator, so that any unspecified
values in the theme element will default to the values they are set in the
theme. theme_replace
will completely replace the element, so any
unspecified values will overwrite the current value in the theme with NULL
s.
Examples
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point()
p
old <- theme_set(theme_bw())
p
theme_set(old)
p
#theme_replace NULLs out the fill attribute of panel.background,
#resulting in a white background:
theme_get()$panel.background
#> List of 4
#> $ fill : chr "grey92"
#> $ colour : logi NA
#> $ size : NULL
#> $ linetype: NULL
#> - attr(*, "class")= chr [1:2] "element_rect" "element"
old <- theme_replace(panel.background = element_rect(colour = "pink"))
theme_get()$panel.background
#> List of 4
#> $ fill : NULL
#> $ colour : chr "pink"
#> $ size : NULL
#> $ linetype: NULL
#> - attr(*, "class")= chr [1:2] "element_rect" "element"
p
theme_set(old)
#theme_update only changes the colour attribute, leaving the others intact:
old <- theme_update(panel.background = element_rect(colour = "pink"))
theme_get()$panel.background
#> List of 4
#> $ fill : chr "grey92"
#> $ colour : chr "pink"
#> $ size : NULL
#> $ linetype: NULL
#> - attr(*, "class")= chr [1:2] "element_rect" "element"
p
theme_set(old)
theme_get()
#> List of 46
#> $ line :List of 4
#> ..$ colour : chr "black"
#> ..$ size : num 0.5
#> ..$ linetype: num 1
#> ..$ lineend : chr "butt"
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ rect :List of 4
#> ..$ fill : chr "white"
#> ..$ colour : chr "black"
#> ..$ size : num 0.5
#> ..$ linetype: num 1
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ text :List of 10
#> ..$ family : chr ""
#> ..$ face : chr "plain"
#> ..$ colour : chr "black"
#> ..$ size : num 11
#> ..$ hjust : num 0.5
#> ..$ vjust : num 0.5
#> ..$ angle : num 0
#> ..$ lineheight: num 0.9
#> ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : logi FALSE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.line :List of 4
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..$ lineend : NULL
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ axis.line.x : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ axis.line.y : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ axis.text :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : chr "grey30"
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.x :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.y :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 1
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.ticks :List of 4
#> ..$ colour : chr "grey20"
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..$ lineend : NULL
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ axis.ticks.length : 'simpleUnit' num 2.75points
#> ..- attr(*, "unit")= int 8
#> $ axis.title.x :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 4.4points 0points 2.2points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.title.y :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : num 90
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 4.4points 0points 2.2points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ legend.background :List of 4
#> ..$ fill : NULL
#> ..$ colour : logi NA
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ legend.margin : 'simpleUnit' num 0.2cm
#> ..- attr(*, "unit")= int 1
#> $ legend.key :List of 4
#> ..$ fill : chr "grey95"
#> ..$ colour : chr "white"
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ legend.key.size : 'simpleUnit' num 1.2lines
#> ..- attr(*, "unit")= int 3
#> $ legend.key.height : NULL
#> $ legend.key.width : NULL
#> $ legend.text :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 0.8
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ legend.text.align : NULL
#> $ legend.title :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 0
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ legend.title.align : NULL
#> $ legend.position : chr "right"
#> $ legend.direction : NULL
#> $ legend.justification : chr "center"
#> $ legend.box : NULL
#> $ panel.background :List of 4
#> ..$ fill : chr "grey92"
#> ..$ colour : logi NA
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ panel.border : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ panel.grid.major :List of 4
#> ..$ colour : chr "white"
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..$ lineend : NULL
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ panel.grid.minor :List of 4
#> ..$ colour : chr "white"
#> ..$ size : num 0.25
#> ..$ linetype: NULL
#> ..$ lineend : NULL
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ panel.margin : 'simpleUnit' num 5.5points
#> ..- attr(*, "unit")= int 8
#> $ panel.margin.x : NULL
#> $ panel.margin.y : NULL
#> $ panel.ontop : logi FALSE
#> $ strip.background :List of 4
#> ..$ fill : chr "grey85"
#> ..$ colour : logi NA
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ strip.text :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : chr "grey10"
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.text.x :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 5.5points 0points 5.5points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.text.y :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : num -90
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 5.5points 0points 5.5points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.switch.pad.grid: 'simpleUnit' num 0.1cm
#> ..- attr(*, "unit")= int 1
#> $ strip.switch.pad.wrap: 'simpleUnit' num 0.1cm
#> ..- attr(*, "unit")= int 1
#> $ plot.background :List of 4
#> ..$ fill : NULL
#> ..$ colour : chr "white"
#> ..$ size : NULL
#> ..$ linetype: NULL
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ plot.title :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 1.2
#> ..$ hjust : num 0
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 6.6points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.subtitle :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 0.9
#> ..$ hjust : num 0
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 4.95points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.caption :List of 10
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 0.9
#> ..$ hjust : num 1
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight: NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 4.95points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
#> ..- attr(*, "unit")= int 8
#> - attr(*, "class")= chr [1:2] "theme" "gganimint"
#> - attr(*, "complete")= logi TRUE
#> - attr(*, "validate")= logi TRUE
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(color = mpg)) +
theme(legend.position = c(0.95, 0.95),
legend.justification = c(1, 1))
last_plot() +
theme(legend.background = element_rect(fill = "white", colour = "white", size = 3))