简体   繁体   中英

Adding legend to multiline ggplot

I'm trying to add a legend to my plot at ggplot. The dataset can be seen in

   percent       KNN  logistic       LDA       QDA
1     0.10 0.7393658 0.2815159 0.2907966 0.2737819
2     0.20 0.4926279 0.2558543 0.2723330 0.2619254
3     0.30 0.4631474 0.2539841 0.2579681 0.2569721
4     0.40 0.3995381 0.2448037 0.2471132 0.2471132
5     0.50 0.3400538 0.2258065 0.2271505 0.2298387
6     0.60 0.3287671 0.2226027 0.2208904 0.2243151
7     0.75 0.3834808 0.2684366 0.2684366 0.2743363
8     0.80 0.3771626 0.2664360 0.2698962 0.2733564
9     0.90 0.3509934 0.2582781 0.2649007 0.2516556
10    0.99 0.2727273 0.3636364 0.3636364 0.3181818

The thing is that it doesn't let use the names of the columns as guide to create the legend of the plot. The code I used to create the plot is the following:

Default <- (ggplot(default_error, aes(x = percentages, colour = variable))
            + geom_line(aes(y = KNN), color = "darkred")
            + geom_line(aes(y = logistic), color = "steelblue")
            + geom_line(aes(y = LDA), color = "green")
            + geom_line(aes(y = QDA), color = "coral3")
       + labs(y = "Error rate", x = "Percentage", title = "Default Error", color = "Method")
       + theme_linedraw()
       + theme(legend.title = element_text(size = 25),
               legend.text = element_text(size = 20),
               legend.position = "right",
               axis.title = element_text(size = 20),
               axis.text = element_text(size = 15, angle = 0, vjust = 0.8, hjust = 0.7),
               plot.title = element_text (face = NULL, hjust = 0, size = 30, family = "montse", vjust = 0),
               plot.subtitle = element_text (face = NULL, hjust = 0.5, size = 10, family = "montse", vjust = 0))
       + scale_color_discrete(name = "Method", labels = c("KNN", "Logistic","LDA","QDA")))
library(tidyr)
default_error <- pivot_longer(default_error, names(default_error)[-1])

Then you don't need all the geom_lines, and the colors can be given in scale_color_manual() :

(ggplot(x, aes(x = percent, y = value, colour = name))
  + geom_line()
  + labs(y = "Error rate", x = "Percentage", title = "Default Error", color = "Method")
  + theme_linedraw()
  + theme(legend.title = element_text(size = 25),
          legend.text = element_text(size = 20),
          legend.position = "right",
          axis.title = element_text(size = 20),
          axis.text = element_text(size = 15, angle = 0, vjust = 0.8, hjust = 0.7),
          plot.title = element_text (face = NULL, hjust = 0, size = 30, family = "montse", vjust = 0),
          plot.subtitle = element_text (face = NULL, hjust = 0.5, size = 10, vjust = 0))
  + scale_color_manual(name = "Method", 
                         values = c(KNN = "darkred", logistic = "steelblue", LDA = "green", QDA = "coral3"),
                         labels = c("KNN", "Logistic","LDA","QDA")))

That should do it.

In the future, please make sure to provide a reproducible example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM