Capital Greek Letters in ggplot legends

1,444 views
Skip to first unread message

Rodrigo Plei

unread,
Jan 3, 2017, 12:20:46 PM1/3/17
to ggplot2
Dear all,

I am working on a heatmap using ggplot2, and I need to put the greek letter "omega" in capital form in the legend.

I tried several google searches and modified my script accordingly without success.

Here my script (adapted from a public domain script took from R-bloggers:

#------

ggheatmap2 <- ggplot(AHM, aes(Var1, Var2, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "white", high = "blue4", mid = "white", 
                       space = "Lab", 
                       name="Grand Mean") +
  #theme_minimal()+ # minimal theme
  theme(legend.title = element_text(size = 12),legend.text=element_text(size=12))+
  theme(axis.text.x = element_text(angle = 360, vjust = 1, 
                                   size = 12, hjust = 1))+
  theme(axis.text.y = element_text(angle = 360, vjust = 1, 
                                   size = 12, hjust = 1))+
  coord_fixed()
# Print the heatmap
print(ggheatmap2)

#------

Where "AHM" is my dataset.

I tried several arguments to put the capital omega using the argument "name" as above, but all failed.

I would greatly appreciate any help.

Happy new year,

Rodrigo


Sherif Soliman

unread,
Jan 3, 2017, 4:03:46 PM1/3/17
to ggplot2

Hi Rodrigo,

Did you come across this page?
http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

See the "Modifying the text of legend titles and labels" heading.

We don't have your data so I can't try to reproduce your exact plot, but here is an example using the built-in iris dataset:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point(aes(color = Species)) +
    scale_color_discrete(name="Ω")

As you can see, the trick is to use the name argument in scale_color_discrete() or in your case since you're using scall_fill_gradient2 to use scale_fill_discrete() to set the legend title.

Does this do what you need?

Sherif

Ben Bolker

unread,
Jan 3, 2017, 4:10:43 PM1/3/17
to ggp...@googlegroups.com

For what it's worth I think I'd slightly prefer using the ?plotmath
interface:

g0 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color = Species))
g0 + scale_colour_discrete(name=~Omega)

The tilde (~) gets ggplot to interpret what follows as an
*expression*, which means it gets handed to the parser used as in
?plotmath; this is more flexible (and probably more robust
cross-platform?), i.e. you could do something like

g0 + scale_colour_discrete(name=~hat(Omega))

If you want to go to the next level of elegance, look into the tikz
package, e.g.
http://iltabiai.github.io/tips/latex/2015/09/15/latex-tikzdevice-r.html
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ggplot2+u...@googlegroups.com
> <mailto:ggplot2+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Sherif Soliman

unread,
Jan 3, 2017, 4:26:36 PM1/3/17
to ggp...@googlegroups.com

On 3 Jan 2017, at 16:08, Ben Bolker wrote:

> For what it's worth I think I'd slightly prefer using the ?plotmath
> interface:
>
> g0 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
> geom_point(aes(color = Species))
> g0 + scale_colour_discrete(name=~Omega)
>
> The tilde (~) gets ggplot to interpret what follows as an
> *expression*, which means it gets handed to the parser used as in
> ?plotmath; this is more flexible (and probably more robust
> cross-platform?), i.e. you could do something like
>
> g0 + scale_colour_discrete(name=~hat(Omega))
>
> If you want to go to the next level of elegance, look into the tikz
> package, e.g.
> http://iltabiai.github.io/tips/latex/2015/09/15/latex-tikzdevice-r.html
>
>

That's very cool. I had no idea this ability/interface existed.

It does produce a different looking "Omega" though - a
characteristically LaTeX-looking one. Still, very cool, thanks for
sharing.

Sherif

Brian

unread,
Jan 3, 2017, 4:49:39 PM1/3/17
to ggp...@googlegroups.com
Hi Rodrigo,
please check out ?plotmath to supply the correct "expression".

From the hip:

name = expression(Omega)

HTH,
Brian
Reply all
Reply to author
Forward
0 new messages