14

More code than you really need, but to set the mood:

#Make some data and load packages
data<-data.frame(pchange=runif(80,0,1),group=factor(sample(c(1,2,3),80,replace=T)))
library(dplyr)
library(magrittr)
library(gridExtra)
library(ggplot2)
data%<>%arrange(group,pchange) %>% mutate(num=1:80)

#Make plot that includes unicode characters
g1<-ggplot(data, aes(factor(num),pchange, fill = group,width=.4)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_classic()+
  theme(axis.ticks = element_blank(), 
        axis.text.x = element_blank(),
        legend.position="right")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  xlab("")+
  scale_fill_discrete("Arbitrary Group",
                      breaks=c(1,2,3),
                      labels=c("< 1 Year", "\u2265 1 Year & \n\u2264 5 Years","> 5 Years"))


#I want to add an A below the plot (this may not be necessary for the issue, but its a part of the workflow so I thought I'd include it.
g <- arrangeGrob(plot=g1,
                 sub = textGrob("A",
                                x = .1,
                                hjust = .5,
                                vjust=-2,
                                gp = gpar(fontface = "bold",
                                          fontsize = 16,
                                          col="black")))

#Save the plot
ggsave(filename="X:/yourpath/Plot1.pdf", plot=g,
       width = 8, height = 4, units = "in", dpi = 600)

Here's what it looks like: Actual Plot

Here's what it should look like (in terms of the characters in the key; plot taken as jpeg directly from Rstudio plot window): Ideal Plot

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrew Taylor
  • 3,438
  • 1
  • 26
  • 47

1 Answers1

25

You have two options. One, use the cairo_pdf device instead of the default pdf in your call you ggsave, e.g.,

library(Cairo)
ggsave(filename="X:/yourpath/Plot1.pdf", plot=g, device=cairo_pdf,
       width = 8, height = 4, units = "in", dpi = 600)

The other option would be to use expressions instead of explicit unicode characters:

g<-ggplot(data, aes(factor(num),pchange, fill = group,width=.4)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_classic()+
  theme(axis.ticks = element_blank(), 
        axis.text.x = element_blank(),
        legend.position="right")+
  scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
  xlab("")+
  scale_fill_discrete("Arbitrary Group",
                      breaks=c(1,2,3),
                      labels=c(expression(phantom(0) < "1 Year"), 
                              expression(paste(phantom(0) >= "1 Year &", phantom(0) <= "5 Years")),
                              expression(phantom(0) > "5 Years")))



ggsave(filename="Plot1.pdf", plot=g,
       width = 8, height = 4, units = "in", dpi = 600)

Although, as you can see, with the second option the formatting isn't as tight as you might like.

As to why you are experiencing this issue, according to the answer here, the pdf driver can only handle single byte encodings.

enter image description here

Community
  • 1
  • 1
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • I am not able to `ggsave()` to work with `device=cairo_pdf`. I get the error: `Error in grid.newpage() : cairo error 'error while writing to output stream'` Any suggestions? – Andrew Taylor Feb 26 '15 at 17:43
  • 1
    Can you try it with `CairoPDF` from the **Cairo** package? – Matthew Plourde Feb 26 '15 at 17:55
  • 1) Either. I was getting the error with the g or g1 object. 2) Also, from a [quick](http://www.r-bloggers.com/using-cairographics-with-ggsave/) search, it seems that Cairo is producing bitmap instead of vector output. If that is true, it is preferred that I stay in vector format. Thus, I will use `expressions`, though I can't figure out a good way to pull the second label to two lines as I had before. – Andrew Taylor Feb 26 '15 at 17:57
  • any pdf is a vector format. I think you're misunderstanding that post. – Matthew Plourde Feb 26 '15 at 18:01
  • `device=cairo_pdf` solved this problem on Ubuntu 16.04. – effel Apr 25 '17 at 16:09
  • @MatthewPlourde from `?cairo_pdf`: "Note that unlike postscript and pdf, cairo_pdf and cairo_ps sometimes record bitmaps and not vector graphics. On the other hand, they can (on suitable platforms) include a much wider range of UTF-8 glyphs, and embed the fonts used." (sorry for necroing this ;) ) – Gerhard Burger Mar 29 '22 at 13:17