label.y.npc in stat_poly_eq change if colour is used in the main aes with several facet

Create issue
Issue #7 on hold
Former user created an issue

I used mensur data.frame (enclosed csv file)

When I used this ggplot lines all work fine with eq and R2 for the legend : gcor <- ggplot(mensur, aes(x=cuisse, y=biceps)) gcor + geom_point() + facet_wrap(~GN) + stat_smooth(method="lm", geom="smooth", show.legend = TRUE, fullrange = TRUE) + stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), label.x.npc = "right", label.y.npc = 0.15, formula = y ~ x, parse = TRUE, size = 3)

but if I used colour on the main aes, the legend decreases between each facet until disappearing completely. ? I don't understand the link between colour of the aes and the label.y.npc

gcor <- ggplot(mensur, aes(x=cuisse, y=biceps, colour=factor(GN)) gcor + geom_point() + facet_wrap(~GN) + stat_smooth(method="lm", geom="smooth", show.legend = TRUE, fullrange = TRUE) + stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), label.x.npc = "right", label.y.npc = 0.15, formula = y ~ x, parse = TRUE, size = 3)

enclosed mensuration.csv mensur <- read.csv(file="Exercices/mensurations.csv", head=T, sep = ";", row.names = 1)

Comments (6)

  1. Pedro J. Aphalo repo owner

    Odd indeed! I will look into this. Please, let me know which package versions you are using. Typing at the console what follows, immediately after the problem-causing example was run should show what is loaded in your R session.

    devtools::session_info()
    

    or just

    sessionInfo()
    

    The devtools function gives output that is tidier, but either will do. Just answer here pasting the output.

  2. lfrangeu

    all my versions :

    devtools::session_info() Session info ------------------------------------------------------------------------------------------ setting value
    version R version 3.4.3 (2017-11-30) system x86_64, darwin15.6.0
    ui RStudio (1.1.414)
    language (EN)
    collate en_US.UTF-8
    tz Europe/Paris
    date 2018-01-23

    Packages ---------------------------------------------------------------------------------------------- package * version date source
    assertthat 0.2.0 2017-04-11 CRAN (R 3.4.0) base * 3.4.3 2017-12-07 local
    bindr 0.1 2016-11-13 CRAN (R 3.4.0) bindrcpp 0.2 2017-06-17 CRAN (R 3.4.0) BiocInstaller * 1.28.0 2017-10-31 Bioconductor
    broom * 0.4.3 2017-11-20 CRAN (R 3.4.3) colorspace 1.3-2 2016-12-14 CRAN (R 3.4.0) compiler 3.4.3 2017-12-07 local
    datasets * 3.4.3 2017-12-07 local
    devtools * 1.13.4 2017-11-09 CRAN (R 3.4.2) digest 0.6.14 2018-01-14 CRAN (R 3.4.3) dplyr * 0.7.4 2017-09-28 CRAN (R 3.4.2) foreign 0.8-69 2017-06-22 CRAN (R 3.4.3) ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.0) ggpmisc * 0.2.16 2017-09-17 CRAN (R 3.4.2) glue 1.2.0 2017-10-29 CRAN (R 3.4.2) graphics * 3.4.3 2017-12-07 local
    grDevices * 3.4.3 2017-12-07 local
    grid 3.4.3 2017-12-07 local
    gtable 0.2.0 2016-02-26 CRAN (R 3.4.0) labeling 0.3 2014-08-23 CRAN (R 3.4.0) lattice 0.20-35 2017-03-25 CRAN (R 3.4.3) lazyeval 0.2.1 2017-10-29 CRAN (R 3.4.2) lubridate * 1.7.1 2017-11-03 CRAN (R 3.4.2) magrittr 1.5 2014-11-22 CRAN (R 3.4.0) MASS * 7.3-48 2017-12-25 CRAN (R 3.4.3) memoise 1.1.0 2017-04-21 CRAN (R 3.4.0) methods * 3.4.3 2017-12-07 local
    mnormt 1.5-5 2016-10-15 CRAN (R 3.4.0) munsell 0.4.3 2016-02-13 CRAN (R 3.4.0) nlme 3.1-131 2017-02-06 CRAN (R 3.4.3) parallel 3.4.3 2017-12-07 local
    pillar 1.1.0 2018-01-14 CRAN (R 3.4.3) pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.0) plyr * 1.8.4 2016-06-08 CRAN (R 3.4.0) polynom * 1.3-9 2016-12-08 CRAN (R 3.4.0) psych 1.7.8 2017-09-09 CRAN (R 3.4.3) purrr 0.2.4 2017-10-18 CRAN (R 3.4.2) R6 2.2.2 2017-06-17 CRAN (R 3.4.0) Rcpp 0.12.15 2018-01-20 CRAN (R 3.4.3) reshape2 1.4.3 2017-12-11 CRAN (R 3.4.3) rlang 0.1.6 2017-12-21 CRAN (R 3.4.3) scales 0.5.0 2017-08-24 CRAN (R 3.4.1) splus2R * 1.2-2 2016-09-02 CRAN (R 3.4.0) stats * 3.4.3 2017-12-07 local
    stringi 1.1.6 2017-11-17 CRAN (R 3.4.2) stringr 1.2.0 2017-02-18 CRAN (R 3.4.0) tibble * 1.4.2 2018-01-22 CRAN (R 3.4.3) tidyr 0.7.2 2017-10-16 CRAN (R 3.4.2) tools 3.4.3 2017-12-07 local
    utils * 3.4.3 2017-12-07 local
    withr 2.1.1 2017-12-19 CRAN (R 3.4.3) xts * 0.10-1 2017-12-20 CRAN (R 3.4.3) zoo * 1.8-1 2018-01-08 CRAN (R 3.4.3)

  3. Pedro J. Aphalo repo owner

    I replaced GN with GENRE in your code, and it works as expected. The text size is unchanged. The plotting area is smaller and the equation does not fit in the available space. The easiest solution is to move the legend from the side to the top. Or you can use parameters label.y and label.x to move the equation to a different position within the plotting area, using data coordinates. I also added justification.

    gcor <- ggplot(mensur, aes(x=cuisse, y=biceps, colour=factor(GENRE))) 
    gcor + geom_point() + facet_wrap(~GENRE) + 
      stat_smooth(method="lm", 
                  geom="smooth", 
                  show.legend = TRUE, 
                  fullrange = TRUE) + 
      stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
                   label.x = 50, 
                   label.y = 44, 
                   hjust = 0,
                   formula = y ~ x, 
                   parse = TRUE, 
                   size = 3)
    
  4. Pedro J. Aphalo repo owner

    Or pass vectors if you want the labels in a different position in each facet. What I do need to fix for the next release is that relative positions (using npc coordinates) are dependent on the range of the data, when it would be best for them to be relative to the plotting area. I need first to check how they behave in R in other contexts.

    gcor <- ggplot(mensur, aes(x=cuisse, y=biceps, colour=factor(GENRE))) 
    gcor + geom_point() + facet_wrap(~GENRE) + 
      stat_smooth(method="lm", 
                  geom="smooth", 
                  show.legend = TRUE, 
                  fullrange = TRUE) + 
      stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
                   label.x = 50, 
                   label.y = c(44, 23.5), 
                   hjust = 0,
                   formula = y ~ x, 
                   parse = TRUE, 
                   size = 3)
    
  5. lfrangeu

    Sorry for the GN issue, is an additional factor create with GENRE and NIVEAU. I'm not sure I totally understand your point. If you suspect that the problem lies with a link between label.y.npc and the range of the data, in this case, why this label does not move if we do not use the colour option in the main aes ? label.y is a good solution but not as pratical as label.y.npc.

  6. Log in to comment