如何在R的ggplot2中建立線寬更大的分位數迴歸圖?


為了在R的ggplot2中建立線寬更大的分位數迴歸圖,我們可以按照以下步驟操作:

  • 首先,建立一個數據框。

  • 然後,使用ggplot2包中的stat_quantile函式(帶size引數)和geom_point函式來建立分位數迴歸圖。

示例

建立資料框

讓我們建立一個如下所示的資料框:

x<-rpois(25,2)
y<-rpois(25,5)
df<-data.frame(x,y)
df

輸出

執行上述指令碼後,將生成以下輸出(由於隨機化,此輸出可能因您的系統而異):

   x y
1  1 6
2  1 1
3  3 4
4  4 9
5  4 7
6  2 5
7  1 7
8  1 4
9  2 7
10 3 3
11 4 3
12 2 5
13 4 7
14 2 7
15 0 8
16 5 0
17 3 4
18 0 2
19 3 0
20 0 5
21 2 5
22 2 5
23 6 7
24 2 2
25 0 3

建立線寬更大的分位數迴歸圖

使用stat_quantile函式(帶size引數)和geom_point函式建立儲存在資料框df中的資料的分位數迴歸圖:

x<-rpois(25,2)
y<-rpois(25,5)
df<-data.frame(x,y)
library(ggplot2)

ggplot(df,aes(x,y))+geom_point()+stat_quantile(formula=y~x,quantiles=c(0.25,0.50,0.75
))

輸出

建立線寬更大的分位數迴歸圖

使用stat_quantile函式(帶size引數)和geom_point函式建立儲存在資料框df中的資料,並具有更大線寬的分位數迴歸圖:

x<-rpois(25,2)
y<-rpois(25,5)
df<-data.frame(x,y)
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()+stat_quantile(formula=y~x,quantiles=c(0.25,0.50,0.75
),size=2)

輸出

更新於:2021年11月16日

201次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.