Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...