简体   繁体   English

R 错误:离散值提供给连续刻度

[英]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 堆栈溢出解决方案 1

Stack Overflow Solution 2 堆栈溢出解决方案 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.您的最后一个 geom_line 使用yExpected.Value的逻辑(又名布尔)值,而其他y变量是数字。 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.您可以将该行替换为y = as.numeric(Expected.Value) ,这样 TRUE 将变为 1 和 FALSE 0,可以绘制在连续轴上。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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