2016-04-09 16 views
0

my previous question ürününde oluşturduğum bu grafiğe daha fazla ek açıklama eklemek istiyorum. ggplot annotation_grob'a nokta ve metin ekleyin?

set.seed(40816) 
library(ggplot2) 
library(grid) 
df.plot <- data.frame(x = rnorm(100, 0, 1)) 

strainerGrob <- function(pos=unit(4,"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) 

enter image description here

ben nokta üzerinde 0 ve 0 segmentinde sağ, bir noktaya 1 soluna -1 eklemek istiyorum

. Tüm bu süre y için mutlak mesafe kullanmak zorunda kalmadan. Mümkün mü? Şu anda sert y

ggplot(df.plot, aes(x = x)) + geom_density() + 
    annotation_custom(strainerGrob(), xmin = -1, xmax = 1, ymin=-Inf, ymax=0) + 
    geom_point(aes(x=0, y=-0.01)) + 
    annotate("text", x = -1, y = -0.01, label = -1, hjust = 1.5) + 
    annotate("text", x = 1, y = -0.01, label = 1, hjust = -1) + 
    annotate("text", x = 0, y = 0, label = 0, vjust = 2.75) 

enter image description here

kodlama yapabilir Ama verileri değiştirmek durumunda noktası ve diğer açıklamaları yanlış yerde sonunda.

df.plot <- data.frame(x = rnorm(100, 0, 4)) 
ggplot(df.plot, aes(x = x)) + geom_density() + 
    annotation_custom(strainerGrob(), xmin = -1, xmax = 1, ymin=-Inf, ymax=0) + 
    geom_point(aes(x=0, y=-0.01)) + 
    annotate("text", x = -1, y = -0.01, label = -1, hjust = 1.5) + 
    annotate("text", x = 1, y = -0.01, label = 1, hjust = -1) + 
    annotate("text", x = 0, y = 0, label = 0, vjust = 2.75) 

enter image description here

cevap

1

grob birden çocuklu bir gTree olabilir, mesela

enter image description here

set.seed(40816) 
library(ggplot2) 
df.plot <- data.frame(x = rnorm(100, 0, 1)) 

strainerGrob <- function(range = c(-1,2), midpoint=0.35, 
         vpos=unit(5,"mm"), 
         pad = unit(1.5,"mm"), 
         gp=gpar(lty=2, lwd=2)){ 

    labels <- as.character(c(range[1], midpoint, range[2])) 
    xpos <- c(0, scales::rescale(midpoint, from=range, to=c(0,1)), 1) 
    sg <- segmentsGrob(0, unit(1,"npc") - vpos, 1, unit(1,"npc") - vpos, gp=gp) 
    tg <- textGrob(labels, x = unit(xpos, "npc") + c(-1,0,1)*pad, 
       hjust = c(1,0.5,0), 
       vjust=c(0.5,0,0.5), y=unit(1,"npc") - vpos + c(0,1,0)*pad) 
    pg <- pointsGrob(x=xpos[2], y=unit(1,"npc") - vpos, pch = 19, gp = gpar(cex=0.5)) 

    grobTree(sg, pg, tg) 
} 


# wrapper to ensure that both geom and grob are in sync with x values 
custom_range <- function(range = c(-1,2), midpoint=0.35, ...){ 
    sg <- strainerGrob(range=range, midpoint=midpoint, ...) 
    annotation_custom(sg, xmin = range[1], xmax = range[2], ymin=-Inf, ymax=0) 
} 

ggplot(df.plot, aes(x = x)) + geom_density() + 
    custom_range(c(-1, 2), 0.35) + 
    expand_limits(y=-0.1) 

o Grafiklerde çeşitli kullanacağız şey olursa , Örneğin facets ile veya birden fazla yerde, bunun yerine özel bir geom yazmanızı öneririm. Bir kerelik için, annotation_custom muhtemelen Tamam.

+0

Çok teşekkür ederim! Bu harika! – Ignacio

+0

'xmax' i 'xmax = 2' 'i değiştirdiğimde nokta ve' 0' hareket eder. Bana onları x = 0'da nasıl tutacağını gösterebilir misin? – Ignacio

+0

Sorunuzda ne yapmaya çalıştığınızı açıklayabilir misiniz? Soruyu sabit değerlerle cevapladım, ama asıl sorunundaki değişkenin ne olabileceği hakkında hiçbir fikrim yok. Veri koordinatlarını ve diğer ızgara ünitelerini karıştırmak genelde biraz zor olabilir. – baptiste