+1 vote
in Programming Languages by (7.3k points)

I am using ggplot for generating a plot. When I display the legend, it shows the title of the legend too. How can I remove the title of the legend from my plot?

Here is my code:

p1 <- ggplot(ndf, aes(x=xvals, y=yvals, fill = category, colour = category)) +

        geom_line() +

        scale_color_manual(values = c('black', 'red', 'blue', '#4DAC26')) +

        labs(x = "class probability", 

             y = "pdf" 

             ) +

        theme_bw() +

        theme(legend.position = c(.99, .99))

1 Answer

0 votes
by (30.2k points)
edited by

You need to add legend.title=element_blank() to the theme to remove the title of the legend.

Change your code as follows:

p1 <- ggplot(ndf, aes(x=xvals, y=yvals, fill = category, colour = category)) +
        geom_line() +
        scale_color_manual(values = c('black', 'red', 'blue', '#4DAC26')) +
        labs(x = "class probability",
             y = "pdf"
             ) +
        theme_bw() +
        theme(legend.position = c(.99, .99), legend.title=element_blank())

...