Error: Discrete value supplied to a continuous scale

118 views Asked by At

I am writing a code to make custom bland-altman plots. I saw that in one of my plots there was some proportional bias. Therefore I needed to transform my data. I used a transformation of difference/means*100 for my y axis. I tried to use the same custom bland-altman plot function that I wrote for the other variables. However, first I got the error that some variables were non-numeric, hence all the as.numeric() functions... This solved the first problem. My problem now is that R gives me the error:

Error in train_continuous(): ! Discrete value supplied to a continuous scale Run rlang::last_trace() to see where the error occurred.

I do now uncerstand what is not continuous about my data. Moreover, I set the x_scale and geom_segements and geom_ribbons in a way that they all run from the very start to the very end of my grid. And I wanted my x axis to be rounded on whole numbers of 5.

There are no NaNs or what so ever in my data. This is my data (PVD_table):

PVD_AVA PVD_CCTools

1 16.19 6.5602 2 10.76 8.8146 3 17.22 12.0546 4 13.42 9.1943 5 7.44 6.8718 6 9.59 7.6431 7 18.50 11.9803 8 14.19 10.9563 9 12.99 9.7333 10 13.18 11.5637 ... 163 18.65 2.6630

My ggplot code:

  #### make PVD as percentage ####
    PVD_table <- AVA_CCTools[, c("PVD_small", "PVD_small_CCTools")]
    names(PVD_table) <- c("PVD_AVA", "PVD_CCTools")
    PVD_dif_perc <- (PVD_table[,1] - PVD_table[,2])/((PVD_table[,1]+PVD_table[,2])/2)*100

    #### put means and differences in 2 columns in df
    stats.data <- data.frame(Mean = numeric(length(PVD_table[,1])),Dif = numeric(length(PVD_table[,1])))
    stats.data$Mean[1:length(PVD_table[,1])] <- (PVD_table[,1]+PVD_table[,2])/2
    stats.data$Dif[1:length(stats$differences)] <- PVD_dif_perc

    stats.data$Mean <- as.numeric(stats.data$Mean)
    stats.data$Dif <- as.numeric(stats.data$Dif)

    # make linear model to fit the data
    lin_model_prop <- lm(Dif ~ Mean, data = stats.data)

    summary(lin_model_prop)


    #### make variables for mean bias, upper/lower LoA and their CIs (all in percentage differences)
    se <-               as.numeric(sd(stats.data$Dif)/(sqrt(length(stats.data$Dif))))
    MoE_bias <-         as.numeric(1.96*se)
    MoE_LoA <-          as.numeric(1.96*sqrt(((3 * (sd(stats.data$Dif)^2)) / length(stats.data$Dif))))
    bias <-             as.numeric(mean(stats.data$Dif))
    bias_upperCI <-     as.numeric(bias + MoE_bias)
    bias_lowerCI <-     as.numeric(bias - MoE_bias)
    upperLOA <-         as.numeric(bias + 1.96*sd(stats.data$Dif))
    upperLOA_upperCI <- as.numeric(upperLOA + MoE_LoA)
    upperLOA_lowerCI <- as.numeric(upperLOA - MoE_LoA)
    lowerLOA <-         as.numeric(bias - 1.96*sd(stats.data$Dif))
    lowerLOA_upperCI <- as.numeric(lowerLOA + MoE_LoA)
    lowerLOA_lowerCI <- as.numeric(lowerLOA - MoE_LoA)
    a <- as.numeric(coef(lin_model_prop)[[2]])
    b <- as.numeric(coef(lin_model_prop)[[1]])

       #### plot the proportional bland_altman plot ####
    # round the means at whole numbers of 5 to make x-axis scale
    mult <- 5
    rounded_means <- mult*round(stats.data$Mean[]/mult)
    rounded_means <- as.numeric(rounded_means)

    # set limits for x-axis
    x_lim_low <- min(rounded_means)-5
    x_lim_up <- max(rounded_means)+5

    PVD_prop_bl_plot<- ggplot(stats.data, aes(x = as.numeric(Mean), y = as.numeric(Dif))) +
      geom_point(size = 0.75) +
      geom_abline(intercept = b, slope = a, linetype = "dotted", color = "black") +  # Add regression line
      scale_x_continuous(limits = c(min(rounded_means), max(rounded_means)), expand = c(0, 0)) +
      labs(
        x = "Mean",
        y = "AVA 3.2- CCTools / Mean * 100"
      ) +                   #line at mean bias
      geom_segment(x = x_lim_low, xend = x_lim_up, y = bias, yend = bias, linetype = "longdash", color = "black" #BIAS
      )+    #line at lower limit of agreement
      geom_segment(x = x_lim_low, xend = x_lim_up, y = lowerLOA, yend = lowerLOA, linetype = "twodash", color = "black" #upper LoA with CI
      )+                    #line at upper Limit of agreement
      geom_segment(x = x_lim_low, xend = x_lim_up, y = upperLOA, yend =upperLOA, linetype = "twodash", color = "black" #lower LoA with CI
      )+                    # black line at y = 0
      geom_segment(x = x_lim_low, xend = x_lim_up, y = 0, yend = 0, linetype = "solid", color = "black"
      )+                    # regression line
      geom_segment(x = x_lim_low, xend = x_lim_up, y = (x_lim_low*a + b), yend = (x_lim_up*a + b), linetype = "dotdash", color = "black"
      )+      # shade the area of the 95% CI at lower LoA
      geom_ribbon(
        aes(x = rounded_means, xmin = x_lim_low, xmax = x_lim_up, ymin = lowerLOA_lowerCI, ymax = lowerLOA_upperCI),
        fill = "grey", alpha = 0.35
      ) +                   # shade the area of the 95% CI of the Bias
      geom_ribbon(
        aes(x = rounded_means, ymin = bias_lowerCI, ymax = bias_upperCI),
        fill = "grey", alpha = 0.40
      ) +                   # shade the area of the 95% CI at upper LoA
      geom_ribbon(
        aes(x = rounded_means, ymin = upperLOA_lowerCI, ymax = upperLOA_upperCI),
        fill = "grey", alpha = 0.35
      )+
      theme(
        plot.title = element_text(color = "1", size = 14, face = "bold", hjust = 0.5),
        plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5),
        plot.caption = element_text(face = "italic", hjust = 0),
        axis.title.x = element_text(color = "black", size = 8),
        axis.title.y = element_text(color = "black", size = 8),
        legend.position = "right",
        aspect.ratio = 0.75
      )

I tried excuting these lines of code by themself and sometimes for a mysterious reason I do not get an error, but most of the time I get and error. I do not understand what causes this problem and why it sometimes works and sometimes doesn't.

0

There are 0 answers