Printing ggplot2 to WMF

26 views
Skip to first unread message

Jason Rupert

unread,
Mar 26, 2009, 9:39:45 AM3/26/09
to ggp...@googlegroups.com

Given current workflow and customer needs it would be really great if I could print out ggplot2 images as WMF. My example code is shown below. Please note, that I am able to successfully print to WMF outside of a loop, but for some reason inside a for loop the images are all blank (easy to reproduce this by commenting out the for loop stuff). Any thoughts or suggestions as it would be really helpful if I figured out what I am doing wrong?

Thanks again.

# Loop code that does not work.
VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')
for(ii in 1:5)
{
VADeaths_title<-paste("C:/Images/VADeaths", ii,"_%02d.wmf", sep = "")
# Produce a Windows Meta File Format Plot
win.metafile(file=as.character(VADeaths_title), pointsize = 10)

counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(Data), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")
qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) +
facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) +
opts(strip.text.x = theme_text(colour = "green", size=14))

dev.off()
}



Elaine...@bd.com

unread,
Mar 26, 2009, 9:49:25 AM3/26/09
to jasonk...@yahoo.com, ggp...@googlegroups.com

Jason -
  I think you just need to add print() around your plot function.  (This is also the case for lattice).  -Elaine

<><><><><><><><><><><>
Elaine McVey
Statistician
BD Technologies
(919) 597-6136
Elaine...@bd.com
<><><><><><><><><><><>



From: Jason Rupert <jasonk...@yahoo.com>
To: ggp...@googlegroups.com
Date: 03/26/2009 09:40 AM
Subject: Printing ggplot2 to WMF



******************************************************************* IMPORTANT MESSAGE FOR RECIPIENTS IN THE U.S.A.: This message may constitute an advertisement of a BD group's products or services or a solicitation of interest in them. If this is such a message and you would like to opt out of receiving future advertisements or solicitations from this BD group, please forward this e-mail to optout...@bd.com. ******************************************************************* This message (which includes any attachments) is intended only for the designated recipient(s). It may contain confidential or proprietary information and may be subject to the attorney-client privilege or other confidentiality protections. If you are not a designated recipient, you may not review, use, copy or distribute this message. If you received this in error, please notify the sender by reply e-mail and delete this message. Thank you. ******************************************************************* Corporate Headquarters Mailing Address: BD (Becton, Dickinson and Company) 1 Becton Drive Franklin Lakes, NJ 07417 U.S.A. *******************************************************************

Felipe Carrillo

unread,
Mar 26, 2009, 10:40:57 AM3/26/09
to ggp...@googlegroups.com, jasonk...@yahoo.com

Not sure if this is doing what you want but I am able to print your graph as win.metafile. I think you have to use the 'print' statement before saving your graphics.

# Loop code that does not work.
VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')
for(i in 1:5)
{
VADeaths_title<-paste("C:/Images/VADeaths", i,"_%02d.wmf", sep = "")
# Produce a Windows Meta File Format Plot
#win.metafile(file=as.character(VADeaths_title), pointsize = 10)

counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(Data), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")
myplot <- qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) +
facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) +
opts(strip.text.x = theme_text(colour = "green", size=14))
win.metafile(filename="VADeaths.wmf",width=8.3,height=6.9,pointsize=9)
print(myplot)
dev.off()
}

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA


--- On Thu, 3/26/09, Jason Rupert <jasonk...@yahoo.com> wrote:

> From: Jason Rupert <jasonk...@yahoo.com>
> Subject: Printing ggplot2 to WMF

Jason Rupert

unread,
Mar 26, 2009, 11:09:49 AM3/26/09
to ggp...@googlegroups.com

In order to keep the scales on plots for multiple data sets it appears that I need to add an empty bin in the bar plot.

I tried to do this in the example below by expanding the range, i.e. range(c(Data, max_range)) where max_range is the desired max x-scale, however apparently this had no effect. I hoped it would have added an "empty" bin from 80-100.

Thanks for your help.


VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')

max_range<-100
counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 20)), Person), nrow)


names(counts) <- c("bin", "person", "n")

print(qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) + facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) + opts(strip.text.x = theme_text(colour = "green", size=14)))


Jason Rupert

unread,
Mar 26, 2009, 11:10:27 AM3/26/09
to Elaine...@bd.com, ggp...@googlegroups.com

This worked great!

Thank you for your help.


--- On Thu, 3/26/09, Elaine...@bd.com <Elaine...@bd.com> wrote:

> From: Elaine...@bd.com <Elaine...@bd.com>
> Subject: Re: Printing ggplot2 to WMF
> To: jasonk...@yahoo.com
> Cc: ggp...@googlegroups.com
> Date: Thursday, March 26, 2009, 8:49 AM
> Jason -
>
> I think you just need to add
> print() around your plot function. (This is also the case
> for lattice).
> -Elaine
>
>
>
> <><><><><><><><><><><>
>
> Elaine McVey
>
> Statistician
>
> BD Technologies
>
> (919) 597-6136
>
> Elaine...@bd.com
>
> <><><><><><><><><><><>
>
>
>
>
>
>
>
>
> From:
> Jason Rupert <jasonk...@yahoo.com>
>
> To:
> ggp...@googlegroups.com
>
> Date:
> 03/26/2009 09:40 AM
>
> Subject:
> Printing ggplot2 to WMF
>
>
>
>
>
>
>
>
>
>
>
>
> Given current workflow and customer needs it would be
> really great if I
> could print out ggplot2 images as WMF. My example code is
> shown below.
> Please note, that I am able to successfully print to WMF
> outside
> of a loop, but for some reason inside a for loop the images
> are all blank
> (easy to reproduce this by commenting out the for loop
> stuff). Any
> thoughts or suggestions as it would be really helpful if I
> figured out
> what I am doing wrong?
>
>
>
> Thanks again.
>
>
>
> # Loop code that does not work.
>
> VADeaths_flat_df = stack(as.data.frame(VADeaths))
>
> names(VADeaths_flat_df) =
> c('Data','Person')
>
> for(ii in 1:5)
>
> {
>
> VADeaths_title<-paste("C:/Images/VADeaths",
> ii,"_ d.wmf",
> sep = "")
>
> # Produce a Windows Meta File Format Plot
>
> win.metafile(file=as.character(VADeaths_title), pointsize =
> 10)
>
>
>
> counts <- ddply(VADeaths_flat_df, .(cut(Data,
> breaks=fullseq(range(Data),
> 20)), Person), nrow)
>
> names(counts) <- c("bin", "person",
> "n")
>
> qplot(person, n, data = counts, fill = person,
> geom="bar", stat="identity",
> width = 0.9)
>
> facet_grid(. ~ bin) opts(axis.text.x = NULL)
> opts(axis.text.y = theme_text(colour
> = "red"))
>
> opts(strip.text.x = theme_text(colour = "green",
> size=14))
>
>
>
> dev.off()
>
> }
>
>
>
>
>
>
>
>
>
> >
>
>
>
>
>
>
>
>

hadley wickham

unread,
Mar 26, 2009, 11:32:27 AM3/26/09
to jasonk...@yahoo.com, ggp...@googlegroups.com
Why not just use xlim() to set the limits of the x axis?
Hadley
--
http://had.co.nz/

Jason Rupert

unread,
Mar 26, 2009, 12:26:12 PM3/26/09
to hadley wickham, ggp...@googlegroups.com

Hadley,

Thank you for the suggestion.

It appears I am having an implemention issue. I attempted to use xlim() with the example shown below, but I received several warning messages and the data does not even appear in the plot. Is the placement of the xlim() important in the string of arguments going into qplot?

Also, it appears that xlim() is the cause of the data not appearing, since I comment it out and the data comes back.

Thank you again for any suggestions you or others may have about adding an additional bin group or extending the x-axis limits to include some empty bins.



VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')
ii<-1

counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(Data), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")

qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) + facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) + opts(strip.text.x = theme_text(colour = "green", size=14)) + xlim(0, 100)


--- On Thu, 3/26/09, hadley wickham <h.wi...@gmail.com> wrote:

> From: hadley wickham <h.wi...@gmail.com>
> Subject: Re: Need to add an empty bin group to bar plot
> To: jasonk...@yahoo.com
> Cc: ggp...@googlegroups.com

hadley wickham

unread,
Mar 26, 2009, 12:28:44 PM3/26/09
to jasonk...@yahoo.com, ggp...@googlegroups.com
Hi Jason,

Since your x values are discrete, you need

xlim("(0,20]", "(20,40]", "(40,60]", "(60,80]", "(80,100]")

Hadley
--
http://had.co.nz/

Jason Rupert

unread,
Mar 26, 2009, 12:43:20 PM3/26/09
to hadley wickham, ggp...@googlegroups.com

This still doesn't seem to be doing the trick? Maybe there is a terminology problem. Below I am trying exactly what you are suggesting, but it seems to wipe out the data in the plot. Also, I am trying to change the bin range and not the x-axis data.

Seems like maybe there should be a way to change that Northern axis scale, but all that comes to mind is changing the range. The xlim, seems to only be suited for changing the Southern axis scale and not the facet extents.

Does this help?

Thanks again.


VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')
ii<-1
counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(Data), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")
#print(
qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) +
facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) +
opts(strip.text.x = theme_text(colour = "green", size=14)) + xlim("(0,20]", "(20,40]", "(40,60]", "(60,80]", "(80,100]")
Reply all
Reply to author
Forward
0 new messages