hrbrmstr / ggalt

:earth_americas: Extra Coordinate Systems, Geoms, Statistical Transformations & Scales for 'ggplot2'
https://cran.r-project.org/web/packages/ggalt/vignettes/ggalt_examples.html
Other
654 stars 99 forks source link

Bug in geom_xspline #64

Open mtorchiano opened 4 years ago

mtorchiano commented 4 years ago

There is a bug in the implementation of geom_xspline that produce a jittered line.

Looks like the result is rendered similarly to a geom_line rather than a geom_path.

library(ggplot2)
library(ggalt)

d.test = data.frame(GDP = c(675L, 730L, 745L, 780L, 775L), 
                      GINI = c(58.1, 59.5, 61, 63, 60.5))

p <- ggplot(d.test,aes(x=GDP,y=GINI))+geom_xspline()+geom_point(color="red")
p

image

The interpolation halfway 3rd and 4th points start going up and down between expected line and the 5th (and last) point.

As a comparison the equivalent interpolation line from xspline is correct:

par(mar=c(3,3,1,1))
plot(0,xlim=c(675,780),ylim=c(58,63))
xspline(d.test$GDP,d.test$GINI,shape=-0.25)
xs = xspline(d.test$GDP,d.test$GINI,shape=-0.25,draw=FALSE)

image

As a further check we can superimpose the xspline interpolation on the geom_xspline to see the result:

p + geom_path(aes(x=x,y=y),data=data.frame(xs), color="red", alpha=0.5, size=2)

image

Session information:

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggalt_0.4.0   ggplot2_3.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0         Rttf2pt1_1.3.7     rstudioapi_0.9.0   bindr_0.1.1       
 [5] magrittr_1.5       maps_3.3.0         MASS_7.3-51.1      tidyselect_0.2.5  
 [9] munsell_0.5.0      colorspace_1.4-0   R6_2.3.0           rlang_0.3.1       
[13] plyr_1.8.4         dplyr_0.7.8        tools_3.5.1        grid_3.5.1        
[17] gtable_0.2.0       ash_1.0-15         KernSmooth_2.23-15 extrafontdb_1.0   
[21] withr_2.1.2        proj4_1.0-8.1      lazyeval_0.2.1     assertthat_0.2.0  
[25] tibble_2.0.1       crayon_1.3.4       bindrcpp_0.2.2     RColorBrewer_1.1-2
[29] purrr_0.3.0        glue_1.3.0         labeling_0.3       compiler_3.5.1    
[33] pillar_1.3.1       scales_1.0.0       extrafont_0.17     pkgconfig_2.0.2  
mtorchiano commented 4 years ago

Just realized that there is a simple workaround.

The problem is very likely due to the use of line instead of path as the basic geom.

The workaround consists in forcing explicitly the use of geom_path by using stat_xspline instead of geom_xspline:

ggplot(d.test,aes(x=GDP,y=GINI))+
  stat_xspline(geom="path")+
  geom_point(color="red")

image