Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 grouping legend

Tags:

r

ggplot2

I have a data frame trajectory_df that has the following structure:

table

The uid row has 6 different possible values, each representing a particular user. So I want to plot 6 lines on the same plot, and I'm currently doing it in this way:

ggplot(trajectory_df, aes(Month, Pagerank, colour=uid, group=uid)) + geom_line() + geom_point() + scale_x_discrete(breaks=month_ticks)

Which gives me this picture:

the graph

Which is just what I want, except for the legend. I want there to be 6 different entries in the legend, not a colorful range of values.

How can I accomplish this?

like image 916
wrongusername Avatar asked Nov 18 '25 10:11

wrongusername


1 Answers

Try this instead:

ggplot(trajectory_df, aes(Month, Pagerank, colour=factor(uid), group=uid)) + 
   geom_line() + 
   geom_point() + 
   scale_x_discrete(breaks=month_ticks)
like image 128
joran Avatar answered Nov 20 '25 01:11

joran