Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot绘制颜色取决于类别的时间序列_R_Ggplot2 - Fatal编程技术网

R 使用ggplot绘制颜色取决于类别的时间序列

R 使用ggplot绘制颜色取决于类别的时间序列,r,ggplot2,R,Ggplot2,考虑以下最低限度的工作示例: library(ggplot2) x <- c(1,2,3,4,5,6) y <- c(3,2,5,1,3,1) data <- data.frame(x,y) pClass <- c(0,1,1,2,2,0) plottedGraph <- ggplot(data, aes(x = x, y = y, colour = factor(pClass))) + geom_line() print(plottedGraph) 库(gg

考虑以下最低限度的工作示例:

library(ggplot2) 
x <- c(1,2,3,4,5,6)
y <- c(3,2,5,1,3,1)
data <- data.frame(x,y)
pClass <- c(0,1,1,2,2,0)

plottedGraph <- ggplot(data, aes(x = x, y = y, colour = factor(pClass))) + geom_line()
print(plottedGraph)
库(ggplot2)

x您应该在
aes()
中使用
group=1
来告诉
ggplot
不同的颜色实际上属于同一行(即组)

ggplot(data, aes(x = x, y = y, colour = factor(pClass), group = 1)) + 
  geom_line()