如何使用 R 中的 ggplot2 為累積和建立點圖?
要使用 ggplot2 為累積和建立點圖,我們需要使用 aes 函式中因變數內部的 cumsum 函式,該函式用於描述變數的繪製方式。例如,如果我們有一個包含 x 和 y 列的資料框 df,其中 y 是因變數,那麼累加和的點圖可以建立為 ggplot(df,aes(1:20,y=cumsum(y)))+geom_point()。
示例
請考慮以下資料框 -
set.seed(666) x<-1:20 y<-rpois(20,5) df<-data.frame(x,y) df
輸出
x y 1 1 7 2 2 3 3 3 10 4 4 3 5 5 4 6 6 6 7 7 10 8 8 5 9 9 1 10 10 3 11 11 7 12 12 1 13 13 2 14 14 3 15 15 3 16 16 7 17 17 1 18 18 8 19 19 5 20 20 5
載入 ggplot2 軟體包並建立累積和點圖 -
library(ggplot2) ggplot(df,aes(1:20,y=cumsum(y)))+geom_point()
輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP