Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set tick labels to edges of continuous ggplot2 legend

Tags:

r

ggplot2

Let's say I have the following plot:

library(ggplot2)
d = subset(diamonds, price >= 257 & price <= 8888)

ggplot(d, aes(depth, carat, colour = price)) +
  geom_point() +
  scale_colour_gradient(limits = c(257, 8888))

plot

How can I change the legend so that the tick mark labels show the minimum and maximum values (257 and 8888)? I want the reader to know what the limits are by the legend, not have to guess.

like image 876
CephBirk Avatar asked Jan 06 '16 17:01

CephBirk


1 Answers

You can specify the breaks and the labels:

ggplot(d, aes(depth, carat, colour = price)) +
  geom_point() +
  scale_colour_gradient(limits = c(257, 8888), 
                        breaks = c(257, 2000, 4000, 6000, 8000, 8888),
                        labels = c(257, 2000, 4000, 6000, 8000, 8888))

enter image description here

like image 191
mpalanco Avatar answered Nov 02 '22 03:11

mpalanco