how to use a gradient color bar for the legend

36 views
Skip to first unread message

lily li

unread,
Aug 31, 2017, 2:20:38 AM8/31/17
to ggp...@googlegroups.com
Dear ggplot2 users,
I have a question about the legend. For example, my dataset "dataf" is like this:

id  cl   x    y
1   1 2.5 11.0
2   1 4.3 12.0
3   1 2.2 10.0
4   1 5.4  9.0
5   1 6.0 10.0
6   2 7.7  8.0
7   2 7.1  7.8
8   2 2.5  8.9
9   3 5.5  8.8
10  3 4.0 72.0
11  3 4.5  8.0
12  4 6.2 10.0
13  4 7.0 10.5

I use the code to print the points.
plot1 = ggplot(dataf,aes(x=x,y=y,color=factor(cl)))+theme_bw()+ geom_point()
print(plot1)

However, if there are more than 100 classes in the 'cl' column, the legend becomes compact and is not easy to recognize. I'm wondering how to use a gradient color bar to represent the legend? This means that not every 'cl' value is listed in the legend, but just a gradient bar (maybe show the smallest and largest values at the two ends of the bar) to represent the color changes. Thanks for your help in advance.

Roman Luštrik

unread,
Aug 31, 2017, 3:06:49 AM8/31/17
to lily li, ggp...@googlegroups.com
Remove the `factor(cl)` around `cl`. Assuming `cl` is integer/numeric.

Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

lily li

unread,
Aug 31, 2017, 9:21:45 AM8/31/17
to Roman Luštrik, ggp...@googlegroups.com
Hi Roman,

Thanks, it works and shows a gradient bar. Would it be possible to increase the color contrasting? Before, the colors change as a rainbow color, now the points all have a blue color and hard to differentiate. Thanks again.

Roman Luštrik

unread,
Aug 31, 2017, 9:54:58 AM8/31/17
to lily li, ggp...@googlegroups.com

If you search the internet for "increase contrast gradient ggplot2" or something akin to this, you can find stackoverflow questions like this one:

Cheers,
Roman
Reply all
Reply to author
Forward
0 new messages