Home > Software design >  Error: Discrete value supplied to continuous scale - stat_ma_line
Error: Discrete value supplied to continuous scale - stat_ma_line

Time:01-31

This is the data frame and ggplot code I am using:

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 = "interval", range.x = "interval", 
           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', label = scientific_10)  
 scale_y_continuous(trans='log10')  
 scale_color_manual(values = c("#0072B2", "#000000","#E7B800","#CC79A7"))   
 labs(colour = "ROS (m/s)")  
 theme_bw()

When I plot using the variable "y = wind250_1", the code work with no problem. But when I try to use the variable "y = lee250_1" it gives the "Error: Discrete value supplied to continuous scale". The variable is numeric (checked the class) and here are a few things I tried it didn't work: use y= as.numeric(lee250_1) in ggplot code, change the name of the variables, run ggplot code without the lines scale_x_continuous(), scale_y_continuous(), and scale_color_manual().

The error I am getting is probably related to the stat_ma_line() because I tried to plot using geom_line() and it did work but I need to use stat_ma_line. So any help on how to solve this error is very much appreciated!!

CodePudding user response:

You probably have too less points per group (probably you need more than 7 points per group), that's why you get an error. I added some fake data and now it works:

ROS<- c(0.03, 0.03, 0.03, 0.03, 0.07, 0.07, 0.07, 0.07, 0.07, 0.03, 0.03, 0.03, 0.03, 0.07, 0.07, 0.07, 0.07, 0.07)
rey <- c(31500,66375,131250,241500,31500,66375,131250,241500,131250, 31600,66475,131350,241600,31300,66575,132250,242500,283425)
lee250_1 <- c(79.84,125.56,93.34,94.42,33.78,49.6,24.2,32.95, 79.94,122.54,92.34,91.42,32.78,43.6,31.95,44.9,32.32,22.2) 

library(ggplot2)
library(ggpmisc)
df<- data.frame(ROS,rey,lee250_1) 
ggplot(df, aes(rey, lee250_1))  
  geom_point(aes(colour = factor(ROS)))  
  stat_ma_line(method = "RMA",
               range.y = "interval", range.x = "interval", fill = 'yellow', linewidth = 1)  
  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()

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

CodePudding user response:

It looks like that the model fit returns NAs for RMA and ggplot would get problems with that. Please see the answer in more details here https://github.com/aphalo/ggpmisc/issues/36

  • Related