1
我有一个数据集,其中包含200个不同的组,这可能需要一个介于0和200之间。我想为每个组绘制一条线,因此总共200行,并有图例是“数字”。我知道如何用一个因素来做到这一点,但不能让它起作用。不是最好的例子:使用ggplot绘制几条“数字”线
library(tidyverse)
df <- data.frame(Day = 1:100)
df <- df %>% mutate(A = Day + runif(100,1,400) + rnorm(100,3,400) + 2500,
B = Day + rnorm(100,2,900) + -5000 ,
C = Day + runif(100,1,50) + rnorm(100,1,1000) -500,
D = (A+B+C)/5 - rnorm(100, 3,450) - 2500)
df <- gather(df, "Key", "Value", -Day)
df$Key1 <- apply(df, 1, function(x) which(LETTERS == x[2]))
ggplot(df, aes(Day, Value, col = Key)) + geom_line() # I would to keep 4 lines, but would like have the following legend
ggplot(df, aes(Day, Value, col = Key1)) + geom_line() # Not correct lines
ggplot(df, aes(Day, Value)) + geom_line(aes(col = Key1)) # Not correct lines
可能是重复的,但我不能找到答案,你猜有什么小是不正确。
谢谢。 200行来自我的“真实数据”。 – MLEN