2012-04-08 64 views
2

我使用直接标签来标注我的情节。正如你在这幅图中看到的那样,标签在geom_line之后,但是我想在geom_smooth之后找到它们。这是由直接标签支持吗?或者其他任何想法如何实现这一目标?提前致谢!如何在geom_smooth之后显示directlabels而不是在geom_line之后显示?

图片情节:http://i.stack.imgur.com/2vZmk.png

这是我的代码:

library("ggplot2") 
set.seed(124234345) 

# Generate data 
df.2 <- data.frame("n_gram" = c("word1"), 
        "year" = rep(100:199), 
        "match_count" = runif(100 ,min = 1000 , max = 2000)) 

df.2 <- rbind(df.2, data.frame("n_gram" = c("word2"), 
         "year" = rep(100:199), 
         "match_count" = runif(100 ,min = 1000 , max = 2000))) 

# plot 
ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) + 
    geom_line(alpha = I(7/10), color="grey", show_guide=F) + 
    stat_smooth(size=2, span=0.3, se=F, show_guide=F) + 
    geom_dl(aes(label=n_gram), method = "last.bumpup", show_guide=F) + 
    xlim(c(100,220)) 

回答

-3

我要在这里回答我自己的问题,因为我从Tyler Rinker的回复中找出答案。

这就是我使用loess()获得标签位置的方法。

# Function to get last Y-value from loess 
funcDlMove <- function (n_gram) { 

    model <- loess(match_count ~ year, df.2[df.2$n_gram==n_gram,], span=0.3) 
    Y <- model$fitted[length(model$fitted)] 
    Y <- dl.move(n_gram, y=Y,x=200) 
    return(Y) 
} 

index <- unique(df.2$n_gram) 
mymethod <- list(
    "top.points", 
    lapply(index, funcDlMove) 
) 

# Plot 

PLOT <- ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) + 
    geom_line(alpha = I(7/10), color="grey", show_guide=F) + 
    stat_smooth(size=2, span=0.3, se=F, show_guide=F) 

direct.label(PLOT, mymethod) 

将产生该地块:http://i.stack.imgur.com/FGK1w.png

-1

这不是你要的,因为我不知道该怎么做,但是这可能是更有益你,你会输少绘制区域标签:

PLOT <- ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) + 
    geom_line(alpha = I(7/10), color="grey", show_guide=F) + 
    stat_smooth(size=2, span=0.3, se=F, show_guide=F) 

mymethod <- list(
    "top.points", 
    dl.move("word1", hjust=-6.65, vjust=13), 
    dl.move("word2", hjust =-7.9, vjust=20.25) 
) 

direct.label(PLOT, mymethod) 

这将产生:

enter image description here

您也可以尝试:

mymethod <- list(
    "top.points", 
    dl.move("word1", hjust=-6, vjust=14), 
    dl.move("word2", hjust =-7.1, vjust=19.5) 
) 

ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) + 
    geom_line(alpha = I(7/10), color="grey", show_guide=F) + 
    xlim(c(100,220))+ 
    stat_smooth(size=2, span=0.3, se=F, show_guide=F) + 
    geom_dl(aes(label=n_gram), method = mymethod, show_guide=F) 

这将产生:

enter image description here

注:打印到其他图形设备(这是Windows的RGUI),您需要调整vjust和hjust以适应。但是,如果有更直接的方式会更好。

+0

谢谢!如果我在具有更多组的数据集上使用它,手动调整每个标签有点繁琐。但我用你的想法来创建一个功能来自动完成此操作。我会发布这个作为我自己问题的答案,我希望这是适当的礼仪。 – 2012-04-09 06:41:44

+0

是的,请做。这个论坛的重点(至少对我来说)是以可搜索的格式获得问题的最佳解决方案。有时我们会根据别人的反应而改进它们。 – 2012-04-09 13:55:35

1
# use stat smooth with geom_dl to get matching direct labels. 
span <- 0.3 
ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) + 
    geom_line(alpha = I(7/10), color="grey") + 
    stat_smooth(size=2, span=span, se=F) + 
    geom_dl(aes(label=n_gram), method = "last.qp", stat="smooth", span=span) + 
    xlim(c(100,220))+ 
    guides(colour="none") 
相关问题