4

I am trying to put greek symbols in my boxplot made in ggplot2. However after going through all the previous questions on stack-overflow I cannot for the life of me get any of their examples to work.

So apologies for the repost, but if someone could help me out here I would be most appreciative.

My Code so far is:

## Data
names = LETTERS[1:3]
x = runif(99)
y = rep(names, length = length(x))
Parameters = factor(rep(c("Lambda", "Phi", "Gamma"), each = length(names)), 
                    levels = c("Lambda", "Phi", "Gamma"))
plot.df = data.frame(x, y, Parameters)
limits = quantile(plot.df[,1], probs = seq(0.1,0.9,by=0.1))
##Create Plot
dodge = position_dodge(width=0.5)
p = ggplot(plot.df, aes(x = y,y = x, colour = Parameters)) +
    geom_boxplot(aes(shape = Parameters), outlier.shape = 19, outlier.colour = NULL, outlier.size = 0.8) +
        scale_shape_manual(values = rep(19, 3)) +
            scale_y_continuous(limits = c(0, 1)) +
                coord_flip() + labs(title = "TITLE", x = "", y = "") + 
                xlim(rev(names)) +
                theme(legend.position = "right")
print(p)

Which gives:

Graph

There are a number of bits in the code that are necessary for my real data (ie. reorganizing of the x axis (which is the y axis) etc.)

I want the legend values to change into the greek letters, but I am absolutely stumped on how to do this.

Thanks

SamPassmore
  • 1,221
  • 1
  • 12
  • 32
  • Did you try [this](http://stackoverflow.com/a/20446298/489704)? – jbaums Feb 10 '14 at 01:24
  • Hi, no I hadn't tried this previously. I gave it a go and it uses the function "unit" which seems to be from the grid package - and is not available for R 3.0.0, Do you know if it has been carried to a different package? – SamPassmore Feb 10 '14 at 01:35
  • 1
    `grid` should be pre-installed in R 3.x.x. Try `library(grid)`. – jbaums Feb 10 '14 at 01:45
  • You might need to load grid. With lattice and ggplot2, it is only attached and not fully loaded. – IRTFM Feb 10 '14 at 01:46
  • Ah yes. This did work, although it plots the old legend as well as the new legend, and has altered the colours. I tried changing legend.position = "none": but this removed both legends – SamPassmore Feb 10 '14 at 01:50

1 Answers1

5

Continue from your script,

my.labs <- list(bquote(lambda),bquote(phi),bquote(gamma))

p <- p+
  scale_colour_manual(values=1:3,breaks=c("Lambda", "Phi", "Gamma"),
                      labels=my.labs)+
  scale_shape_manual(values=rep(19, 3),breaks=c("Lambda", "Phi", "Gamma"),
                      labels=my.labs)
print(p)

enter image description here

Read more

Community
  • 1
  • 1
xb.
  • 1,617
  • 11
  • 16
  • Hi, Thanks - this looks like it works, but I am yet to try it out. Could you tell me the significance of values=rep(19,3) in the scale_shape_manual function? – SamPassmore Feb 10 '14 at 03:10
  • Right. Need both a manual color and a manual shape specification to prevent two legends from being created. (Rather a strange design.) If you want caps, then just capitalize the Greek letter names. – IRTFM Feb 10 '14 at 03:21
  • 1
    @SamPassmore, `values=rep(19,3)` is just to keep the **shape** values you defined. And you can of course re-defined them. Similarly, you can define you own color by setting `values` in `scale_colour_manual`, where I set 1:3 for "balck", "red" and "green". – xb. Feb 10 '14 at 03:27