在制作與深度和沉降時間相關的 Remotion 列線圖時,如果曲線(如拋物線)低于其上十個(7 到 10 和 18 到 20),我需要擬合曲線(作為拋物線)來去除標簽。這非常接近我的需要。
data.frame(
depth=rep(seq(0.5, 3.5, 0.5), each=8),
time=rep(seq(0, 280, 40), times=7),
ss = c(
820,369,238,164,107,66,41,33,
820,224,369,279,213,164,115,90,
820,631,476,361,287,230,180,148,
820,672,558,426,353,287,238,187,
820,713,590,492,402,344,262,230,
820,722,615,533,460,394,320,262,
820,738,656,574,492,418,360,303)
) %>%
transmute(
depth = depth,
time = time,
R = 100*(1- ss/820)
) %>%
mutate(G=factor(round(R, digits=-1))) %>%
ggplot(aes(x=time, y=depth, colour=time))
geom_label(aes(label=round(R)))
scale_y_continuous(trans = "reverse")
geom_path(aes(group=G))
但它沒有得到拋物線曲線。我怎樣才能在十條件下平滑它們?
uj5u.com熱心網友回復:
我不確定這是否是你要找的。我將資料和繪圖分開并應用于stat_smooth
每個組。不幸的是,平滑線不遵循配色方案。您還將看到對創建樣條線的方法產生的幾個警告。
plt <- ggplot(df1, aes(x=time, y=depth, colour = time))
geom_label(aes(label=round(R)))
scale_y_continuous(trans = "reverse")
geom_path(aes(group=G), size = .6, alpha = .5)
lapply(1:length(unique(df1$G)),
function(i){
df2 <- df1 %>% filter(G == unique(G)[i])
plt <<- plt
stat_smooth(data = df2, size = .5,
aes(x = time, y = depth),
se = F, method = lm, color = "darkred",
formula = y ~ splines::bs(x, knots = nrow(df2)))
})
您可以使用其他引數進一步擴展它。我只是不確定你到底在期待什么。
plt <- ggplot(df1, aes(x=time, y=depth, colour = time))
geom_label(aes(label=round(R)))
scale_y_continuous(trans = "reverse")
geom_path(aes(group=G), size = .6, alpha = .5)
lapply(1:length(unique(df1$G)),
function(i){
df2 <- df1 %>% filter(G == unique(G)[i])
# u <- df1 %>% {nrow(unique(.[,c(1:2)]))}
plt <<- plt
stat_smooth(
data = df2, size = .5,
aes(x = time, y = depth),
se = F, method = lm, color = "darkred",
formula = y ~ splines::bs(x, knots = nrow(df2),
degree = ifelse(nrow(df2) <= 4,
3, nrow(df2) - 2)))
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485241.html
上一篇:在R中呼叫ggMarginal后正確調整多個ggExtraPlot物件的大小
下一篇:ggplot2中回歸模型的殘差圖