Hacker News new | past | comments | ask | show | jobs | submit login
An Introduction on How to Make Beautiful Charts with R and ggplot2 (minimaxir.com)
87 points by minimaxir on Feb 12, 2015 | hide | past | favorite | 19 comments



Like many ggplot articles, they seem to consider the primary output being the Web, and not print publications.

One thing I haven't quite figured out with ggplot2 is how to keep the text size constant in publications across figures. If you look at professionally typeset books, you'll see that the figure text (say the labels in a chart) is the same as the text of the article itself. Even this article suffers from the problem (but obviously, that's because the text is in a PNG so it can't scale with the article text).

The only workaround I have is to manually set the width of the figure before exporting, but this is fairly inflexible. Perhaps tikz output is another option, but my understanding (possibly incorrect) is that it isn't maintained anymore.

Can anyone recommend a good workflow for charts in R and ggplot2 to handle this situation?


These are really nice plots for the web. For publication it's quite essential for me to produce pdf charts with the text rendered with LaTeX. I use python's matplotlib for that. Can ggplot do something like this? That would be really nice.


You can do it with ggplot2 using the 'extrafont' library and installing the CM fonts (after loading extrafont, do 'font_install("fontcm")'). Then it's just adding

  + theme(text=element_text(family="CM Roman"))
to your plot.

For me that doesn't work when displaying the graphs in a window, but it does work when exporting to PDF with ggsave. Afterwards you have to embed the CM font into the PDF with 'embed_fonts'.


The tikzGraphics package (http://www.rforge.net/doc/packages/tikzDevice/tikz.html) can convert R charts to LaTeX equivalents. Whether or not it actually works well with ggplot2 is TBD.


ggplot2 is a very elegant language for describing how to draw your data. Two great books for interested parties: the R Graphics Cookbook [0] which has tons of ggplot2 examples and gets you plotting immediately; and ggplot2: Elegant Graphics for Data Analysis [1] written by the author of the ggplot2 library and providing a very thorough description of this implementation of the "Grammar of Graphics".

[0] http://amzn.to/1AhyEdE

[1] http://amzn.to/1B4OSq9


This is great. I started learning R really just to become more effective at winning arguments about baseball and it's such a great tool (also for things other than baseball arguments).


Great, I was introduced to ggplot2 via your note in the footer in articles you've shared in the past. This helps a great deal. Thanks Max! :)

ps: Is there a way to generate vector outputs?


ggsave (and most R graphics devices) supports .pdf and .svg output. I remember reading an article about prerendering plots in ggplot2 then exporting into Illustrator for more professional touch ups.

Note that if you want to include custom fonts with those outputs, it gets really complicated.



Decent references. I find myself doing similar, final-draft touch-ups with plots for manuscripts.


I haven't played around with Inkscape, but I'll definitely be taking a look now!


Now that's clever!!!


I've had good success with .svg from ggplot2. The only issue I've seen is that the legend for continuous color maps is glitchy. Especially if you try to touch up the plot in Inkscape.


R has a built-in Cairo-based SVG output device that allows you to write out any plot in a vector format: http://www.inside-r.org/r-doc/grDevices/cairo. It's great for publication-quality plotting.


ggplot2 is great. I recently took this intro to ggplot2. A lot of the biologists in my lab go to my officemate for his R/ggplot2 plotting skills, to help create graphs for papers.

R takes a little getting used to but making graphs programmatically is pretty awesome.

The class materials and downloads. The wrap-up section has additional links:

http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html


I am going off-topic but I have a question for you.

I recently had a discussion with a friend of mine who's into academics (2nd PhD). He's highly proficient in LaTeX/Perl/R and typeset his own papers (for publishing and for conferences) but he mentioned that researchers shouldn't do that because this is too much time consuming and it could be better done by someone trained for that: a secretary. But budget cuts ended up forcing researchers, who can hardly type with one finger (and aren't trained to use LaTex/R/etc.) , to type out their papers.

Wouldn't it be better if researchers could just send a rough draft to a service that would typeset and make beautiful plots out of it ? Isn't your officemate (may I assume he is a researcher too ?) running the risk to spend too much time on formating papers (and God knows you can spend a lot of time in LaTeX and R docs) ?


My office is lab IT/ bioinformatics staff not researchers.

I think most people at this point type better than they write, also I think they compose while they write, making edits etc. Plus errors in transcriptions. I haven't seen anyone doing much formatting/ typesetting of the text, which you are right, could be done by anyone.


How are things progressing with ggplot for python? I am quite jealous of the syntax


ggplot for Python [0] is coming along nicely. My own recent use suggests that for basic plotting, most of the expected functionality is there. More advanced displays, such as faceting, are still in progress. Overall, I've been impressed.

[0] https://github.com/yhat/ggplot/




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: