如何使使用ggplot2在R中建立的圖中所有文字大小相同?
為了使使用ggplot2在R中建立的圖中所有文字大小相同,我們可以按照以下步驟操作:
- 首先,建立一個數據框。
- 然後,使用ggplot2建立圖表。
- 之後,使用theme函式建立相同的圖表,並使用text引數更改文字大小。
建立資料框
讓我們建立一個如下所示的資料框:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) df
執行上述指令碼後,將生成以下輸出(由於隨機化,此輸出將在您的系統上有所不同):
x y 1 25 50 2 46 28 3 29 10 4 16 8 5 37 45 6 41 6 7 27 40 8 28 34 9 33 4 10 10 21 11 40 7 12 36 42 13 18 33 14 11 23 15 5 19 16 24 24 17 23 12 18 48 22 19 7 39 20 4 49 21 19 43 22 15 5 23 2 36 24 44 20 25 34 15
使用ggplot2建立圖形
使用geom_point在x和y之間建立散點圖:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
輸出
建立具有相同文字大小的上述圖形
將theme函式新增到上述指令碼中,並將圖中顯示的所有文字的文字大小設定為20:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+theme(text=element_text(size=20))
輸出
廣告