我有一個圖,其 y 軸需要顯示密度而不是頻率。
這是我使用的代碼:
ggplot(stocks_orig, aes(x=Value)) geom_histogram(aes(y=..density..), colour="black", fill="white", bins=20) geom_density(aes(y=..density..),alpha=.2, fill="lightblue", size=1)
geom_vline(aes(xintercept = -0.019), linetype = "dashed", size = 1, color = "blue") annotate("text", x =0.0, y = 51, label ="number1")
geom_vline(aes(xintercept = -0.029), linetype = "dotted", size = 1, color = "blue") annotate("text", x =-0.051, y = 25, label = "number2")
labs(title="Title", subtitle="subtitle", caption="Caption")
這是我得到的圖,它顯示了頻率,盡管使用了 aes(y=..density..):

這是我的資料:
> dput(stocks_orig[1:10,])
structure(list(Date = structure(c(14613, 14614, 14615, 14616,
14617, 14620, 14621, 14622, 14623, 14624), class = "Date", tzone = "Europe/Prague"),
Growth = c(0.0139029051689914, -0.001100605444033, -0.000800320170769155,
-0.000300045009001992, 0.00359353551013022, 0.00169855663558151,
-0.00662187630888697, 0.00836491633162767, 0.00259662584726591,
-0.00944445882799969), Medium = c(0.0181345701954827, 0.00458945233380722,
0.00159872136369707, 0.00697561373642514, 0.00409161790325356,
0.000699755114273265, -0.0108587433348759, 0.00717420374800045,
0.00119928057548219, -0.0118701725704874), Value = c(0.0273232956488904,
0.0134096869099177, 0.0061808590750811, 0.0120273802127185,
0.000499875041650993, -0.000800320170769155, -0.021938907518754,
0.0119285708652738, 0.00379279823869626, -0.0170444346092585
)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000024c38fd1ef0>)
uj5u.com熱心網友回復:
你確定這不是密度?曲線要成為密度,它必須滿足三個規則(有關更多數學解釋,請參見例如
ggplot(NULL, aes(x = rnorm(5000, sd = 0.1)))
geom_density(aes(y=..density..), size = 1)

uj5u.com熱心網友回復:
我假設您希望箱高度加一,并且密度曲線遵循相同的縮放比例。默認行為是不同的,并且被設計成曲線下的面積總計為 1。這意味著對于窄 x 范圍,峰值密度可以遠高于 1。為了使 bin 的總高度添加到 1,您可以按 bin 寬度縮放輸出(您可以使用binwidth比更直接地控制bins)。
比較:
ggplot(mtcars, aes((wt-3)/100))
geom_histogram(aes(y=..density..), binwidth = 1/120)
geom_density(aes(y=..density..))

和
ggplot(mtcars, aes((wt-3)/100))
geom_histogram(aes(y=..density../120), binwidth = 1/120)
geom_density(aes(y=..density../120))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427636.html
上一篇:使用ggplot2的螺旋條形圖
