2015-02-26 2 views
6

अधिक कोड से यूनिकोड वर्णों से खोने: Actual Plotggsave 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) 

यहाँ यह क्या लग रहा है की तरह है कुंजी में वर्णों के संदर्भ में; सीधे रुस्टूडियो प्लॉट विंडो से जेपीईजी के रूप में लिया गया प्लॉट): Ideal Plot

उत्तर

7

आपके पास दो विकल्प हैं। एक, अपने आप कॉल ggsave, जैसे में डिफ़ॉल्ट pdf के बजाय cairo_pdf डिवाइस का उपयोग

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

अन्य विकल्प स्पष्ट यूनिकोड वर्ण के बजाय एक्सप्रेशन का उपयोग करना होगा,:,

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) 

हालांकि के रूप में आप देख सकते हैं, दूसरे विकल्प के साथ स्वरूपण उतना तंग नहीं है जितना आप चाहें।

here के उत्तर के अनुसार, आप इस समस्या का सामना क्यों कर रहे हैं, pdf ड्राइवर केवल एक बाइट एन्कोडिंग को संभाल सकता है।

enter image description here

+0

मैं 'ggsave नहीं पा रहा हूँ()' 'डिवाइस = cairo_pdf' के साथ काम करने के लिए। मुझे त्रुटि मिलती है: 'grid.newpage में त्रुटि(): कैरो त्रुटि' आउटपुट स्ट्रीम में लिखते समय त्रुटि 'कोई सुझाव? –

+0

क्या आप इसे ** काहिरा ** पैकेज से 'काहिरा पीडीएफ' के साथ आजमा सकते हैं? –

+0

1) या तो। मुझे जी या जी 1 ऑब्जेक्ट में त्रुटि मिल रही थी। 2) इसके अलावा, एक [त्वरित] (http://www.r-bloggers.com/using-cairographics-with-ggsave/) खोज से, ऐसा लगता है कि काहिरा वेक्टर आउटपुट के बजाय बिटमैप का उत्पादन कर रहा है। यदि यह सच है, तो यह प्राथमिकता दी जाती है कि मैं वेक्टर प्रारूप में रहूं। इस प्रकार, मैं 'एक्सप्रेशन' का उपयोग करूंगा, हालांकि मैं दूसरे लेबल को दो लाइनों में खींचने का एक अच्छा तरीका नहीं समझ सकता था जैसा कि मैंने पहले किया था। –