2

I tried to add subscripts to my ggplot2 geom_point colour legend by using scale_colour_discrete. Similar problems popped up here

p <- ggplot(myData, aes(myFeature1,myFeature2))
p <- p + geom_point(aes(colour = myFeature3)) + facet_grid(n ~ cond)
p <- p + scale_colour_discrete(breaks = levels(myData$myFeature3), labels = c(expression(myFeature3[1]),expression(myFeature3[2]))

However, the following error occurs: Error in FUN(X[[1L]], ...) : cannot coerce type 'symbol' to vector of type 'double'

This error does NOT occur, without in the labels definition expression. It DOES occur whatever is inside the expression.

Any ideas on the subject? Does scale_colour_discrete just not work with expression? Is there another way to get subscripts into those legend factor names?

Thanks a lot!

Community
  • 1
  • 1
Helga
  • 91
  • 2
  • 5

2 Answers2

1

Can't replicate this without your data. Trying something similar with the 'diamonds' data as described in help(scale_colour_discrete):

d + scale_colour_discrete(breaks=levels(diamonds$clarity),labels=rep(expression(a^2),8))

works, labelling all levels with a-squared in math notation.

d + scale_colour_discrete(breaks=levels(diamonds$clarity),labels=rep(expression(clarity[1]),8))

works, labelling with clarity subscript 1 in all levels.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
1

Here is an example using the diamonds data set where the labels for the diamond cut are replaced by Cut_1, ..., Cut_5.

ggplot(diamonds, aes(x = carat, y = price)) + 
  geom_point(aes(colour = cut)) + 
  facet_grid(color ~ clarity) + 
  scale_colour_discrete(breaks = levels(diamonds$cut), 
                        labels = c(expression(Cut[1]),
                                   expression(Cut[2]),
                                   expression(Cut[3]),
                                   expression(Cut[4]),
                                   expression(Cut[5])))

If this is not helping you find a solution to your problem can you provide a reproducible example for others to trouble shoot?

Peter
  • 7,460
  • 2
  • 47
  • 68