简体   繁体   中英

R Error: Discrete value supplied to continuous scale

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???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM