aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

stat_ma_line - Error: Discrete value supplied to continuous scale #36

Closed rakelrpf closed 1 year ago

rakelrpf commented 1 year ago

There are two problems happening when using stat_ma_line():

1) when I use the range.y and range.x = "interval", the ggplot code works fine when I use the variable "y = wind250_1". However when I use the variable "y= lee250_1" I get the error: Discrete value supplied to continuous scale.

2) if I change the range.y and range.x = "relative". The code runs fine for both y variables. However, when I exclude the lines " "scale_x_continuous(trans='log10')" and scale_y_continuous(trans='log10') and use "y=lee250_1", it gives me the error: Discrete value supplied to continuous scale.

The variable is numeric, I even used the as.numeric() in the code. The values per group could be a problem, as suggested in this post - https://stackoverflow.com/questions/75242355/error-discrete-value-supplied-to-continuous-scale-stat-ma-line. However it does not make sense for me considering that the code works with the variable "wind250_1" and not with "lee250_1".

ROS<- c(0.03, 0.03, 0.03, 0.03, 0.07, 0.07, 0.07, 0.07, 0.07, 0.1, 0.1, 0.1)
wind<- c(0.84, 1.77, 3.5, 6.44, 0.84, 1.77, 3.5, 6.44, 7.55, 0.84, 1.77, 3.5)
rey <- c(31500,66375,131250,241500,31500,66375,131250,241500,283125,31500,66375,131250)
wind250_1 <- c(69.4,69.4,1,1,31.08,37.07,1,1,1,22.8,19.45,1) 
lee250_1 <- c(79.84,125.56,93.34,94.42,33.78,49.6,38.95,40.9,39.32,24.2,32.95,27.46) 

df<- data.frame(ROS,wind,rey,wind250_1,lee250_1) 

ggplot() +
  stat_ma_line(df, mapping=aes(rey, lee250_1),  method="RMA",
               range.y = "relative", range.x = "relative", 
               linewidth = 1,fill = "yellow") +
  geom_point(df, mapping = aes(x = rey, lee250_1, colour=factor(ROS)), 
             size=3)+ 
  xlab("Re") + ylab((expression(paste(tau~"windward"))))+
  #scale_x_continuous(trans='log10') +
  #scale_y_continuous(trans='log10') +
  scale_color_manual(values = c("#0072B2", "#000000","#E7B800","#CC79A7")) + 
  labs(colour = "ROS (m/s)") +
  theme_bw()
#> Error: Discrete value supplied to continuous scale"
aphalo commented 1 year ago

@rakelrpf Thanks for reporting the problem!

I need to add error handling code and a better error message, but the root of the problem seems to be that the model fit returns NAs for "RMA". I am not sure why.

I copy here the code example for the model fit function call equivalent to that wrapped by stat_ma_line()

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

ROS<- c(0.03, 0.03, 0.03, 0.03, 0.07, 0.07, 0.07, 0.07, 0.07, 0.1, 0.1, 0.1)
wind<- c(0.84, 1.77, 3.5, 6.44, 0.84, 1.77, 3.5, 6.44, 7.55, 0.84, 1.77, 3.5)
rey <- c(31500,66375,131250,241500,31500,66375,131250,241500,283125,31500,66375,131250)
wind250_1 <- c(69.4,69.4,1,1,31.08,37.07,1,1,1,22.8,19.45,1) 
lee250_1 <- c(79.84,125.56,93.34,94.42,33.78,49.6,38.95,40.9,39.32,24.2,32.95,27.46) 

df<- data.frame(ROS,wind,rey,wind250_1,lee250_1) 

nrow(df)
#> [1] 12
sapply(df, class)
#>       ROS      wind       rey wind250_1  lee250_1 
#> "numeric" "numeric" "numeric" "numeric" "numeric"

lmodel2::lmodel2(lee250_1 ~ rey, data = df, range.y="relative", range.x="relative", nperm=0)
#> No permutation test will be performed
#> 
#> Model II regression
#> 
#> Call: lmodel2::lmodel2(formula = lee250_1 ~ rey, data = df, range.y =
#> "relative", range.x = "relative", nperm = 0)
#> 
#> n = 12   r = 0.01628888   r-square = 0.0002653275 
#> Parametric P-values:   2-tailed = 0.9599283    1-tailed = 0.4799641 
#> Angle between the two OLS regression lines = 1.291638 degrees
#> 
#> Regression results
#>   Method Intercept        Slope Angle (degrees) P-perm (1-tailed)
#> 1    OLS  55.96853 5.983967e-06    0.0003428561                NA
#> 2     MA  55.96853 5.983966e-06    0.0003428560                NA
#> 3    SMA  12.19621 3.673653e-04    0.0210484789                NA
#> 4    RMA  54.38782 1.903417e-05    0.0010905777                NA
#> 
#> Confidence intervals
#>   Method 2.5%-Intercept 97.5%-Intercept    2.5%-Slope  97.5%-Slope
#> 1    OLS       17.53142        94.40564 -0.0002528270 0.0002647950
#> 2     MA       24.62004        87.31701 -0.0002528271 0.0002647950
#> 3    SMA      -29.08603        33.61093  0.0001905668 0.0007081888
#> 4    RMA             NA              NA            NA           NA
#> 
#> Eigenvalues: 8056585228 1087.006 
#> 
#> H statistic used for computing C.I. of MA: 6.698315e-08

Created on 2023-01-27 with reprex v2.0.2

aphalo commented 1 year ago

To do for next release: change code of stat_sma_line() and stat_sma_eq() to test for NAs before pasing the result of the fit to geoms. Trigger informative warning message.

aphalo commented 1 year ago

@rakelrpf With some datasets lmodel2() fails to compute the confidence band and returns NA values. Somewhere downstrean 'ggplot2' chokes with these NA values. This is now fixed in the version here in GitHub, but will take some time before I submit it to CRAN.

In the new version, your example, instead of failing, now plots the RMA line without a confidence band with a warning.

Thanks very much for reporting this problem!

aphalo commented 1 year ago

@rakelrpf Please, reopen the issue if the problem persists.