2015-02-26 7 views
6

più codice di quello che ha realmente bisogno, ma per impostare lo stato d'animo:ggsave perdere charaters Unicode da ggplot + gridExtra

#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) 

Ecco come si presenta: Actual Plot

Qui è quello che dovrebbe essere simile (in termini di caratteri nella chiave, trama presa come jpeg direttamente dalla finestra di trama Rstudio): Ideal Plot

risposta

7

Si hanno due opzioni. Uno, utilizzare il dispositivo cairo_pdf invece del default pdf nella chiamata si ggsave, ad esempio,

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

L'altra opzione sarebbe quella di utilizzare le espressioni invece di caratteri Unicode espliciti:

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) 

Anche se, come puoi vedere, con la seconda opzione la formattazione non è così stretta come potresti.

Per quanto riguarda il motivo per cui si verifica questo problema, in base alla risposta here, il driver pdf può gestire solo codifiche a singolo byte.

enter image description here

+0

io non sono in grado di 'ggsave()' per lavorare con 'device = cairo_pdf'. Ho ricevuto l'errore: 'Errore in grid.newpage(): errore cairo 'durante la scrittura sul flusso di output'' Qualche suggerimento? –

+0

Puoi provarlo con 'CairoPDF' dal pacchetto ** Cairo **? –

+0

1) O. Stavo ricevendo l'errore con l'oggetto g o g1. 2) Inoltre, da una ricerca rapida (http://www.r-bloggers.com/using-cairographics-with-ggsave/), sembra che Cairo stia producendo bitmap anziché output vettoriale. Se ciò è vero, è preferibile che io rimanga in formato vettoriale. Quindi userò 'espressioni ', anche se non riesco a trovare un buon modo per tirare la seconda etichetta su due righe come prima. –