ggplot + powerpoint = wall + head … solution?

Confession, by ‘solution?’ I literally mean I’m asking for your thoughts on a solution.

Like it or lump it I do a lot of graphs for presentations, largely in powerpoint. That’s the way my colleagues/industry work(s) and it’s not about to change anytime soon. I have a real frustration with getting my plots out and into powerpoint.

What am I after?

A good workflow for exporting plots, the same dimensions each time, so that tick mark text, axis labels etc are readable and the plots are nice and sharp. I want the plot to take up about 75% of the slide (so leaving enough room for a heading). I want to be able to drag and drop the plots from a folder onto the slide.

What have I done in the past?

Numerous different things. Sometimes if it’s got to be done quick I’ll use the export in Rstudio and quickly resize. Or I’ll zoom in on Rstudio (resizing) and take a screenshot (v easy with a mac but the naming is naff). Or I’ll export them using a function and then resize within powerpoint. But THAT is what I am trying to avoid (well, perfect). Why, because it’s a pain. Because it’s inconsistent. If I am changing slides from a plot of males to a plot of females, I want the plot to be the EXACT same size sitting in the exact same spot on the next slide. Nothing irritates me more than plots bouncing around from slide to slide. And this is difficult and time consuming to do and get right within ppt.

Where am I up to with a workflow?

This is where I’m hoping someone can set me on the straight and narrow. I’m looking at ggsave and png/ as two options. What works well on the slide is a png that is about 400×400 pixels . This is 5.56×5.56 inches or 14.11×14.11cm at 72 dpi. That drops nicely on a slide leaving enough room for a heading.

With ggsave you can’t specify size in pixels(?), hence providing the values in the other units.

Where I have hit a wall is the ratio of text to plot size. ggsave seems to have things nice and sharp, but altering ‘scale’ or ‘dpi’ (no surprises based on their intention) changes the actual ‘size on the slide’ of the plot, given that ppt has to show it at 72 dpi. The problem with ggsave is that with w/h 14.11cm and dpi 72 then the size is right but it’s not sharp. png/ has the same problem. When I set w/h to 14.11cm and dpi to 300 the actual image (when at 72 dpi) is ~55cm. And powerpoint has to display it at 72 dpi, so it blows off the page.

What does kinda work?

If I create an image using ggsave w/h 14.11cm and dpi 300, open it in preview on the mac, it says that at 72 dpi it is ~55cm, I can then resize it down to 14.11cm (with the ‘resample image’ option UNchecked), I then get an image that is 14.11cm that is nice and sharp and drops into powerpoint nicely.


Is there a way to get R to resample the image on the way out? Is there a better way to do this (has to be!)? Would creating a theme where everything is smaller benefit the situation (I tend to think not).

I feel like I’m going about this all the wrong way. But, given that the ‘what does kinda work?’ above actually does get a nice sharp plot to just drag and drop onto the page, that means it is possible and it’s just about the workflow right, right?

About these ads

18 thoughts on “ggplot + powerpoint = wall + head … solution?

  1. I wonder what `getOption(“bitmapType”)` says? It sounds like you are one a Mac? If so you are probably using ‘quartz’ by default. The trouble you are facing that:

    “By default no resolution is recorded in the file. Viewers will often assume a nominal resolution of 72ppi when none is recorded.”

    Using the ‘cairo’ device (if your machine supports it, check `capabilities()`) might help. On my machine (Mac OS 10.8) ‘cairo’ correctly records the DPI in the file, giving you the expected size in Powerpoint (or any viewer that honours this information):

    png(‘test.png’, width=14.11, height=14.11, units=’cm’, res=300, type=’cairo’)
    plot(1,1) # or print(my_ggplot_object)

    • Brilliant, that was it. Thank you. So using

      ggsave(“test_ggsave_300.png”, width=14.11, height=14.11, unit=”cm”, dpi=300)

      Gave me an image I could drop into ppt that was the right size and nice and sharp.

      Ultimately what I wanted to get to was a theme_ppt() [for increased axis fonts etc] to use in conjunction with this for quick, clean, consistent graphing for ppt, but I wanted to know I could reliably export them sharply before working on that.

      On a side note, what is the best medium these days for questions like this? I didn’t really want to spam Rbloggers. stackoverflow is more for specific coding questions which this could have loosely fallen into, but for more general conversation/musings like this, where do people go these days?

      • Glad to help. Of course, there is probably a very good reason that cario is not the default device on Macs, so you might not want to use `options(bitmapType=”cairo”)`. I don’t ever use it myself, and don’t know it’s limitations (if any), I just happen to know that it would write the correct DPI information… Something like `ggsave(“test_ggsave_300.png”, width=14.11, height=14.11, unit=”cm”, type=”cairo”)` should work in this instance too.

        As someone has already pointed out, you might be best to go via a vector-based format. This way you don’t have to worry about DPI or ‘sharpness’ of the image and you get what you ask for when you request `(… width=x, height=y)`.

        If I were building a workflow as you describe my approach would be to generate a nice vector-based image (my preference would be for PDF) and then convert this to PNG. I would do this outside R, but if you wanted to automate it you could probably use system calls to `ImageMagic` (something like `convert -density 300 -units ‘PixelsPerInch’ in.pdf out.png` might work) or (only on a Mac) `sips` (don’t know the syntax off the top of my head…). Both of these tools ought to write the DPI information to file and give you the ‘size’ image you are after. At least this way you have the PDF (or equivalent vector image) which will be far more useful in the long run (esp. if you ever move away from PPT).

      • I will have to look into the broader differences between quartz and cairo. I like the idea of having a new function like ggppt where type=”cairo” is the default. There would have to be a good reason to generate a PDF first and then have to run another function/command to get the PNG. I think if PDFs insert fine with backward compatibility I’ll work with that, otherwise the cairo type is looking appealing.

        Often these graphs are very quickly put together, sometimes with a few iterations and used once, so would love to have minimal steps.

        Thanks again.

  2. You could export the image as an emf file (savePlot(“c:/plot123n.emf”,type=”emf”)), drop it into PPT where it would be a vector. I haven’t tried it with ggplot2 but used this way on a few occasions with svg files I exported as emf.

  3. I’m surprised that this question hasn’t been raised earlier. I write most of my presentations in Keynote as I much prefer the interface and I think that is it neater for aligning objects. Unfortunately, most of the systems I present on are not compatible with Keynote (iWork on iCloud could the be the answer to this). However, it is still a major pain to save plots from R, then import them into Keynote, resize and reposition them so that they don’t jump around the page. I would be keen to hear if there is a slicker way of exporting images from R to presentation software.

  4. Oh I know such problems…
    Have you tried it with PDF in ggsave? Just add it to the filename, ggsave figures out the rest. Microsoft used to fight PDF (sometimes displaying only the preview), but not any more, sigh. With PDF the dpi value becomes irrelevant in vector drawings, and you can select the appropriate width & height without it becoming fuzzy. I have not tested this :).

    • Thanks Michael. I am a fan of vector based art. I guess for things shown on a projector, even medium res images are normally fine, hence the tendency for png. I will have a play tomorrow with the PDF option, I’m running office 2011 on a mac, and most of these people I’m working with are on office 2003 on windows, so I’d have to check compatibility wise that an inserted PDF will work alright. I will post a summary post when I get time after I have this all sorted :)

  5. A general response re “alternatives to powerpoint”. As I said at the start, these simply aren’t an option. These are for presentations given by 60yo professors etc (absolutely no offence on account of age intended), being delivered in other states/countries on other peoples systems or at large conferences (where everything is ppt). I’d love to recommend another platform, but it’s just not an option, these aren’t tech savvy/new technology adopting people I’m working with (for this workflow). Full presentations in PDFs aren’t an option as these are generally presentations being edited on a plane/in a hotel last minute so I don’t have control over the final version. A lot of the people I work with are on government (hospital) computers that are still running office 2003, so there are compatibility issues to start with, but also platforms other than office aren’t even available, even html based would be dodgy (IE 7 only people) let alone that fact that my colleagues wouldn’t be able to edit them.

    I don’t want to labour this point, I’m fully supportive of new presentation platforms myself and dabble with them regularly. But feasibility wise, moving to another platform would be as well received as switching your grandparents 3210 out for an android touchscreen phone.

  6. What OS are you running on your own computer?

    When I use Windows, I get each plot the way I like it in plain Rgui and then save it as a WMF file. I insert each file into PowerPoint and resize them (not manually but by going into picture properties and setting the exact dimensions). Then, I ungroup them, which asks me if I want to turn the picture into a native PowerPoint object, and I tell it yes. The resizing step can probably be automated to some extent with VB, but I haven’t gotten around to it.

    When I use Linux, I wrote the following script to export the current plot to an EMF file which can then be copied to a Windows machine and processed as above. Sometimes natively generated WMFs give better results, sometimes EMFs do, but they both screw up the fonts FAR less than if you attempt to do this using the dev.copy2eps that comes with the grDevices base R package.

    ######## begin ########
    dev.copy2emf 0){
    system(paste(‘fig2dev -L emf ‘,file,’.fig ‘,
    ‘> ‘,file,’.emf’,sep=”));
    } else stop(‘This command only works for Linux with the transfig package installed. If you are running Linux, please make sure that the \’fig2dev\’ command exists and is in your path, then try again.’);
    ######## end ########

    As the error message above says, make sure to have fig2dev installed.

    If you’re running MacOS, I know nothing about that, so good luck. You could try looking for a MacOS port of fig2dev and then modifying the above script to use it.

  7. I don’t know what your “home” platform is (Windows, Mac, Linux?) but I’m pretty sure ImageMagick runs on both Windows and Macs and can do all kinds of image resizing / resampling (among many other things) from R code via ‘system( … )’. That’s how I’d approach it, but my home platform is Linux, where ImageMagick is often installed by default.

  8. When I need figures to be a certain size and have consistency (for scientific journals mostly) I use tiff().
    I just tested using a ggplot2 tutorial data set and this works with ggplot2 (I admit I haven’t used ggplot2 enough)

    qplot(data=myData,x=BM,main="Histogram of BodyMass")
    stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

    I use tiff because it is accepted/recommended by almost all journals. It also fits nicely into power point and the lzw compression keeps it pretty small. In powerpoint you can change the size of the slide which also allows you to see how big it is in inches or cm so you can adjust the tiff size accordingly.

  9. Pingback: ggplot + powerpoint = wall + head … solution? - R Project Aggregate

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s