在 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 人檢視

開啟你的 職業

完成課程,獲得認證

開始
廣告