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:
Qui è quello che dovrebbe essere simile (in termini di caratteri nella chiave, trama presa come jpeg direttamente dalla finestra di trama Rstudio):
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? –
Puoi provarlo con 'CairoPDF' dal pacchetto ** Cairo **? –
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. –