R ggplot2: legend should be discrete and not continuous

时光毁灭记忆、已成空白 提交于 2019-11-30 06:50:49

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/20301922/r-ggplot2-legend-should-be-discrete-and-not-continuous

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!