2016-04-08 22 views
0

Yoğunluk çiziminin altında bir bölüm çizmek istiyorum ve mesafenin sabit sayıda piksel olmasını istiyorum. Mümkün mü? Mesafeyi nasıl kodlayacağımı biliyorum. Örneğin:Göreli 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") 

üretir:

enter image description here

Ancak

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") 

yoğunluğu

enter image description here

den çok daha uzak segment bir arsa üretir

cevap

2

Sen annotation_grob kullanabilirsiniz,

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

teşekkürler @baptiste! Örneğin segmenti nasıl biraz daha düşük yapabileceğinizi açıklayabilir misiniz? – Ignacio

+1

tune 'pos = birim (2," mm ")' yi beğeninize göre – baptiste

+0

, not çizelgenizin dışına düşerse ek açıklamanızın kaybolabileceğini unutmayın; bu durumda, expand_limits öğesini manuel olarak ayarlamanız gerekir. – baptiste