将ylab添加到ggplot中,使用五十五度的ggtheme

时间:2016-03-23 15:15:53

标签: r ggplot2

如果您使用theme_fivethirtyeight,是否可以在y轴上添加标签?我试过ylab,但它不起作用:

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

1 个答案:

答案 0 :(得分:14)

你可以,但它需要比ylab更多的工作,因为你需要更改theme中默认设置的一些theme_fivethirtyeight设置。如果您查看theme_fivethirtyeight的代码(只需在控制台中运行theme_fivethirtyeight查看代码),您就会看到axis.title设置为element_blank()。所以这个主题根本没有轴标题。如果要设置y轴标签,则需要更改此项。

例如,您可以添加

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

到你的图表,但是你也会得到一个x轴标签。

另一种方法是使用

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

enter image description here

由于axis.title.y继承自axis.title,但仅将axis.title.y设置为element_text()并不起作用。