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()