Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ylab to ggplot with fivethirtyeight ggtheme

Tags:

r

ggplot2

Is it possible to add a label to the y axis if you are using theme_fivethirtyeight? I tried ylab but it does not work:

library(ggplot2)
library(ggthemes)
p2 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) +
  geom_point() +
  ggtitle("Cars")
p2 + geom_smooth(method = "lm", se = FALSE) +
  scale_color_fivethirtyeight("cyl") +
  theme_fivethirtyeight() + ylab('SOMETHING')

enter image description here

like image 954
Ignacio Avatar asked Mar 23 '16 15:03

Ignacio


1 Answers

You can, but it'll take a bit more work than ylab because you need to change some of the theme settings that are the defaults in theme_fivethirtyeight. If you take a look at the code for theme_fivethirtyeight (just run theme_fivethirtyeight in your console to see the code), you'll see that axis.title is set to element_blank(). So this theme has no axis titles at all. You'll need to change this if you want to set a y axis label.

For example, you could add

theme(axis.title = element_text()) + ylab('Something')

to your graph, but then you'll get an x axis label, as well.

An alternative would be to use

theme(axis.title = element_text(), axis.title.x = element_blank()) + ylab('Something')

enter image description here

Asaxis.title.y inherits from axis.title, it didn't work to just set axis.title.y to element_text().

like image 194
aosmith Avatar answered Oct 30 '22 07:10

aosmith