Search code examples
rggplot2cowplot

Alter default settings for cowplot theme


I like the default theme for cowplot, but I want to make some alterations. For example, I'd like to be able to adjust the default for legend.key. A MWE,

library(ggplot2); library(cowplot)

plt = ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) + geom_point() + theme(legend.key = element_rect(color = 'black'))

plt

However, this doesn't work.
enter image description here

Is there any way of adjusting the cowplot theme without having to redefine the whole dang thing manually?


Solution

  • The cowplot theme sets the default linetype of rects to 0, which means 'transparent':

    rect = element_rect(fill = "transparent", colour = NA, color = NA, size = 0, linetype = 0)
    

    Overriding that default give you what you want:

    library(ggplot2)
    library(cowplot)
    
    ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) + 
        geom_point() + 
        theme(legend.key = element_rect(color = 'black', linetype = 1))