Skip to content

theme_update(text) fails due to NULL values #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oneillkza opened this issue Jan 14, 2016 · 9 comments
Closed

theme_update(text) fails due to NULL values #1489

oneillkza opened this issue Jan 14, 2016 · 9 comments

Comments

@oneillkza
Copy link

It's possible, using theme(), to set individual font elements inline with plotting, eg (adapted from ggplot documentation example):

df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
ggplot(df, aes(x = gp, y = y)) + 
    geom_point() +
    theme(text=element_text(size=16))

However, when using theme_update to try to set this globally, it complains about NULL values in the text element.

theme_update(text=element_text(size=16))
ggplot(df, aes(x = gp, y = y)) + 
    geom_point()

results in

Error in FUN(X[[i]], ...) : 
  Theme element 'text' has NULL property: family, face, colour, hjust, vjust, angle, lineheight, margin, debug

I don't know if this is intended, but it seems strange that the behaviour of theme() and update_theme() is different, and it's frustrating that it isn't possible to set font size globally.

(This is with ggplot2_2.0.0.)

@wmayner
Copy link

wmayner commented Jan 19, 2016

I'm having the same problem, though I'm updating the theme manually with

my.theme = theme_grey() + theme(text=element_text(family='Roboto', size=30))

As with OP, the same modification on a ggplot object works fine; it's only when trying to modify the default theme that this error is raised.

@oneillkza
Copy link
Author

Interestingly, as a work-around, the following does work:

theme_set(theme_bw(base_size = 16))
ggplot(df, aes(x = gp, y = y)) + 
    geom_point()

@hadley
Copy link
Member

hadley commented Jan 25, 2016

The base theme settings which are inherited by other settings need to be fully specified. Otherwise nothing gets inherited.

@hadley hadley closed this as completed Jan 25, 2016
@oneillkza
Copy link
Author

I see. So the solution for actually updating a single element of an element of a theme is something like:

theme_update(text=(theme_grey()+theme(text = element_text(family = "Times")))$text )

That's pretty ugly; I'd expect theme_update to pull the base theme settings from the theme being updated, rather than requiring them to all be specified. It's your package, though.

@hadley
Copy link
Member

hadley commented Jan 25, 2016

@oneillkza there's probably a better way to do it than that, but I hardly ever use themes so I'm the wrong person to ask.

@oneillkza
Copy link
Author

Thanks Hadley, that's understandable.

Hmm ... looking at the actual definition for theme_update(), it seems like the issue is that it uses %+replace% instead of +.

theme_update <- function(...) {
# Make a call to theme, then add to theme
theme_set(theme_get() %+replace% theme(...))
}

So for now the better way to update individual elements in a theme is to bypass theme_update and just to use theme_set directly:

theme_set(theme_get() + theme(text=element_text(size=16)))
theme_get()$text

It'd be nice to have an update function that uses +, but I'll create a new issue articulating this, since this is becoming more of a feature request.

@oneillkza
Copy link
Author

Or, rather, I'll make a pull request ;)

@eejd
Copy link

eejd commented Nov 13, 2016

@oneillkza , Can't you just use the theme() for your needs?

@oneillkza
Copy link
Author

@eejd In a round-about way (see conversation above), but anyway this problem was solved in #1519 , so I can just use the current version of the package ;)

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants