在 R 中使用 ggplot2 在散点图的 X 轴标签绘制长线条的办法?


当我们在 R 中创建一个绘图时,变量名称会自动绘制为轴标签,但有时我们希望给出 X 轴标签或 Y 轴标签的一个简短说明。如果说明过短,导致表达式函数不能包含标签长度,那么会变得困难,但可以通过表达式中的 atop 来实现。

示例

考虑以下数据框 −

 Live Demo

set.seed(123)
x<-rnorm(20)
y<-rpois(20,2)
df<-data.frame(x,y)
df

输出

    x    y
1 -0.56047565 1
2 -0.23017749 2
3 1.55870831 2
4 0.07050839 1
5 0.12928774 1
6 1.71506499 1
7 0.46091621 1
8 -1.26506123 2
9 -0.68685285 1
10 -0.44566197 4
11 1.22408180 0
12 0.35981383 2
13 0.40077145 3
14 0.11068272 0
15 -0.55584113 2
16 1.78691314 1
17 0.49785048 0
18 -1.96661716 3
19 0.70135590 4
20 -0.47279141 1
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()

输出

使用较长的 X 轴标签创建绘图 −

ggplot(df,aes(x,y))+geom_point()+
+ xlab(expression(atop("The points on the scatterplot are",paste("representing the relationship between x and y"))))

输出

更新日期: 21-Aug-2020

71 人查看

开启你的 职业

完成课程,获得认证

开始
广告
© . All rights reserved.