Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: theme_set, pdf export, and Illustrator having difficulties

I have a list of all theme elements stored as a theme that is evoked before plotting. However, I recently found that when I export to pdf and try to open in Adobe Illustrator, I get an error "Acrobat PDF File Format is having difficulties." I've also seen Illustrator report that it has "Too few operands." However, when I use a standard theme (say, theme_set(theme_gray())), there's no problem in Illustrator. Also, regardless of the theme, I don't have issues with plotting or saving modified figures.

Perhaps someone can suggest a) the best way to store and evoke large quantities of theme settings, and b) what could be the source of my ggplot2 -> pdf -> Illustrator woes.

Here is my current theme, how I evoke it, and a sample plot. Note that some of the parameters are commented out, serving as placeholders if I should need them later.

textc <- "grey20"
gridc <- "grey20"
backc <- "white"
fontsize <- 12

new_theme <- theme_set(theme_update(  
  #axis.title = element_text()
  axis.title.x = element_text(colour=textc,size=fontsize,angle=0,hjust=.5,vjust=.5,face="plain"),
  axis.title.y = element_text(colour=textc,size=fontsize,angle=90,hjust=.5,vjust=.5,face="plain"),

  #axis.text = element_text()
  axis.text.x = element_text(colour=textc,size=fontsize,angle=0,hjust=.5,vjust=1.5,face="plain"),
  axis.text.y = element_text(colour=textc,size=fontsize,angle=0,hjust=1,vjust=0,face="plain"),

  axis.ticks = element_line(colour=gridc, size=0.5, linetype="solid"),
  axis.ticks.length = unit(.25,'cm'),
  axis.ticks.margin = unit(.25,'cm'), 

  axis.line = element_line(colour=gridc, size=NA, linetype="solid"),
  #axis.line.x = element_line()
  #axis.line.y = element_line()

  legend.background = element_rect(colour=NA,fill=NA,size=NA,linetype="solid"), # removes title and legend
  legend.margin = unit(0,"cm"),
  legend.key = element_rect(colour=NA,fill=NA,size=NA,linetype="solid"),
  legend.key.size = unit(1, 'cm'), # spacing between entries
  #legend.key.height = unit(),
  legend.key.width = unit(1,'cm'),
  legend.text = element_text(colour=textc,size=fontsize,angle=0,hjust=0,vjust=0,face="plain"),
  #legend.title.align =  0,#between 0 and 1
  legend.title = element_text(colour=textc,size=fontsize,angle=0,hjust=0,vjust=0,face="plain"),
  #legend.title.align = 0,# between 0 and 1

  #legend.position = "right"
  #legend.direction = "horizonal"
  #legend.justification = "center"
  #legend.box = "horizontal  

  panel.background = element_rect(colour=NA,fill=NA,size=NA,linetype="solid"),
  panel.border = element_rect(colour=NA,fill=NA,size=NA,linetype="solid"),
  panel.margin = unit(c(0, 0, 0, 0),'cm'),
  #panel.grid = element_line()
  panel.grid.major = element_line(colour=gridc, size=.4, linetype="dashed"),
  panel.grid.minor = element_line(colour=gridc, size=.4, linetype="dashed"),
  panel.grid.minor.x = element_blank(),
  panel.grid.major.x = element_blank(),
  #  panel.grid.minor.y = element_blank(),
  #  panel.grid.major.y = element_blank(),

  plot.background = element_rect(colour=NA,fill=backc,size=NA,linetype="solid"),
  plot.title = element_text(colour=textc,size=16,angle=0,hjust=0,vjust=1,face="plain"),
  plot.margin = unit(c(.25, .25, .25, .25),'in') # top, right, bottom, left

  #strip.background = element_rect(colour=NA,fill=NA,size=NA,linetype=NA),
  #strip.text
  #strip.text.x = element_text(colour=textc,size=fontsize,angle=0,hjust=0,vjust=0,face="plain"),
  #strip.text.y = element_text(colour=textc,size=fontsize,angle=-90,hjust=0,vjust=0,face="plain")
))

theme_set(theme_gray())
theme_set(new_theme)
options(scipen=9999) # suppress scientific notation


d <- ggplot(mtcars,aes(x=wt,y=mpg))+
  stat_binhex()+
  scale_fill_gradientn(colours=c("darkorange2","red","black"),name = "Frequency",na.value=NA)
try(ggsave(plot=d,filename=<some file.pdf>,height=4,width=6))

SessionInfo():

R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] hexbin_1.26.0      lattice_0.20-10    reshape2_1.2.1     Hmisc_3.10-1       survival_2.36-14  
[6] plyr_1.7.1         RColorBrewer_1.0-5 ggplot2_0.9.2.1   

loaded via a namespace (and not attached):
 [1] cluster_1.14.3   colorspace_1.2-0 dichromat_1.2-4  digest_0.5.2     gtable_0.1.1    
 [6] labeling_0.1     MASS_7.3-22      memoise_0.1      munsell_0.4      proto_0.3-9.2   
[11] scales_0.2.2     stringr_0.6.1    tools_2.15.2 
like image 839
metasequoia Avatar asked Nov 07 '12 02:11

metasequoia


1 Answers

size=NA in axis.line = element_line(colour=gridc, size=NA, linetype="solid") cause the problem. Use element_blank if you do not want to show the line.

This may not be a bug, but I suggest you to file this problem on github: https://github.com/hadley/ggplot2/issues In future, ggplot2 may have ability to check the validity of parameters.

like image 116
kohske Avatar answered Nov 10 '22 19:11

kohske