Why my scatter plot coding is working on some visuals and not to others.

I am using the code below for all my visuals, and for some works and for others not. I have checked the format anD everything

library(ggplot2)
dataset$Date<- as.Date(dataset$Date, format='%d/%m/%y' )
ggplot(dataset, aes(x=Date, y=MN)) +
geom_point(alpha=0.8, aes(color=ELB_Mn)) +
scale_colour_manual(values = c("white"="black", "tomato"="tomato", "lightskyblue"="lightskyblue", "mediumpurple"="purple", "orange")) +
geom_line(data=dataset, aes(x=Date, y=Mn_Target), color = "red") +
guides(color=FALSE)

Error: Discrete value supplied to continuous scale

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

How do I share data for a reprex?

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Hello,

Thank you for answering me back.

I know why I had an issue. It was because there is no data/value for target. I haven't use R before and this is the first time, but I need to use a condition saying If no target value, don't display scatter plot.

CODING

library(ggplot2)
dataset$Date<- as.Date(dataset$Date, format='%d/%m/%y' )
ggplot(dataset, aes(x=Date, y=MN)) +
geom_point(alpha=0.8, aes(color=ELB_Mn)) +
scale_colour_manual(values = c("white"="black", "tomato"="tomato", "lightskyblue"="lightskyblue", "mediumpurple"="purple", "orange")) +
geom_line(data=dataset, aes(x=Date, y=Mn_Target), color = "red") +
guides(color=FALSE)

DATA

test_ok <- function (x) {
  #adapted from shiny::isTruthy()
  if (inherits(x, "try-error")) 
    return(FALSE)
  if (!is.atomic(x)) 
    return(TRUE)
  if (is.null(x)) 
    return(FALSE)
  if (length(x) == 0) 
    return(FALSE)
  if (all(is.na(x))) 
    return(FALSE)
  if (is.character(x) && !any(nzchar(stats::na.omit(x)))) 
    return(FALSE)
  if (is.logical(x) && !any(stats::na.omit(x))) 
    return(FALSE)
  return(TRUE)
}


if(test_ok(dataset$Mn_Target)){
  # do your code here , given that its ok to do so
  print("im fine")
}

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.