Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct label on smoothed line methods not working? #24

Closed
markbneal opened this issue Apr 21, 2020 · 5 comments
Closed

Direct label on smoothed line methods not working? #24

markbneal opened this issue Apr 21, 2020 · 5 comments

Comments

@markbneal
Copy link

Hi, I am trying to use the method as per this stackoverflow answer here

This code...

library("ggplot2")
#install.packages("directlabels")
library(directlabels)
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)) )

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

produces this error:

Warning message:
Computation failed in `stat_smooth()`:
object 'last.qp' of mode 'function' was not found 

Interestingly, as a side issue, I went to run the code in the documentation from geom_dl() and i got fatal errors from Rstudio, when printing some objects, as noted in the code. I tested on Rstudio cloud, and that code worked fine there, but I'm running a pretty standard and neat setup - happy to give more details if this can't be reproduced.

#from geom_dl help
  vad <- as.data.frame.table(VADeaths)
  names(vad) <- c("age","demographic","deaths")
  ## color + legend
  leg <- ggplot(vad,aes(deaths,age,colour=demographic))+
    geom_line(aes(group=demographic))+
    xlim(8,80)
  print(direct.label(leg,list("last.points",rot=30)))
  ## this is what direct.label is doing internally:
  labeled <- leg+
    geom_dl(aes(label=demographic), method=list("last.points",rot=30))+
    scale_colour_discrete(guide="none")
  print(labeled)
  ## no color, just direct labels!
  p <- ggplot(vad,aes(deaths,age))+
    geom_line(aes(group=demographic))+
    geom_dl(aes(label=demographic),method="top.qp") #error when printing !!!!!!!!!!
  print(p)
  ## add color:
  p+aes(colour=demographic)+
    scale_colour_discrete(guide="none")
  ## add linetype:
  p+aes(linetype=demographic)+
    scale_linetype(guide="none")
  ## no color, just direct labels
  library(nlme)
  bwbase <- ggplot(BodyWeight,aes(Time,weight,label=Rat))+
    geom_line(aes(group=Rat))+
    facet_grid(.~Diet)
  bw <- bwbase+geom_dl(method="last.qp") #error when printing !!!!!!!!!!
  print(bw)
  ## add some more direct labels
  bw2 <- bw+geom_dl(method="first.qp")
  print(bw2)
  ## add color
  colored <- bw2+aes(colour=Rat)+
    scale_colour_discrete(guide="none")
  print(colored)
  ## or just use direct.label if you use color:
  direct.label(bwbase+aes(colour=Rat),dl.combine("first.qp","last.qp"))
  
  ## iris data example
  giris <- ggplot(iris,aes(Petal.Length,Sepal.Length))+
    geom_point(aes(shape=Species))
  giris.labeled <- giris+
    geom_dl(aes(label=Species),method="smart.grid")+
    scale_shape_manual(values=c(setosa=1,virginica=6,versicolor=3),
                       guide="none")
  ##png("~/R/directlabels/www/scatter-bw-ggplot2.png",h=503,w=503)
  print(giris.labeled)
  ##dev.off()
@markbneal
Copy link
Author

Sessioninfo()

R version 3.6.1 (2019-07-05)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252    LC_MONETARY=English_New Zealand.1252
[4] LC_NUMERIC=C                         LC_TIME=English_New Zealand.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] directlabels_2020.1.31 ggplot2_3.3.0         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3       rstudioapi_0.11  magrittr_1.5     splines_3.6.1    tidyselect_1.0.0 munsell_0.5.0    lattice_0.20-40 
 [8] colorspace_1.4-1 R6_2.4.1         rlang_0.4.5      quadprog_1.5-8   dplyr_0.8.4      tools_3.6.1      ggpmisc_0.3.3   
[15] grid_3.6.1       packrat_0.5.0    gtable_0.3.0     nlme_3.1-144     mgcv_1.8-31      withr_2.1.2      digest_0.6.25   
[22] assertthat_0.2.1 tibble_2.1.3     lifecycle_0.2.0  crayon_1.3.4     Matrix_1.2-18    farver_2.0.3     purrr_0.3.3     
[29] glue_1.3.1       labeling_0.3     compiler_3.6.1   pillar_1.4.3     scales_1.1.0     pkgconfig_2.0.3 

@tdhock
Copy link
Owner

tdhock commented Jun 20, 2020

this issue happens because stat_smooth also has a "method" parameter. the method="last.qp" is meant to be passed to geom_dl, but ggplot2 (mistakenly?) passes it to stat_smooth, which rightly raises the warning because it does not know anything about last.qp.
I posted an issue with ggplot2 tidyverse/ggplot2#4073 so maybe they will suggest a fix.
BTW in 2013 at the time of my SO answer this was working, but ggplot2 changed something (how the extra params/arguments are handled?) so that now it no longer works.

@tdhock
Copy link
Owner

tdhock commented Jun 24, 2020

a work-around with the current code is to calculate the smooth yourself (instead of using stat_smooth), save the smooth lines in a data table, and then plot/label it via geom_line/geom_dl.

@tdhock
Copy link
Owner

tdhock commented Jul 14, 2020

another work-around (override stat_params manually p$layers[[2]]$stat_params$method <- NULL) is mentioned in tidyverse/ggplot2#4073 (comment) and this seems to be an issue with ggplot2 so I'm closing this issue.

@tdhock
Copy link
Owner

tdhock commented Nov 4, 2020

another work-around mentioned in #31 is using direct.label(ggplot.object, method=something) instead of ggplot.object+geom_dl(), which works because direct.label internally uses the same "override stat_params" trick, dlgeom$stat_params <- L$stat_params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants