0

As you know, there are many themes in ggplot2 such as theme_economist, theme_minimal, theme_tufte, etc.

For instance, I would like to add this code to my visualisation:

theme_economist(axis.text.x = element_text(angle = 90))

But I am getting the "unused argument" error because I can add this argument to only theme function.

You can simply use this code:

ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5) + theme_economist()

Do you have any suggestion?

Thanks in advance.

datazang
  • 989
  • 1
  • 7
  • 20
  • 2
    Why not ```ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5) + theme_economist() + theme(axis.text.x = element_text(angle = 90)) ``` – Jack Brookes Dec 23 '19 at 19:32
  • 1
    Or `your_theme <- function(...) theme_economist() + theme(...)`. Then do `your_plot + your_theme(axis.text.x = element_text(angle = 90))` – markus Dec 23 '19 at 19:33

1 Answers1

1

It seems like you have two options.

Option 1

Create a new function that adds the elements of the theme to the original function, then use that function in ggplot() sequence. Here, theme_new is the new theme with the tick labels at 90 degrees. Note that when you use theme_new you omit the parentheses (i.e., theme_new and not theme_new()).

library(ggplot2)
library(ggthemes)

df <- data.frame(
    x = rnorm(1000, 500, 100),
    y = rnorm(1000, 500, 100)
)

theme_new <- theme_economist() + theme(
    axis.text.x = element_text(angle = 45)
)

ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    theme_new

Option 2:

Create your own theme by copying the theme_economist() definition in the ggthemes library and replace the code with the design elements that you want. If you want to view the definition, you can view it here.

mikebader
  • 1,075
  • 3
  • 12