“Error: Discrete value supplied to continuous scale” but I'm using numeric variables

I'm writing a code in R to get a plot with ggplot. In doing so, I'm running this function:

    BasicScatterPlotClassWrapper_b<- function(sample,pivot.var,pivot.title,lab.size,pivot.dot.size,pivot.ylab,pivot.xlab,font.size.x,font.size.y,pivot.lab,variable_y,variable_x) {
   
      sample_w <- sample %>%
      select(pivot.var) %>%
      drop_na %>%
      mutate(y.calc = variable_y)  %>%
      mutate(x.calc = variable_x)

     
    
      ## GGplot 
      gg <- ggplot(sample_w, aes(x.calc,y.calc)) +
      ggtitle(pivot.title) + 
      geom_point(aes(colour= ColoursXXX$blue)) +
      scale_color_manual(values= ColoursXXX$blue) +
      geom_text_repel(aes(x.calc,y.calc, label = ""), size = pivot.lab ,fontface = "bold") +
      xlab(pivot.xlab) +
      ylab(pivot.ylab) +
      scale_y_continuous(labels = scales::percent) +
      scale_x_continuous(labels = scales::percent) +
      theme(axis.title.y = element_text(face = "bold", size = 9, color="black"),
            axis.title.x = element_text(face = "bold", size = 9, color="black"),
            axis.text.x=element_text(size = font.size.x, face = "bold",hjust= 0.5),
            axis.text.y=element_text(size = font.size.y, face = "bold",hjust= 0.5),
            legend.position="none", 
            panel.background = element_rect(fill = "transparent", colour = NA),
            axis.line = element_line(size = 0.2, colour = "black"))

      return(gg)
      
      
    }

If I substitute the inputs from the function with their actual value, I can get the plot. If I call the function with a specific command to export the plot in PPT, I get this error: "Error: Discrete value supplied to continuous scale"

The inputs are the following ones:

openChartPPT(BasicScatterPlotClassWrapper_b(
                                           sample=DF, 
                                           pivot.var= c("COV.proj.ratio", "COV.curr.ratio"),
                                           pivot.title="",
                                           lab.size=3,
                                           pivot.dot.size=2,
                                           pivot.ylab="bla bla",
                                           pivot.xlab="bla bla bla", 
                                           font.size.x=7, 
                                           font.size.y=7,
                                           pivot.lab=2,
                                           variable_y = "COV.proj.ratio",
                                           variable_x = "COV.curr.ratio"), width = 8, height = 6)

The two variables COV.proj.ratio and COV.curr.ratio are numeric.

Do you have any clue about what the issue could be?

Many thanks, R

Well you need to convert the strings to column identifiers for one thing. I think the double bang does it.

sample_w <- sample %>%
      select(!!pivot.var) %>%
      drop_na %>%
      mutate(y.calc = !!variable_y)  %>%
      mutate(x.calc = !!variable_x)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Many thanks, I used !!sym() and it worked smoothly

1 Like