1000 の間の一連の乱数を使用して R で密度曲線を作成し、特定の値以下の部分をシェーディングしようとしています。geom_area
orを含む多くのソリューションがありますがgeom_ribbon
、それらはすべて、yval
私が持っていない を必要とします (これは 1000 個の数値のベクトルです)。どうすればこれを行うことができるかについてのアイデアはありますか?
その他の 2 つの関連する質問:
- 累積密度関数(現在、生成に使用しています)に対して同じことを行うことは可能です
stat_ecdf
か、それともまったくシェーディングすることはできますか? geom_vline
y 軸全体ではなく、密度曲線の高さまでしか上がらないように編集する方法はありますか?
コード: (これgeom_area
は、私が見つけたいくつかのコードを編集しようとして失敗したものです。ymax
手動で設定すると、曲線の下の領域だけでなく、プロット全体を占める列が得られます)
set.seed(100)
amount_spent <- rnorm(1000,500,150)
amount_spent1<- data.frame(amount_spent)
rand1 <- runif(1,0,1000)
amount_spent1$pdf <- dnorm(amount_spent1$amount_spent)
mean1 <- mean(amount_spent1$amount_spent)
#density/bell curve
ggplot(amount_spent1,aes(amount_spent)) +
geom_density( size=1.05, color="gray64", alpha=.5, fill="gray77") +
geom_vline(xintercept=mean1, alpha=.7, linetype="dashed", size=1.1, color="cadetblue4")+
geom_vline(xintercept=rand1, alpha=.7, linetype="dashed",size=1.1, color="red3")+
geom_area(mapping=aes(ifelse(amount_spent1$amount_spent > rand1,amount_spent1$amount_spent,0)), ymin=0, ymax=.03,fill="red",alpha=.3)+
ylab("")+
xlab("Amount spent on lobbying (in Millions USD)")+
scale_x_continuous(breaks=seq(0,1000,100))