R:错误:长度必须为1或与数据x相同 库(ggplot2) mydata

R:错误:长度必须为1或与数据x相同 库(ggplot2) mydata,r,ggplot2,R,Ggplot2,图表图像的x轴标记为“迭代”,但该信息不包括在“mydata”中的示例中。如果没有这些缺失的信息,很难帮助你 根据您的评论,您可能有兴趣设置x轴的限制。在这种情况下,您可以尝试使用类似的方法 library(ggplot2) mydata <- data.frame(variable = c(1, 1, 1, 2, 2, 2), value = c(1, 2, 3, 4, 5, 6)) p <- ggplot(data = mydata, a

图表图像的x轴标记为“迭代”,但该信息不包括在“mydata”中的示例中。如果没有这些缺失的信息,很难帮助你

根据您的评论,您可能有兴趣设置x轴的限制。在这种情况下,您可以尝试使用类似的方法

library(ggplot2)
mydata <- data.frame(variable = c(1, 1, 1, 2, 2, 2),
                     value = c(1, 2, 3, 4, 5, 6))
p <- ggplot(data = mydata, aes(x = 1:3, y = value, group = variable, color = variable)) + geom_line()
> p
Error: Aesthetics must be either length 1 or the same as the data (6): x

p2错误不清楚吗?尝试:
x=1:6
。我希望x轴的范围为1:3。
x=rep(1:3,2)
?此外,在aes()内设置x=1:3只需创建要在ggplot内使用的向量x=c(1,2,3)。这不会子集数据或设置x轴上的限制。
p2 <- ggplot(data = mydata, aes(x = variable, y = value)) + 
  geom_line() +
  xlim(1,3)
p2