1

I am trying to export a figure as SVG from R to Inkscape (I am using RStudio 0.98.1102 and R version 3.1.2). Here is my script.

library(ggplot2)

Gene<-c("Gene1","Gene2","Gene1","Gene2")
count1<-c(12,14,16,34)
count2<-c(4,7,9,23)
count3<-c(36,22,54,12)
count4<-c(12,24,35,23)
Species<-c("A","A","B","B")
df<-data.frame(Gene,count1,count2,count3,count4,Species)

cols = c(2,3,4,5)
df1  = transform(df, mean=rowMeans(df[cols]), sd=apply(df[cols],1, sd))

# df1 looks like this
#   Gene count1 count2 count3 count4 Species  mean        sd
#1 Gene1     12      4     36     12       A 16.00 13.856406
#2 Gene2     14      7     22     24       A 16.75  7.804913
#3 Gene1     16      9     54     35       B 28.50 20.240224
#4 Gene2     34     23     12     23       B 23.00  8.981462

ggplot(df1, aes(x=as.factor(Gene), y=mean, fill=Species)) +
  geom_bar(position=position_dodge(), stat="identity", colour='black') +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,position=position_dodge(.9)) + 
  theme(axis.text.x  = element_text(angle=90, vjust=0.8, size=14, colour="black",face="italic")

So, as you see, I want the x axis labels to be kept in italic font when exported to SVG... and they are not! ;-) They just come back to plain font.

I have tried the "classic" way, i.e. saving SVG directly with the plot interface in RStudio, and I have tried both ways described here, using whether ggsave or the Cairo package... None is working!

Any idea on what I should do?

Thanks a lot!


EDIT:

What I have been able to do thanks to @baptiste comment:

  1. Draw my plot with ggplot2 and export it as SVG (with the legend not in italic) in file1.svg

  2. Draw my plot with gridSVG in file2.svg using grid.export("file2.svg") (with error bars not displayed properly but with italic axis legend)

  3. Modify file2.svg to keep only the italic axis legend and delete the other layers

  4. Import the new file2.svg (italic axis legend only) into file1.svg using Inkscape, and adjust manually

Not elegant AT ALL, but working, though I would not like to do it for many files ;) Hence I do not accept my edit as an answer!

Community
  • 1
  • 1
tlorin
  • 1,100
  • 6
  • 17
  • 30
  • maybe try the gridSVG package – baptiste May 05 '15 at 08:11
  • @baptiste Mmmh... I just tried it (adding `grid.export("example.svg")` at the end of my script) and it's working indeed for the font, but I am not getting the error bars properly now! Maybe I should try another gridSVG function, or draw plot with another package? – tlorin May 05 '15 at 08:23
  • 1
    I can get the correct output with `ggsave` to open in Firefox, but it looks a mess in Illustrator. Perhaps Inkscape doesn't interpret the italics correctly. – James May 05 '15 at 08:44

0 Answers0