3

I want to get a symbol in a ggplot legend without specifying in the scale.

I want this:

df <- data.frame(a=1:5, b=factor(1:5, labels=c(1:4, "\u2265 5")), c=10:14)

ggplot(df, aes(a,c, color=b)) + geom_point()

which produces this plot

enter image description here

To produce a plot like this:

ggplot(df, aes(a,c, color=b)) + geom_point() + scale_color_brewer(labels=c(1:4, "\u2265 5"))

enter image description here

Is that possible?

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.0

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   digest_0.6.4       grid_3.1.2        
 [4] gtable_0.1.2       labeling_0.3       MASS_7.3-35       
 [7] munsell_0.4.2      plyr_1.8.1         proto_0.3-10      
[10] RColorBrewer_1.0-5 Rcpp_0.11.3        reshape2_1.4      
[13] scales_0.2.4       stringr_0.6.2      tools_3.1.2 
Tom
  • 4,860
  • 7
  • 43
  • 55
  • If you name factor levels of `b` as `1`, `2`, `3`, `4`, `≥5`, yes. – Roman Luštrik Jan 15 '14 at 09:35
  • 1
    @RomanLuštrik the problem is that `≥` is being converted to `=` in the data.frame (on my Windows 7 PC). Try `labels=c(1:4, "\u2265 5");labels`. – Simon O'Hanlon Jan 15 '14 at 09:40
  • I have tested it and it indeed doesn't work. Interesting. – Roman Luštrik Jan 15 '14 at 09:53
  • Hmm, on my system `R version 3.0.2 (2013-09-25) Platform: x86_64-suse-linux-gnu (64-bit)` data.frame as well as legend is ok. – redmode Jan 15 '14 at 11:13
  • @redmode that figures! A windows problem most likely (as per usual). – Simon O'Hanlon Jan 15 '14 at 11:38
  • I am indeed using a windows 7 PC as well. `R version 3.0.2 (2013-09-25)` and `ggplot2 0.9.3.1`. – Tom Jan 15 '14 at 12:45
  • I can reproduce the problem on Windows as well, but notably `plot(df$a,df$c,main='\u2265')` works. – nograpes Jan 15 '14 at 13:21
  • Also, you can label the legend properly with Unicode: `ggplot(df, aes(a,c, color=b)) + geom_point() + scale_color_discrete(guide = guide_legend(title = "\u2265"))`. I suspect that this is related to a [previous question of mine](http://stackoverflow.com/questions/17715956/why-do-some-unicode-characters-display-in-matrices-but-not-data-frames-in-r). You'll note that the character probably doesn't display correctly when you just type `df` either. – nograpes Jan 15 '14 at 13:29
  • @nograpes Explicitly putting the unicode in ggplot works for me too (whether in the legend, or titles, or axis labels). It's the passing via a factor that gets me. You're right typing `df` does not work, but typing `df$b` does. – Tom Jan 15 '14 at 14:34
  • I believe it is an instance of https://stackoverflow.com/questions/58448811/what-does-format-do-to-utf-8-strings. – jarauh Oct 18 '19 at 10:19

0 Answers0