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

Ign*_*cio 9 r ggplot2

如果使用,是否可以在y轴上添加标签theme_fivethirtyeight?我试过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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

aos*_*ith 13

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

例如,您可以添加

theme(axis.title = element_text()) + ylab('Something')
Run Code Online (Sandbox Code Playgroud)

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

另一种方法是使用

theme(axis.title = element_text(), axis.title.x = element_blank()) + ylab('Something')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

作为axis.title.y继承axis.title,它只是设置axis.title.y为不起作用element_text().