2016-04-08 8 views
0

密度プロットの下に線分を描きたいので、距離を一定のピクセル数にしたいと思います。これは可能ですか?私は距離をハードコードする方法を知っています。例えば:相対yを使用して密度プロットの下の線分を描画しますか?

set.seed(40816) 
library(ggplot2) 
df.plot <- data.frame(x = rnorm(100, 0, 1)) 
ggplot(df.plot, aes(x = x)) + geom_density() + 
    geom_segment(aes(x = -1, y = -0.05, xend = 1, yend = -0.05), 
       linetype = "longdash") 

が生成:

enter image description here

しかし

df.plot <- data.frame(x = rnorm(100, 0, 4)) 
ggplot(df.plot, aes(x = x)) + geom_density() + 
    geom_segment(aes(x = -1, y = -0.025, xend = 1, yend = -0.025), 
       linetype = "longdash") 

ははるか遠く密度

enter image description hereからセグメントとのプロットを生成します

答えて

2

あなたは、おかげで@baptiste

set.seed(40816) 
library(ggplot2) 
df.plot <- data.frame(x = rnorm(100, 0, 1)) 
strainerGrob <- function(pos=unit(2,"mm"), gp=gpar(lty=2, lwd=2)) 
    segmentsGrob(0, unit(1,"npc") - pos, 1, unit(1,"npc") - pos, gp=gp) 

ggplot(df.plot, aes(x = x)) + geom_density() + 
    annotation_custom(strainerGrob(), xmin = -1, xmax = 1, ymin=-Inf, ymax=0) + 
    expand_limits(y=-0.1) 

enter image description here

+0

、annotation_grobを使用することができます!セグメントを少し低くする方法を説明できますか? – Ignacio

+1

'pos = unit(2、" mm ")'を好きなように調整してください。 – baptiste

+0

注釈がプロットパネルの外にある場合は注釈が消えるかもしれないので、手動でexpand_limitsを調整する必要があります。 – baptiste

関連する問題