B. Davis
B. Davis

Reputation: 3441

Changing the labels in multiple ggplot legends

Using the diamonds data set and the following code I created the fig below.

ggplot(diamonds[diamonds$color == c("D", "E", "F"),], aes(x=carat, y=price, shape = color, color=cut)) +
  geom_point() 

enter image description here

I am trying to change the legend labels (not titles) to something other than they currently are.

I have been trying the code (below) for a single legend

ggplot(diamonds[diamonds$color == c("D", "E", "F"),], aes(x=carat, y=price, shape = color, color=cut)) +
  geom_point() + 
  scale_shape_manual(lables = c("DDD", "EEE", "FFF"))

But get this error:

Error in discrete_scale(aesthetic, "manual", pal, ...) : 
  unused argument (lables = c("DDD", "EEE", "FFF"))

How do you specify the legend to be altered when there are two?

Thanks

Upvotes: 1

Views: 2675

Answers (3)

lukeA
lukeA

Reputation: 54237

Note your typo lables instead labels that's causing the error. Try

scale_shape_discrete(labels = c("DDD", "EEE", "FFF"))

Upvotes: 3

Bangyou
Bangyou

Reputation: 9816

Or you can use factor to change label of each level

diamonds2 <- diamonds[diamonds$color == c("D", "E", "F"),]
diamonds2$color <- factor(diamonds2$color, levels = c('D', 'E', 'F'), labels = c("DDD", "EEE", "FFF"))

ggplot(diamonds2, aes(x=carat, y=price, shape = color, color=cut)) +
  geom_point() 

Upvotes: 2

Ram Narasimhan
Ram Narasimhan

Reputation: 22496

If you pass values as well to scale_shape_manual you can change the legend labels.

ggplot(diamonds[diamonds$color == c("D", "E", "F"),], aes(x=carat, y=price, shape = color, color=cut)) +  geom_point() + 
  scale_shape_manual(values=1:3,
                      labels=c("CCC", "DDD", "EEE"))

Produces: enter image description here

Upvotes: 3

Related Questions