0

The plotting code below gives Error: Discrete value supplied to continuous scale

I already tried this solution mentioned by stack overflow community but none of them is working. I don't know what wrong with this code.

Stack Overflow Solution 1

Stack Overflow Solution 2

What's wrong with this code?

#Library Download
library(ggplot2)
library(dplyr)
library(ggthemes)

#Setting Working Directory
setwd("")
getwd()

#Main Code
data <- read.csv("Test2.csv",header=TRUE)
str(data)
xd <-factor(data$SampleID)
g <- ggplot()
g <- g + geom_bar(data= data,aes(x = xd, y = Average.of.Result,group=Element,color=Element),stat='identity',
  position="dodge",
  na.rm = FALSE,
  show.legend = NA,
  fill = rgb(0, 0, 0.8)
)+  
  theme_minimal(base_size = 12, base_family ="Segoe UI")+
  geom_line(data=data,aes(x=xd,y=X10xDL,group=Element), size=1.25,color="blue",linetype = "dashed")+
  geom_line(data=data,aes(x=xd,y=Expected.Value,group=Element), size=1.25,color="red",linetype = "dashed") +
  theme_minimal(base_size = 12, base_family ="Segoe UI")
g

Dataset Attached here

Any help???

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • What is the output of `str(data)`? Are the columns that you are using for `y = ` all coming in as numeric, ie `dbl` or `int`? – Jon Spring Jan 21 '22 at 05:32
  • SampleID: Int, Element: chr, Expected.Value: logi, X10xDL: int, Average.of.Result:num. This is the result I am getting when I run str(data). I also attached data if you wanted to see actual data. – user17538713 Jan 21 '22 at 05:43

1 Answers1

1

Your last geom_line uses the logical (aka boolean) values of Expected.Value for y, while the other y variables are numeric. You could replace it with y = as.numeric(Expected.Value) for that line, so that TRUE will become 1 and FALSE 0, which can be plotted on a continuous axis.

ggplot(data = mutate(mtcars, am_logi = am == 1)) +
  geom_line(aes(x = wt, y = mpg)) +
  # geom_point(aes(x = wt, y = am_logi)) +          # ERROR
  geom_point(aes(x = wt, y = as.numeric(am_logi)))  # NO ERROR
Jon Spring
  • 55,165
  • 4
  • 35
  • 53