Wagner Jorge
Wagner Jorge

Reputation: 430

Correct legend in ggplot2

I'd like to plot correctly the legend in the graphic of the MWE, i.e a line for PSO and a pointed line for Approach.

mean_f2 <- data.frame(Probabilities = seq(0.1, 1, 0.1), Approach = runif(10),
                      PSO =  10*runif(10))

g2 <- ggplot(data = mean_f2, aes(x = Probabilities)) + 
             geom_point(aes(y = Approach, colour = 'Approach')) + 
             geom_line(aes(y = Approach, colour = 'Approach')) +
             geom_line(aes(y = PSO, colour = 'PSO')) + 
             scale_colour_manual('', breaks = c('Approach', 'PSO'), 
                      values = c('Approach' = 'black', 'PSO' = 'gray')) +
            scale_x_continuous(breaks = seq(0.1, 1, 0.1), limits = c(0.1, 1)) +
               xlab('Probabilities') + ylab('Error') + theme_bw()
g2

enter image description here

Upvotes: 1

Views: 59

Answers (1)

mt1022
mt1022

Reputation: 17299

Is this what you want?

enter image description here

Produced with:

g2 + guides(color = guide_legend(override.aes = list(pch = c(16, NA))))

Upvotes: 2

Related Questions