eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
406 stars 18 forks source link

new_scale_color() not working with geom_segment. #19

Closed Maschette closed 4 years ago

Maschette commented 5 years ago

Hi Elio, I'm not sure if this is an error with new_scale_color() or me being an idiot in its use (probably the latter).

When I try and use new_scale_color() and then geom_segment it recolors the previous layer as well.

dummy<-data.frame(x=runif(100, -180, 180), y=rnorm(100), col=rep(1:5,20))
segs<-data.frame(area=c("WPA","EPA","AOA","CIA","EIA","WPA"), xmin=c(-180, -124,-56.9,30.1,115,170.1), xmax=c(-124.8, -57,30,115,170,180), col=c(1,2,3,4,5,1), y=1.1)

library(ggplot2)
library(ggnewscale);library(dplyr)

ggplot(data=dummy, aes(x=x, y=y, col=as.factor(col)))+geom_point()+scale_color_manual(values = c(1,2,3,4,5), name="colors")


ggplot(data=dummy, aes(x=x, y=y, col=as.factor(col)))+geom_point()+scale_color_manual(values = c(1,2,3,4,5), name="colors")+
    new_scale_color()+
    geom_segment(data=segs, aes(x=xmin, y=1.01, xend=xmax, yend=1.01, col=area), inherit.aes = FALSE, lwd=2) +
    scale_color_viridis_d( name = "Area",option="D")

Created on 2019-10-04 by the reprex package (v0.3.0)

eliocamp commented 5 years ago

Are you using the latest version? I think this is an error fixed in the developement version.

devtools::install_github("eliocamp/ggnewscale")

Here, it works :point_down:

dummy <- data.frame(x = runif(100, -180, 180), y = rnorm(100), col = rep(1:5, 20))
segs <- data.frame(
  area = c("WPA", "EPA", "AOA", "CIA", "EIA", "WPA"),
  xmin = c(-180, -124, -56.9, 30.1, 115, 170.1),
  xmax = c(-124.8, -57, 30, 115, 170, 180),
  col = c(1, 2, 3, 4, 5, 1), y = 1.1
)

library(ggplot2)
library(ggnewscale)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

ggplot(dummy, aes(x, y, col = as.factor(col))) +
  geom_point() +
  scale_color_manual(values = c(1, 2, 3, 4, 5), name = "colors") +
  new_scale_color() +
  geom_segment(data = segs, aes(x = xmin, y = 1.01, xend = xmax, yend = 1.01, col = area), 
               inherit.aes = FALSE, lwd = 2) +
  scale_color_viridis_d(name = "Area", option = "D")

Created on 2019-10-03 by the reprex package (v0.3.0)

Maschette commented 5 years ago

Yep, I downloaded it this morning (and again just then) and still get the same thing.

library(ggnewscale)
sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 17134)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
#> [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
#> [5] LC_TIME=English_Australia.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggnewscale_0.4.0.9000
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.2       knitr_1.25.1     magrittr_1.5     tidyselect_0.2.5
#>  [5] munsell_0.5.0    colorspace_1.4-1 R6_2.4.0         rlang_0.4.0     
#>  [9] stringr_1.4.0    highr_0.8        dplyr_0.8.3      tools_3.6.1     
#> [13] grid_3.6.1       gtable_0.3.0     xfun_0.9         htmltools_0.3.6 
#> [17] assertthat_0.2.1 yaml_2.2.0       lazyeval_0.2.2   digest_0.6.21   
#> [21] tibble_2.1.3     crayon_1.3.4     purrr_0.3.2      ggplot2_3.2.1   
#> [25] glue_1.3.1       evaluate_0.14    rmarkdown_1.15.2 stringi_1.4.3   
#> [29] compiler_3.6.1   pillar_1.4.2     scales_1.0.0     pkgconfig_2.0.3

Created on 2019-10-04 by the reprex package (v0.3.0)

Maschette commented 5 years ago

but interestingly if I run that code as a reprex again it works bu if i run it in my console it doesn't. So I am assuming it is caused by a conflict with a different package. I will tart fresh and try and work out which one it is.

eliocamp commented 5 years ago

Weird. Please let me know the results. I can't replicate the issue in the current version. I've just updated all my packages and I still cant.