Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot2: legend should be discrete and not continuous

Tags:

r

legend

ggplot2

I have the following data:

benchmark   mispredpenal    IPC     pred
ammp        1               1.0589  2lev
ammp        5               1.0450  2lev
...

and use the following command:

ggplot(IPC, aes(x = benchmark, y = IPC, group=mispredpenal, colour=mispredpenal)) + 
  geom_point() + geom_line()

Everything looks like it should, but I would like the legend to be discrete, and not the continuous (gradient). How should I do this?

Edit: Misprediction is either 1, 5, 9, 13 or 17.

like image 796
user720491 Avatar asked Nov 30 '13 15:11

user720491


People also ask

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

How do I get rid of the legend in ggplot2?

Example 1: Remove All Legends in ggplot2 We simply had to specify legend. position = “none” within the theme options to get rid of both legends.


1 Answers

You want the variable mispredpenal to be a factor in that case:

ggplot(IPC, aes(x = benchmark, y = IPC, group=factor(mispredpenal), colour=factor(mispredpenal))) + 
  geom_point() + geom_line()
like image 76
musically_ut Avatar answered Sep 18 '22 03:09

musically_ut