使用以下数据:
> str(attribute)
'data.frame': 431 obs. of 2 variables:
$ pos: int 1 2 3 4 5 6 7 8 9 10 ...
$ att: num 0.652 0.733 0.815 1.079 0.885 ... *[between 0 and 3]
Run Code Online (Sandbox Code Playgroud)
和:
ggplot(attribute, aes(x=pos, y=att)) + geom_line() + geom_smooth()
Run Code Online (Sandbox Code Playgroud)
我做了:

我想逐渐平滑黑色曲线,而不是"geom_smooth默认值".我试过n,level选择,但没有做我想要的.哪个是逐步增加平滑度的最佳方法?(例如,在一个中平均2个值,然后在一个中尝试3个,依此类推).我想这是非常容易或无法使用的东西geom_smooth,但我不知道搜索/寻找什么.谢谢.
我试图将观察到的值绘制为与预期值相对的点,如下所示:
d <- data.frame(
ranks = 1:9,
observed = c(0.736, 0.121, 0.067, 0.034, 0.026, 0.015, 0.001, 0.001, 0.000),
expected = c(0.735, 0.136, 0.051, 0.025, 0.015, 0.009, 0.006, 0.005, 0.003)
)
ggplot(d, aes(x=ranks, y=observed)) +
geom_point(size=2.2) +
geom_line(aes(x=ranks, y=expected), size=0.8, colour='red')
Run Code Online (Sandbox Code Playgroud)
这是正确的,但我更希望线条很好地平滑(没有肘部)。使用geom_smooth()withloess或gam并没有真正帮助,因为两者都过度平滑(以不同的方式)。有什么建议吗?
更新:如果这很有用,以下是我生成预期值的方式:
# BACIS POWER FUNCTION:
fPow <- function(x, a, b) {a * x^b}
# INITIALIZE PARAMETERS:
est1 <- coef(nls(observed ~ fPow(ranks, a, b),
start=c(a=1, b=1), data=d))
# FITTING:
nlfit1 <- nls(observed ~ …Run Code Online (Sandbox Code Playgroud) 我想使用 ggplot2 和 plotly 在 R 中模拟 Excel 平滑样式。
套餐
library(dplyr)
library(tibble)
library(ggplot2)
library(plotly)
library(ggforce) #only for geom_bspline() in example below (other smoothing functions can be used)
library(scales) #only for percent() formatting below
Run Code Online (Sandbox Code Playgroud)
示例数据集
df <- tibble(Group = rep(LETTERS[1:2], each = 10),
x = rep(1:10, 2),
y = c(2, 5, 9, 15, 19, 19, 15, 9, 5, 2,
1, 0, 3, 2, 3, 4, 14, 24, 24, 25)*0.01)
Run Code Online (Sandbox Code Playgroud)
我想要的是
到目前为止我所拥有的
(df %>%
ggplot(aes(x, y)) +
geom_point(aes(color = Group)) +
ggforce::geom_bspline(aes(group …Run Code Online (Sandbox Code Playgroud) 我的数据看起来像:
year mean.streak
1958 2.142857
1959 3.066667
1960 2.166667
1961 2.190476
Run Code Online (Sandbox Code Playgroud)
我的局部回归图的代码如下所示:
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+
geom_point(color = 'black')+
geom_smooth(method = 'loess')
Run Code Online (Sandbox Code Playgroud)
和输出:
我想通过将平滑线穿过所有数据点而不是典型的锯齿状来捕获数据的正弦模式geom_line。我通过编写以下内容尝试多项式插值:
ggplot(df)+
geom_point(aes(x = year, y = mean.streak, colour = year), size = 3) +
stat_smooth(aes(x = year, y = mean.streak), method = "lm",
formula = y ~ poly(x, 57), se = FALSE)
Run Code Online (Sandbox Code Playgroud)
摘自该线程。但我收到错误:
Warning message:
Computation failed in `stat_smooth()`:
'degree' must be less than …Run Code Online (Sandbox Code Playgroud)