create high-quality plots with ggplot2, modify them using inkscape and embed them in word

1,833 views
Skip to first unread message

Ellen

unread,
Jun 20, 2012, 4:03:57 PM6/20/12
to ggp...@googlegroups.com
Hello ggplot'ers

I was wondering which is the best way (which type of image (pdf, svg, png,..), using other packages like Cairo...?) to make a graph in ggplot2, modify these in a program like inkscape or illustrator and embed them in word.

I'm writing my thesis and I chose Cambria as the font, and hence plot annotations also need to be in cambria. If I change this in ggplot2 (which is possible) and I open the graph (saved as pdf) in inkscape, it cannot read this letter type. However, if I don't change the font in R and import the graph in inkscape, the latter does not recognize axis labels as text and I do not seem to be able to modify the font in this program. I also tried to use the Cairo package and save the graph as a svg file. When I set the resolution to 300 (resolution needs to be specified), the graph symbols and lines become totally scrambled up! I'm becoming sort of desperate and I'm afraid I'll wind up using sigamplot or worse: excel.. ;)

Thanks for the help
Ellen

Winston Chang

unread,
Jun 20, 2012, 5:37:42 PM6/20/12
to Ellen, ggp...@googlegroups.com
Hi Ellen -

I've sometimes had better luck saving as SVG and opening in Inkscape.

I just got a package on CRAN last week that might help with your problem. If you want to use your system fonts and embed them in a PDF, it should be able to do it for you. Instructions are here (scroll down a bit):

Try making the plot and embedding the fonts in the PDF before opening. I haven't tried extrafont in this kind of situation, so I'm curious whether it solves your issue.

-Winston




Ellen

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

davebraze

unread,
Jun 21, 2012, 2:19:48 PM6/21/12
to ggp...@googlegroups.com
I try to avoid the need for post hoc editing of plots, but on the occasions when I have done it I've had generally good luck with writing to svg files from R and then editing those with Inkscape. Can't say that I've ever tried to change fonts though.

-Dave

Sam Albers

unread,
Jun 21, 2012, 2:26:13 PM6/21/12
to davebraze, ggp...@googlegroups.com
Ellen - I would echo this sentiment. When I first began using ggplot2
I followed a similar route (making a plot, exporting as svg, editing
in inkscape). As I have become more experienced with ggplot2 I have
come to realize that as a package it meets almost every need to
produce publication quality plots. If you are experiencing issues
simply post it to the list. This is a very friendly environment filled
with many knowledgeable folks. Plus eliminating the inkscape step will
save you a bunch of time and have the added bonus of being able to
reproduce previous plots with similar formatting very easily.

Sam

baptiste auguie

unread,
Jun 21, 2012, 4:20:52 PM6/21/12
to Ellen, ggp...@googlegroups.com
Hi,

I save ggplots as pdf files, with no special treatment, and open them
in Illustrator, it works perfectly well. I've used this procedure
successfully for virtually every figure of my articles. Haven't tried
with Inkscape though.

Cheers,

baptiste

Ellen Pape

unread,
Jun 25, 2012, 8:51:59 AM6/25/12
to Winston Chang, ggp...@googlegroups.com
Hello Winston,

I installed the extrafont package as you suggested, but I seem to have a problem

when I run the command font_import(), I get the following message:

Scanning ttf files in C:\WINDOWS\Fonts ...
Extracting .afm files from .ttf files...
C:\WINDOWS\Fonts\arial.ttfError in system2(ttf2pt1, c(args, shQuote(ttfiles[i]), shQuote(tmpfiles[i])),  : 
  CreateProcess failed to run 'C:\PROGRA~1\R\R-215~1.0\library\Rttf2pt1\exec\ttf2pt1.exe -G fAe "C:\WINDOWS\Fonts\arial.ttf" "C:\DOCUME~1\Elpape\LOCALS~1\Temp\Rtmp42TjjM/fonts/arial"'

I do not understand what the problem is? Maybe you can help me?

Thanks!
Ellen


Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

Brandon Hurr

unread,
Jun 25, 2012, 9:21:01 AM6/25/12
to Ellen Pape, Winston Chang, ggp...@googlegroups.com
Just to confirm, I get a similar error on OSX. I don't think Rttf2pt1 is installing correctly or I need to do something more to help it along...

> install.packages('extrafont')

also installing the dependencies ‘extrafontdb’, ‘Rttf2pt1’


trying URL 'http://cran.ma.imperial.ac.uk/bin/macosx/leopard/contrib/2.15/extrafontdb_1.0.tgz'

Content type 'application/x-gzip' length 6234 bytes

opened URL

==================================================

downloaded 6234 bytes


trying URL 'http://cran.ma.imperial.ac.uk/bin/macosx/leopard/contrib/2.15/Rttf2pt1_1.0.tgz'

Content type 'application/x-gzip' length 93086 bytes (90 Kb)

opened URL

==================================================

downloaded 90 Kb


trying URL 'http://cran.ma.imperial.ac.uk/bin/macosx/leopard/contrib/2.15/extrafont_0.9.tgz'

Content type 'application/x-gzip' length 25663 bytes (25 Kb)

opened URL

==================================================

downloaded 25 Kb



The downloaded binary packages are in

/var/folders/rg/9nvd_h115qv1x7zsxnq8tl8h0000gn/T//Rtmp7pKKwN/downloaded_packages

> library(extrafont)

> font_import()

Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system. Continue? [y/n] y

Scanning ttf files in /Library/Fonts ...

Extracting .afm files from .ttf files...

Error in which_ttf2pt1() : ttf2pt1 not found in path.

> sessionInfo()

R version 2.15.1 (2012-06-22)

Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)


locale:

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8


attached base packages:

[1] grid      stats     graphics  grDevices utils     datasets  methods   base     


other attached packages:

[1] extrafont_0.9 scales_0.2.1  ggplot2_0.9.1


loaded via a namespace (and not attached):

 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       labeling_0.1       MASS_7.3-18       

 [6] memoise_0.1        munsell_0.3        plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5

[11] reshape2_1.2.1     Rttf2pt1_1.0       stringr_0.6        tools_2.15.1    

Winston Chang

unread,
Jun 25, 2012, 10:51:42 AM6/25/12
to Brandon Hurr, Ellen Pape, ggp...@googlegroups.com
Hi Brandon -

Can you try installing the latest version from github? It looks like the error on Mac was happening because of some differences in the CRAN build system and my local machine, but I think I have a workaround.

library(devtools)
install_github('Rttf2pt1', 'wch')

-Winston

Winston Chang

unread,
Jun 25, 2012, 10:55:14 AM6/25/12
to Ellen Pape, ggp...@googlegroups.com
Hi Ellen -

Thanks for letting me know - it looks like this is happening because of some differences between the build system on CRAN and my local machine. I think the fix to make the Mac error that Brandon mentioned was pretty simple, but I'll have to do some more investigation to figure out how to fix the issue in Windows.

-Winston

Brandon Hurr

unread,
Jun 25, 2012, 11:00:46 AM6/25/12
to Winston Chang, Ellen Pape, ggp...@googlegroups.com
yup, that worked. 

Winston Chang

unread,
Jun 25, 2012, 1:57:19 PM6/25/12
to Ellen Pape, ggp...@googlegroups.com
I think I've fixed the issue and the new version 1.1 is up on CRAN, but it might take a few days before the Windows binary is available:

Again, please let me know if you run into are any more problems.

-Winston

Ellen Pape

unread,
Jun 25, 2012, 2:09:23 PM6/25/12
to Winston Chang, ggp...@googlegroups.com
Hi Winston,

Ok, I'll wait for the new windows binary then..

Thanks for the effort!

Ellen
Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

Jean-Olivier Irisson

unread,
Jun 27, 2012, 7:53:49 PM6/27/12
to Ellen, ggplot2
On 2012-Jun-20, at 22:03 , Ellen wrote:
>
> I'm writing my thesis and I chose Cambria as the font, and hence plot annotations also need to be in cambria. If I change this in ggplot2 (which is possible) and I open the graph (saved as pdf) in inkscape, it cannot read this letter type. However, if I don't change the font in R and import the graph in inkscape, the latter does not recognize axis labels as text and I do not seem to be able to modify the font in this program.

I've always used R to write PDFs and then opened them in Inkscape. When you open the PDF there is a checkbox related to importing text as actual text or outlines and regarding how to pick the font.

I've had issues with fonts not recognized by Inkscape but they were in dfont format (a Mac-specific font container). As long as the font is available in TTF form, Inkscape should be able to read it. If you can use Cambria in Inkscape (with the text tool in a new document, not an imported graph), then Inkscape knows about it and should be able to import it from the PDF.

PS: related to this: when saving the PDF, set argument: useDingbats=FALSE, otherwise points are actually written as character in a dingbats font which Inkscape might not know about and it will show you the letter "q" instead of the points.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/
Send me large files at: http://jo.irisson.com/upload/

Ellen Pape

unread,
Jun 30, 2012, 8:21:29 AM6/30/12
to Jean-Olivier Irisson, ggp...@googlegroups.com
Hello Jean-Olivier

Thanks for the tip! I'll certainly try it out..

cheers
ellen


Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

Martin Rittner

unread,
Jul 5, 2012, 6:54:23 AM7/5/12
to ggp...@googlegroups.com
Hi,

I just tried what you are describing: when importing the pdf into
inkscape, there is an option "import text as text" - ticking that, I can
edit the text in my graph without issue..

I tried this in Inkscape 0.48.2 on Linux Mint.

Also: you can save svg directly with svg. It will not export the text as
text though, you will need to get the font right in ggplot.

png is a raster format, you can't edit the font in there. It's fine for
presentations, but less well suited for a thesis (unless you get the
fonts right in ggplot and save the png in a very high resolution, but
that will hog up memory, i.e. word will become slow/unstable).

Hope, that helps,
Martin

Shicheng Guo

unread,
Jun 6, 2016, 4:49:48 PM6/6/16
to ggplot2, ellen...@gmail.com, baptist...@googlemail.com
Hi baptiste, 

I also do the way like you, However, each time I edit the figure with illustrator, the point size and line width will become very tinny, do you know why?


Thanks. 


Shicheng
Reply all
Reply to author
Forward
0 new messages