2

I am trying to add a \lozenge to a ggplot2 title. Some example code would be:

tmp = data.frame(x = rnorm(100), y = rnorm(100))
ggplot(tmp, aes(x, y)) + 
  geom_point() + 
  ggtitle(expression(lozenge))

But I cannot get the lozenge to appear. It simply prints the word lozenge. I also need to be able to change the colour of the lozenge.

nathaneastwood
  • 3,664
  • 24
  • 41

1 Answers1

1

You can try:

ggplot(tmp, aes(x, y)) + 
         geom_point() + 
         ggtitle(bquote(symbol("\340")))

To change its color, you can add a theme argument.
For example, for a red lozenge:

ggplot(tmp, aes(x, y)) + 
            geom_point() +   
            ggtitle(bquote(symbol("\340"))) +  
            theme(plot.title=element_text(color="red"))

with the example: enter image description here

EDIT

If you want to have a bi-color title, following Roland's solution of this question, this is how to do it:

# assign your plot object to a variable
p<-ggplot(tmp, aes(x, y)) + geom_point() + ggtitle(bquote(paste("some text ",symbol("\340")," some more text",sep="")))

# get the "Grob" object
grob_p<- ggplotGrob(p)

# modify the text and text colors. Here, you have to use the hexadecimal code for the lozenge    
grob_p[[1]][[8]]$label<-c("some text ", bquote("\U0025CA"), " some more text")
grob_p[[1]][[8]]$gp$col<-c("black","red","black")

# adjust the coordinates of the different strings
grob_p[[1]][[8]]$x<-unit(c(0.41,0.5,0.63),"npc")

# plot the modified object
plot(grob_p)

enter image description here

Community
  • 1
  • 1
Cath
  • 23,906
  • 5
  • 52
  • 86
  • Cool thanks. Is there a symbol for a filled lozenge? And also, my title will also include additional text, which I want to remain black, so is there a way to colour just the lozenge itself? – nathaneastwood Feb 16 '15 at 12:28
  • 1
    @Natty_E, yes, sure, it's called a "diamond" and the code is `250`. I'm not really used to `ggplot` so I don't know what would be the best way to draw a 2-color title. In regular plot, I would put the title in 2 calls, one for black words and one for colored words. – Cath Feb 16 '15 at 12:32
  • Ok no worries. I can probably work that part out myself. Thanks for your help :) – nathaneastwood Feb 16 '15 at 12:34
  • @Natty_E, see my edit to have (for example) red lozenge and black text in the title – Cath Feb 16 '15 at 13:41