如何在 R 中使用 ggplot2 更改 X 轴标签的颜色?
标签的默认颜色为黑色,但我们可能希望将其更改为其他颜色,这样一来,如果需要,我们可以让查看者关注标签。若要使用 ggplot2 更改 X 轴标签的颜色,我们可以使用具有 axis.title.x 参数的主题函数,该参数可用于更改标签值的颜色。
示例
考虑下面的数据框 -
x<−rnorm(20,5,0.25) y<−rnorm(20,5,0.004) df<−data.frame(x,y) df
输出
x y 1 5.030380 4.997751 2 5.240119 4.998680 3 4.544677 4.999195 4 4.858193 4.998952 5 5.071308 5.001092 6 5.512129 4.998037 7 5.236292 5.002558 8 5.081739 5.001903 9 4.940688 5.005155 10 4.437274 5.002876 11 5.472674 4.997229 12 4.813980 5.005284 13 4.859050 4.993907 14 5.353379 4.998850 15 5.537771 4.988465 16 5.302592 4.999421 17 5.486402 4.991891 18 5.121558 5.003857 19 4.908864 5.003393 20 5.137028 5.008340
加载 ggplot2 包并创建一个散点图 -
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
输出
在散点图中创建带有红色 X 轴标签 -
示例
ggplot(df,aes(x,y))+geom_point()+theme(axis.title.x=element_text(colour="red"))
输出
广告