我有一個相當大的資料框架(近10萬個觀測值,約有40個變數),我想讓ggplot用lm-或loess-線來繪制散點圖。但是這些線應該只根據每個觀察日期的變數值的某個四分位數來計算。我想在ggplot中直接進行過濾或子集,而不需要事先創建一個新的資料物件或子資料框架。
由于我的 "真實 "資料框架太大,我創建了一個虛構的例子,有144個觀測值的資料框架,名為df_Bandvals(代碼在帖子末尾)。
這里有以下結構,前25行和基于所有觀測值的loess線的散點圖
> str(df_Bandvals)
'data.frame': 144個5個變數的:
$ obsdate : int 190101 190101 190101 190101 190101 190101 190101 190101 190101 190101. ..
$ transsect : chr "A" "A" "A" "A" 。 ..
$ PointNr 。 num 1 2 3 4 5 6 1 2 3 4 . ..
$ depth : num 31 31 31 31 31 31 31 31 31 31. ..
$ Band12plusmin: num 169 241 229 159 221 196 188 216 233 149 . ..
> df_Bandvals
obsdate transsect PointNr depth Band12plusmin
1 190101 A 1 31 169
2 190101 A 2 31 241
3 190101 A 3 31 229
4 190101 A 4 31 159
5 190101 A 5 31 221
6 190101 A 6 31 196
7 190101 B 1 31 188
8 190101 B 2 31 216
9 190101 B 3 31 233
10 190101 B 4 31 149
11 190101 B 5 31 169
12 190101 B 6 31 181
13 190102 A 1 3 356
14 190102 A 2 3 368
15 190102 A 3 3 293
16 190102 A 4 3 261
17 190102 A 5 3 313
18 190102 A 6 3 374
19 190102 B 1 3 327
20 190102 B 2 3 409
21 190102 B 3 3 369
22 190102 B 4 3 334
23 190102 B 5 3 376
24 190102 B 6 3 318
25 190103 A 1 25 183
該圖顯示了深度與Band12plusmin的關系,并有一個相應的黃土線。點的顏色被分配到各自的觀測日期(obsdate)。每個觀測日期包括12次觀測。
現在,我的基本問題是。如何只根據每個觀測日期的低50%-四分位Band12plusmin-值來得到一條黃土線?或者換句話說,關于繪圖:ggplot應該只使用每種顏色的6個低點來計算線條。
而且正如之前提到的,我想直接在ggplot中進行過濾或子集,而不用事先創建一個新的資料物件或子資料框架。
我嘗試了一下子集,但在這種情況下,我的問題是我不能只指定一個通用的Band12plusmin-閾值,因為當然,每個obsdate-組的50%-閾值都是不同的。 我是R和ggplot的新手,所以,現在我沒能找到一個解決方案,比如說類-個體-衍生-閾值條件的過濾。 誰能幫幫我?
這里是資料框架和繪圖的代碼
。obsdate< -rep(c(190101。 190112)。 each=12, mode=factor)
transsect< -rep(rep(c("A"。 "B")。 each=6), 12)
PointNr< -rep(c)。 2, 3。 4, 5, 6), times=24)
深度< -rep(c(31。 3。 25, -9, 13, 18。 7, -10, 3, -4, 11,21)。 each=12)。
Band12< -rep(c(199。 349。 225, 844, 257, 231, 301, 875, 378, 521, 210,246) 。 each=12)
set.seed(13423)
plusminRandom< -round(rnorm(144。 平均=0。 sd=33))
plusminRandom
Band12plusmin<-Band12 plusminRandom
df_Bandvals<-data. frame(obsdate, transsect, PointNr, depth, Band12plusmin)
str(df_Bandvals)
head(df_Bandvals, 20)
library (ggplot2)
ggplot(data=df_Bandvals。 aes(x=depth, y=Band12plusmin))
scale_x_continuous(limits = c(-15。 35))
scale_y_continuous(limits = c(120。 960))
geom_point(Aes(color=factor(obsdate))。 大小=1. 5)
geom_smooth(method="loess"/span>)
uj5u.com熱心網友回復:
你應該能夠在geom_smooth()中使用data引數
ggplot(data = df_Bandvals, aes(x = depth, y = Band12plusmin))
scale_x_continuous(limits = c(-15。 35))
scale_y_continuous(limits = c(120。 960))
geom_point(aes(color = factors(obsdate))。 大小= 1. 5)
geom_smooth()
資料 = df_Bandvals %>%
group_by(obsdate) %>%
filter(Band12plusmin< 中位數(Band12plusmin)),
方法 = "loess"/span>
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330194.html
標籤:
上一篇:遞回強型別函式的型別化
下一篇:ggplot中的累積平均線圖

