Reputation: 191
I am trying to manually add legend to ggplot2. I have tried multiple ways as suggested in previous posts like using scale_colour_manual or using aes(color) in the geom function. But it doesn't work for me.
model_file =
structure(list(thresholds = c(1.86, 0.86, 0.69, 0.51, 0.41, 0.37,
0.36, 0.33, 0.32, 0.31, 0.27, 0.26, 0.25, 0.24, 0.23, 0.18, 0.16,
0.15, 0.14, 0.12, 0.11, 0.1, 0.09, 0.08, 0.07, 0.06, 0.05, 0.04,
0.03, 0.02, 0.01, 0), true_positive_rate = c(0, 0.0769230769230769,
0.153846153846154, 0.153846153846154, 0.230769230769231, 0.230769230769231,
0.230769230769231, 0.230769230769231, 0.307692307692308, 0.307692307692308,
0.307692307692308, 0.307692307692308, 0.307692307692308, 0.307692307692308,
0.307692307692308, 0.307692307692308, 0.307692307692308, 0.307692307692308,
0.384615384615385, 0.461538461538462, 0.461538461538462, 0.538461538461538,
0.538461538461538, 0.538461538461538, 0.538461538461538, 0.538461538461538,
0.538461538461538, 0.538461538461538, 0.538461538461538, 0.538461538461538,
0.538461538461538, 1), false_positive_rate = c(0, 0, 0, 0.00112834978843441,
0.00112834978843441, 0.00169252468265162, 0.00225669957686883,
0.00282087447108604, 0.00310296191819464, 0.00366713681241185,
0.00592383638928068, 0.00648801128349788, 0.0078984485190409,
0.00818053596614951, 0.00902679830747532, 0.0112834978843441,
0.0129760225669958, 0.0132581100141044, 0.014950634696756, 0.0155148095909732,
0.0166431593794076, 0.017771509167842, 0.0183356840620592, 0.0194640338504937,
0.0197461212976023, 0.0214386459802539, 0.0222849083215797, 0.0228490832157969,
0.0236953455571227, 0.0242595204513399, 0.0256699576868829, 1
)), class = "data.frame", row.names = c(NA, -32L))
patient_file =
structure(list(true_positive_rate = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.0833333333333333, 0.0833333333333333,
0.0833333333333333, 0.0833333333333333, 0.0833333333333333, 0.0833333333333333,
0.0833333333333333, 0.0833333333333333, 0.0833333333333333, 0.0833333333333333,
0.0833333333333333, 0.0833333333333333, 0.0833333333333333, 0.0833333333333333,
0.0833333333333333, 0.0833333333333333, 0.0833333333333333, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667,
0.166666666666667, 0.166666666666667, 0.166666666666667, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.272727272727273, 0.272727272727273, 0.272727272727273, 0.272727272727273,
0.272727272727273, 0.272727272727273, 0.272727272727273, 0.272727272727273,
0.272727272727273, 0.272727272727273, 1), false_positive_rate = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.000341646737273659, 0.000341880341880342, 0.000342114266164899,
0.000342348510783978, 0.000342348510783978, 0.000342817963661296,
0.000342817963661296, 0.000342935528120713, 0.000342935528120713,
0.000343053173241852, 0.000343053173241852, 0.000343053173241852,
0.000343170899107756, 0.000343406593406593, 0.000343406593406593,
0.000343406593406593, 0.000343642611683849, 0.000343760742523204,
0.000343878954607978, 0.000343997248022016, 0.000344115622849277,
0.000344115622849277, 0.000344115622849277, 0.000344115622849277,
0.000344115622849277, 0.000344234079173838, 0.00034435261707989,
0.00034447123665174, 0.00034447123665174, 0.000344827586206897,
0.00034494653328734, 0.00034494653328734, 0.00034494653328734,
0.00034494653328734, 0.000345065562456867, 0.000345065562456867,
0.000345065562456867, 0.000345065562456867, 0.000345065562456867,
0.000345065562456867, 0.000345065562456867, 0.000345065562456867,
0.000345065562456867, 0.000345065562456867, 0.000345065562456867,
0.000345065562456867, 1)), class = "data.frame", row.names = c(NA,
-94L))
ggplot(model_file,
aes(false_positive_rate,true_positive_rate)) +
geom_line(color= "blue") +
geom_line(data = patient_file, color = "red")+
xlab("False Positive rate") + ylab("True Positive rate")+
theme(legend.position="top",
plot.title = element_text(hjust = 0.5),
text = element_text(size=20))+
scale_color_manual(values=c("A"="abc", "B"="xyz"))
I want to add a legend (A= abc, B=xyz) to the bottom of the plot.
Upvotes: 0
Views: 37
Reputation: 20329
First you need to combine both data sets like so:
library(tidyverse)
new_data <- bind_rows(
model_file %>% select(-thresholds) %>% mutate(type = "abc"),
patient_file %>% mutate(type = "xyz"))
This creates one data frame where model data gets type abc
and patient data gets type xyz
Then you can create a plot, where you map color to type to get the legend and adapt the legend as you need it:
ggplot(new_data, aes(true_positive_rate, false_positive_rate, color = type)) +
geom_line() +
scale_color_manual(values = c(abc = "red", xyz = "blue")) +
theme(legend.position = "bottom")
Upvotes: 1