r ggplot错误:美学长度匹配颜色&;形状

r ggplot错误:美学长度匹配颜色&;形状,r,ggplot2,R,Ggplot2,我正试图开发一个使用ggplot的函数,但我遇到了一个问题,即aes()函数和我将传递到geom_point()的参数,特别是color和shape 这里是一个非常简化的代码版本,在这个版本中,事情开始崩溃。了解代码试图实现的目标可能会有所帮助。假设我们有一个数据帧x,其中包含变量Dose和Response,并且有一个名为OutlierDetect(Dose,Response)的函数,它将x的索引返回到某个拟合曲线中,该索引似乎是一个异常值。目标是将这些索引绘制为与其他数据不同的形状 a <

我正试图开发一个使用ggplot的函数,但我遇到了一个问题,即
aes()
函数和我将传递到
geom_point()
的参数,特别是
color
shape

这里是一个非常简化的代码版本,在这个版本中,事情开始崩溃。了解代码试图实现的目标可能会有所帮助。假设我们有一个数据帧x,其中包含变量Dose和Response,并且有一个名为
OutlierDetect(Dose,Response)
的函数,它将x的索引返回到某个拟合曲线中,该索引似乎是一个异常值。目标是将这些索引绘制为与其他数据不同的形状

a <- ggplot(NULL, aes(x = Dose, y = Response)
shape.vec <- rep(19, nrow(x))
out.index <- OutlierDetect(Dose=x$Dose,Response=x$Response)
shape.vec[out.index] <- 17
a <- a + geom_point(data = x, size = 5, alpha = 0.8, aes(color = "group1"), shape = shape.vec)
这一块也是如此

a <- ggplot(NULL, aes(x = Dose, y = Response)
a <- a + geom_point(data = x, size = 5, alpha = 0.8, aes(color = "group1"), shape = 19) 
# can add more data frames y, z, ect, so long
# as the aes color parameter has a different name
a您的代码有
nrow(x$Dose)
当您试图从1D向量调用行数时,该代码不起作用。您可以将其更改为
nrow(x)
length(x$Dose)


a在寻求帮助时,您应该包括一个简单的示例输入和所需的输出,用于测试和验证可能的解决方案。但是
ggplot
aes()
中要使用的所有内容都来自数据源时效果最佳。您不必更改
x
,只需将包含绘图所需所有列的版本传递到
ggplot
。是否可以使用
dput()
显示数据?如果没有看到数据的样子,很难回答。请参见Hello MrFlick,为了让事情变得简单,
Dose@JustinLandis所有这些代码都应该编辑到您的问题中,而不是放在您的代码能够生成此图的有趣注释中,因为即使在我运行(您的更正版本)时,我仍然会收到相同的错误消息。可能是我拥有的r或ggplot2版本,我目前拥有r版本3.4.4和ggplot2版本2.2.1。您正在运行哪个版本的ggplot2?我正在运行R3.3.2和ggplot2.2.1.9000
a <- ggplot(NULL, aes(x = Dose, y = Response)
a <- a + geom_point(data = x, size = 5, alpha = 0.8, aes(color = "group1"), shape = 19) 
# can add more data frames y, z, ect, so long
# as the aes color parameter has a different name
x <- data.frame(Dose = c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10),
Response = seq(20))
a <- ggplot(NULL, aes(x = Dose, y = Response))
shape.vec <- rep(19, nrow(x))
out.index <- nrow(x) # lets say the OutlierDetect function always says the last index is an outlier
shape.vec[out.index] <- 17
a <- a + geom_point(data = x, size = 5, alpha = 0.8, aes(color = "group1"),     shape = shape.vec)
a <- ggplot(NULL, aes(x = Dose, y = Response))
shape.vec <- rep(19, nrow(x))
out.index <- nrow(x) # lets say the OutlierDetect function always says the last index is an outlier
shape.vec[out.index] <- 17
a <- a + geom_point(data = x, size = 5, alpha = 0.8, aes(color = "group1"),shape = shape.vec)