如何使用 R 中的 gridExtra 在使用多重圖表的頂部新增標題?
gridExtra 資料包作為 ggplot2 中 par(mfrow) 的替代項,因此,我們可以使用 ggplot2 和 gridExtra 在一個圖表視窗中建立多重圖表。現在,如果我們想要為所有圖表加一個標題或者可以說是為多重圖表加一個主標題,可以使用 top 引數將標題放在標題的頂部。類似地,我們可以根據需要使用 bottom、left 和 right,但為此我們還需要 grid 資料包。
示例
考慮以下資料框架 -
set.seed(123) x1<-rnorm(10) x2<-rnorm(10,0.5) x3<-rnorm(10,0.8) x4<-rnorm(10,1.5) df<-data.frame(x1,x2,x3,x4) df
輸出
x1 x2 x3 x4 1 -0.56047565 1.72408180 -0.26782371 1.926464 2 -0.23017749 0.85981383 0.58202509 1.204929 3 1.55870831 0.90077145 -0.22600445 2.395126 4 0.07050839 0.61068272 0.07110877 2.378133 5 0.12928774 -0.05584113 0.17496073 2.321581 6 1.71506499 2.28691314 -0.88669331 2.188640 7 0.46091621 0.99785048 1.63778704 2.053918 8 -1.26506123 -1.46661716 0.95337312 1.438088 9 -0.68685285 1.20135590 -0.33813694 1.194037 10 -0.44566197 0.02720859 2.05381492 1.119529
示例
library(ggplot2)
library(gridExtra)
library(grid)
p1<-ggplot(df,aes(x1))+geom_histogram(bins=15)
p2<-ggplot(df,aes(x2))+geom_histogram(bins=15)
p3<-ggplot(df,aes(x3))+geom_histogram(bins=15)
p4<-ggplot(df,aes(x4))+geom_histogram(bins=15)
grid.arrange(p1,p2,p3,p4,nrow=2,top=textGrob("Histograms of 4 Variables"))
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP