我正在使用ggplot和stat_ecdf函数创建频率图.我想将Y值添加到图表中以获取特定的X值,但是无法弄清楚如何.geom_point或geom_text似乎是可能的选项,但是当stat_ecdf自动计算Y时,我不知道如何在geom_point/text映射中调用该值.
我的初始情节的示例代码是:
x = as.data.frame(rnorm(100))
ggplot(x, aes(x)) +
stat_ecdf()
Run Code Online (Sandbox Code Playgroud)
现在我将如何在此处添加特定的yx点,例如x = -1处的y值.
最简单的方法是创建预先使用ECDF功能ecdf()从stats包装,然后使用绘制它geom_label().
library(ggplot2)
# create a data.frame with column name
x = data.frame(col1 = rnorm(100))
# create ecdf function
e = ecdf(x$col1)
# plot the result
ggplot(x, aes(col1)) +
stat_ecdf() +
geom_label(aes(x = -1, y = e(-1)),
label = e(-1))
Run Code Online (Sandbox Code Playgroud)