p <- ggplot(data = cars) +
aes(x = speed) +
aes(y = dist) +
geom_point() +
aes(color = speed) +
labs(title = "Correlation") +
labs(caption = "cars dataset") +
theme(legend.position = c(.2,.8),
legend.direction = "horizontal")
Pros - very straightforward; not hacky, just uses existing gtable API Cons - output is no longer a ggplot object
library(grid)
library(gtable)
p_gtable <- ggplot_gtable(ggplot_build(p))
grid.newpage()
grid.draw(p_gtable)
p_gtable_legend_quiet <- gtable_filter(p_gtable, "guide-box", invert = TRUE)
grid.newpage()
grid.draw(p_gtable_legend_quiet)
Pros - you get back a ggplot object and can keep adding layers to it Cons - quite a lot to search through and replace
p_global_theme <- ggplot2:::plot_theme(p)
typeof(p_global_theme)
## [1] "list"
names(p_global_theme)
## [1] "line" "rect"
## [3] "text" "title"
## [5] "aspect.ratio" "axis.title"
## [7] "axis.title.x" "axis.title.x.top"
## [9] "axis.title.x.bottom" "axis.title.y"
## [11] "axis.title.y.left" "axis.title.y.right"
## [13] "axis.text" "axis.text.x"
## [15] "axis.text.x.top" "axis.text.x.bottom"
## [17] "axis.text.y" "axis.text.y.left"
## [19] "axis.text.y.right" "axis.ticks"
## [21] "axis.ticks.x" "axis.ticks.x.top"
## [23] "axis.ticks.x.bottom" "axis.ticks.y"
## [25] "axis.ticks.y.left" "axis.ticks.y.right"
## [27] "axis.ticks.length" "axis.ticks.length.x"
## [29] "axis.ticks.length.x.top" "axis.ticks.length.x.bottom"
## [31] "axis.ticks.length.y" "axis.ticks.length.y.left"
## [33] "axis.ticks.length.y.right" "axis.line"
## [35] "axis.line.x" "axis.line.x.top"
## [37] "axis.line.x.bottom" "axis.line.y"
## [39] "axis.line.y.left" "axis.line.y.right"
## [41] "legend.background" "legend.margin"
## [43] "legend.spacing" "legend.spacing.x"
## [45] "legend.spacing.y" "legend.key"
## [47] "legend.key.size" "legend.key.height"
## [49] "legend.key.width" "legend.text"
## [51] "legend.text.align" "legend.title"
## [53] "legend.title.align" "legend.position"
## [55] "legend.direction" "legend.justification"
## [57] "legend.box" "legend.box.just"
## [59] "legend.box.margin" "legend.box.background"
## [61] "legend.box.spacing" "panel.background"
## [63] "panel.border" "panel.spacing"
## [65] "panel.spacing.x" "panel.spacing.y"
## [67] "panel.grid" "panel.grid.major"
## [69] "panel.grid.minor" "panel.grid.major.x"
## [71] "panel.grid.major.y" "panel.grid.minor.x"
## [73] "panel.grid.minor.y" "panel.ontop"
## [75] "plot.background" "plot.title"
## [77] "plot.title.position" "plot.subtitle"
## [79] "plot.caption" "plot.caption.position"
## [81] "plot.tag" "plot.tag.position"
## [83] "plot.margin" "strip.background"
## [85] "strip.background.x" "strip.background.y"
## [87] "strip.placement" "strip.text"
## [89] "strip.text.x" "strip.text.y"
## [91] "strip.switch.pad.grid" "strip.switch.pad.wrap"
## [93] "strip.text.y.left"
p_elements_with_colour <- Filter(Negate(is.null),
lapply(p_global_theme,
function(element) if (is.list(element))
element$colour))
p_elements_with_colour
## $line
## [1] "black"
##
## $rect
## [1] "black"
##
## $text
## [1] "black"
##
## $axis.text
## [1] "grey30"
##
## $axis.ticks
## [1] "grey20"
##
## $legend.background
## [1] NA
##
## $legend.key
## [1] NA
##
## $panel.background
## [1] NA
##
## $panel.grid
## [1] "white"
##
## $plot.background
## [1] "white"
##
## $strip.background
## [1] NA
##
## $strip.text
## [1] "grey10"
p +
aes(color = NULL) ->
p
p$layers[[1]]$geom$default_aes$colour ->
color
alpha(color, alpha = .3) ->
p$layers[[1]]$geom$default_aes$colour
p