R ggplot中的线组图例

R ggplot中的线组图例,r,ggplot2,legend,R,Ggplot2,Legend,我试图绘制一组线,并在图例中为这些线指定一个标签。下面是我正在使用的数据(df2)的一个示例 structure(list( true = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,

我试图绘制一组线,并在图例中为这些线指定一个标签。下面是我正在使用的数据(df2)的一个示例

structure(list(
true = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
.Label = c("model1", "model2"), class = "factor"), 
test = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), 
.Label = c("false.model1", "false.model2", "false.model3", "true model"), class = "factor"), 
est.interval = c(0, 5, 5, 7, 7, 10, 10, 11, 11, 0, 2, 2, 3, 3, 0, 2, 2, 4, 4, 0, 2, 2, 3, 3, 4, 4, 0, 7, 7, 10, 10, 13, 13, 0, 4, 4, 5, 5, 0, 5, 5, 6, 6, 7, 7, 9, 9, 0, 2, 2, 3, 3, 4, 4), 
sens = c(1, 1, 0.75, 0.75, 0.5, 0.5, 0.25, 0.25, 0, 1, 1, 0.25, 0.25, 0, 1, 1, 0.5, 0.5, 0, 1, 1, 0.5, 0.5, 0.25, 0.25, 0, 1, 1, 0.75, 0.75, 0.25, 0.25, 0, 1, 1, 0.5, 0.5, 0, 1, 1, 0.75, 0.75, 0.5, 0.5, 0.25, 0.25, 0, 1, 1, 0.5, 0.5, 0.25, 0.25, 0)), 
.Names = c("true", "test", "est.interval", "sens"), 
class = "data.frame")
这里我用ggplot绘制曲线

ggplot(df2, aes(x=est.interval, y=sens, color=test)) +
    ylim(0,1) +
    geom_line(size = 0.6) +
    facet_wrap(~true, nrow=1) +
    scale_colour_manual(values=c(rep('#CCCCCC', 3), "#000000"), name="Estimation Model") + 
    guides(color=guide_legend(keywidth = 3, keyheight = 1))

我想让三条灰色的“false.model”曲线分别在绘图上可见,但在图例中,我只希望在一条灰线旁边有一个“false models”条目。有什么想法吗?

我对您的
dput
有问题。似乎缺少行名,这使其成为无效的data.frame。这里有一个修正的垃圾场

df2 <- structure(list(true = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("model1", 
"model2"), class = "factor"), test = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L), .Label = c("false.model1", "false.model2", "false.model3", 
"true model"), class = "factor"), est.interval = c(0, 5, 5, 7, 
7, 10, 10, 11, 11, 0, 2, 2, 3, 3, 0, 2, 2, 4, 4, 0, 2, 2, 3, 
3, 4, 4, 0, 7, 7, 10, 10, 13, 13, 0, 4, 4, 5, 5, 0, 5, 5, 6, 
6, 7, 7, 9, 9, 0, 2, 2, 3, 3, 4, 4), sens = c(1, 1, 0.75, 0.75, 
0.5, 0.5, 0.25, 0.25, 0, 1, 1, 0.25, 0.25, 0, 1, 1, 0.5, 0.5, 
0, 1, 1, 0.5, 0.5, 0.25, 0.25, 0, 1, 1, 0.75, 0.75, 0.25, 0.25, 
0, 1, 1, 0.5, 0.5, 0, 1, 1, 0.75, 0.75, 0.5, 0.5, 0.25, 0.25, 
0, 1, 1, 0.5, 0.5, 0.25, 0.25, 0)), .Names = c("true", "test", 
"est.interval", "sens"), row.names = c(NA, -54L), class = "data.frame")
得到


您的示例缺少变量,如
true.models.testCase
,以便可以运行,加上数据中的空格。值使得通过复制/粘贴将其导入R非常困难。最好包含示例数据的
dput()
。最好给我们
df2
,这就是你想要绘制的。如何创建这些数据实际上是无关紧要的。如果没有代码生成您所看到的绘图,就很难提供建议。谢谢。我已经提供了对代码的建议编辑。这更有帮助。谢谢。这太完美了。非常感谢!
df2$testcol<-factor(ifelse(df2$test=="true model",1,2), 
   levels=1:2,
   labels=c("True Model","False Model"))
ggplot(df2, aes(x=est.interval, y=sens, group=test, colour=testcol)) +
    ylim(0,1) +
    geom_line(size = 0.6) +
    facet_wrap(~true, nrow=1) +
    scale_colour_manual(values=c("True Model"='#CC0000', 
        "False Model"="#999999"), name="Estimation Model") + 
    guides(color=guide_legend(keywidth = 3, keyheight = 1))