当有几组情节时,我正在与ggplot中的传奇顺序挣扎。这是一个例子:
library(ggplot2)
library(data.table)
data <- data.table(time = rep(1:50,4),dampingtime = rep(c(4,8,12,16),each = 50),
excitation = rep(c(rep(0,10),rep(1,10),rep(0,30)),4))
data[,signal := time + .GRP, by = dampingtime]
data[,dampingtime := as.factor(dampingtime)]
我有
> levels(data$dampingtime)
[1] "4" "8" "12" "16"
我相应地对ggplot legends - change labels, order and title做了,当我做
时,我确实得到了正确的传奇顺序ggplot(data = data) +
geom_point(aes(time,signal,color = dampingtime))
我的问题是我想添加一个带有图例条目的激励列,所以我这样做:
ggplot(data = data) +
geom_line(aes(time,excitation,color = "excitation")) +
geom_point(aes(time,signal,color = dampingtime))
在Greor评论之后,我将激励添加到因子水平
data[,dampingtime := factor(dampingtime,levels = c(levels(dampingtime),"excitation"))]
levels(data$dampingtime)
[1] "4" "8" "12" "16" "excitation"
但我得到完全相同的问题。我该怎么做才能得到正确的订单?
答案 0 :(得分:1)
您可以指定> df$id <- which(df[,-c(1,2)]==1, arr.ind = TRUE)[, 1]
> df
var1 var2 id1 id2 id3 id
obs1 . . 0 1 0 2
obs2 . . 1 0 0 1
obs3 . . 0 0 1 3
scale_colour_discrete