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/dev.off 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/dev.off 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.

Question!

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?

ggplot graphs in publications?

The grey background and/or default choice of colours for groups makes a ggplot graph stand out to any R user when seen in a presentation. But ggplot graphs get all ninja when it comes to publications, either that or not a lot of graphs generated using ggplot have been published in the journal I read (health research (epidemiology/diabetes largely). Like many others ggplot is my go to graphing package and I’ve prepared graphs using ggplot that have been published in the past.

Continue reading

Latent class mixed models – with graphics

UPDATE – July 2017

The site I was originally using to store the script and data has since closed, have migrated to GitHub, and updated the R code to remove deprecated ggplot code etc.

R code – https://github.com/nzcoops/etc
.csv file – https://raw.githubusercontent.com/nzcoops/etc

Foreword

Apologies that the output isn’t showing well as far as spacing and alignment is concerned. Am working on fixing that!

Background

Generally when we have a set of data, we have known groupings. Be that three different treatment groups, two sex groups, 4 ethnicity groups etc. There is also the possibility of unobserved groupings within your data, some examples (I’m clutching at straws here) are those who are vegetarians and those who aren’t, those who regularly exercise and those who don’t, or those who have a family history of a certain condition and those who don’t (assuming those data were not collected). There is an approach to look into this.

Continue reading

2011 Perth City to Surf Stats

Like every year, August sees the thousands taking part in the Perth City to Surf, and with that comes the chance for some stats. Why? Curiosity more than anything, and to convince myself that my time in the 12km run of 1 hour 9 mins and 36 seconds wasn’t so bad given I came down with the dreaded man flu just 36 hours prior to the race starting.

Despite the papers saying around 42,000 competed, the official results site lists results for 32,243 (excluding <10 people listed as NA for age and sex). 15,424 males (45.1%) and 18,791 females (54.9%)*.

Continue reading

gridExtra – Multiple plots from ggplot2

Thanks to this great post http://www.imachordata.com/?p=730 we can now put multiple plots on a display with ggplot2. This provides somewhat similar functionality to ‘par(mfrow=c(x,y))’ which would allow multiple plots with the base plot function. gridExtra doesn’t have quite the same level of options as ‘par’, but the syntax is simple.

grid.arrange( graph1, graph2, ncol=2

Simple. ‘grid.table’ is worth checking out as well.

NOTE: Saving

I normally use ‘ggsave(…)’ to save my plots created with ggplot2. This (as yet) doesn’t work with using grid.arrange. A work around is to use a more tradition/base approach:

pdf("filename.pdf")
grid.arrange( graph1, graph2, ncol=2)
dev.off()

Enjoy!

Package available here: http://cran.r-project.org/web/packages/gridExtra/