How to show directlabels after geom_smooth and not after geom_line?

╄→尐↘猪︶ㄣ 提交于 2019-12-19 09:58:12

问题


I'm using directlabels to annotate my plot. As you can see in this picture the labels are after geom_line but I want them after geom_smooth. Is this supported by directlabels? Or any other ideas how to achieve this? Thanks in advance!

This is my code:

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

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



回答2:


This is not what you asked for as I don't know how to do that, but this might be more useful to you as you will lose less plotting area to labels:

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)

which yields:

You could also try:

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)

which yields:

NOTE: to print to other graphics devices (this was the windows rgui) you'll need to tweak the vjust and hjust to suit. But if there's a more direct way that would be nicer.




回答3:


I'm gonna answer my own question here, since I figured it out thanks to a response from Tyler Rinker.

This is how I solved it using loess() to get label positions.

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

Which will generate this plot: http://i.stack.imgur.com/FGK1w.png



来源:https://stackoverflow.com/questions/10065196/how-to-show-directlabels-after-geom-smooth-and-not-after-geom-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!