1
votes

I am trying to plot a group of lines and assign one label to these lines in the legend. Here's an example of the data (df2) that I am using.

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

And here I plot the curves using 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))

I'd like to have the three grey "false.model" curves separately visible on the plot, but in the legend I just want one "false models" entry beside one grey line. Any thoughts on how to do this?

1
You example is missing variables like true.models.testCase in order to be runnable, plus the spaces in your data.values makes this very difficult to import into R via copy/paste. It would be better to include a dput() of your sample data. Even better just give us df2 and that's what you want to plot. How you create that data is really irrelevant. Without having the code to generate the plot as you see it, it's difficult to offer suggestions.MrFlick
Thank you. I've provided the suggested edits to the code.user2220537
This is much more helpful. Thanks.MrFlick
This is perfect. Thanks so much!user2220537

1 Answers

1
votes

I had a problem with your dput. It seemed to be missing rownames which made it an invalid data.frame. Here a corrected dump

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

The easiest thing to do would be to create a new factor that you can use for the coloring. For example

df2$testcol<-factor(ifelse(df2$test=="true model",1,2), 
   levels=1:2,
   labels=c("True Model","False Model"))

Now all the false models will share a common value. Then you can do

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

to get

enter image description here