我正在尝试绘制一组线条,并为图例中的这些线条指定一个标签。这是我正在使用的数据(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"曲线在情节上分别可见,但在图例中我只想要一个"假模型"在一条灰线旁边进入。有关如何做到这一点的任何想法?
答案 0 :(得分:1)
我的dput
出了问题。它似乎缺少rownames,使其成为无效的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")
最简单的方法是创建一个可用于着色的新因子。例如
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))
获取