R script using ggplot2 does not produce usable pdf with pdf graphics device

197 views
Skip to first unread message

Faheem Mitha

unread,
Mar 6, 2012, 7:15:27 AM3/6/12
to ggp...@googlegroups.com

Hi,

Consider
############################################################
foo.R
############################################################
makegg <- function()
{
library(ggplot2)
pdf(file = "foo.pdf", width = 4, height = 4)
carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
vegLengths <- rbind(carrots, cukes)
ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha =
0.2)
dev.off()
}

makegg()
############################################################

This does not produce a usable pdf. However, if I remove the function and
put all statements in global scope, it works fine.

I'm using

Rscript foo.R

I'm on Debian squeeze with ggplot2 0.8.9 and R 2.14.2. What am I doing
wrong, if anything? Please cc me on any reply. Thanks.

Regards, Faheem

Faheem Mitha

unread,
Mar 6, 2012, 7:22:24 AM3/6/12
to ggp...@googlegroups.com

On Tue, 6 Mar 2012, Faheem Mitha wrote:

> Hi,
>
> Consider
> ############################################################
> foo.R
> ############################################################
> makegg <- function()
> {
> library(ggplot2)
> pdf(file = "foo.pdf", width = 4, height = 4)
> carrots <- data.frame(length = rnorm(100000, 6, 2))
> cukes <- data.frame(length = rnorm(50000, 7, 2.5))
> carrots$veg <- 'carrot'
> cukes$veg <- 'cuke'
> vegLengths <- rbind(carrots, cukes)
> ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)
> dev.off()
> }
>
> makegg()
> ############################################################
>
> This does not produce a usable pdf. However, if I remove the function and put
> all statements in global scope, it works fine.

Never mind, apparently one needs to wrap the call to ggplot in a print()
statement for some reason. http://cran.r-project.org/doc/FAQ/R-FAQ.html,
item 7.22. Mentioned in
http://stackoverflow.com/questions/5625394/problem-saving-pdf-file-in-r-with-ggplot2,
otherwise I might have spent more time looking.

Regards, Faheem

ONKELINX, Thierry

unread,
Mar 6, 2012, 9:18:27 AM3/6/12
to Faheem Mitha, ggp...@googlegroups.com
Or use ggsave() to create the pdf. Much easier.

makegg <- function()
{
library(ggplot2)


carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
vegLengths <- rbind(carrots, cukes)

p <- ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha =
0.2)
ggsave(p, file = "foo.pdf", width = 4, height = 4)
}

makegg()

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] Namens Faheem Mitha
Verzonden: dinsdag 6 maart 2012 13:22
Aan: ggp...@googlegroups.com
Onderwerp: Re: R script using ggplot2 does not produce usable pdf with pdf graphics device

Regards, Faheem

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

zekai...@netscape.net

unread,
Mar 6, 2012, 9:43:28 AM3/6/12
to fah...@faheem.info, ggp...@googlegroups.com
You can modify your code, as attached!


Zekai Otles

zekai...@aim.com


, 

otherwise I might have spent more time looking.

                                                          Regards, Faheem

-- 
You received this message because you are subscribed to the ggplot2 mailing 
list.
Please provide a reproducible example: http://gist.github.com/270442
usablePDF.R

Faheem Mitha

unread,
Mar 6, 2012, 1:14:02 PM3/6/12
to Matthew Krachey, ggp...@googlegroups.com

Hi Matthew,

Yes, I'm aware that (as Thierry Onkelinx also suggested) that I can use
ggsave, but I'm actually using the TikZ driver tikzdevice. I don't suppose
that ggsave is smart enough to call this if I was to pass "foo.tex" to
ggsave, though that would not be a bad feature-ette to have.

Thanks to everyone for all their replies.
Regards, Faheem

On Tue, 6 Mar 2012, Matthew Krachey wrote:

> This worked fine
> makegg <- function()
>  {
>  require(ggplot2)


> carrots <- data.frame(length = rnorm(100000, 6, 2))
> cukes <- data.frame(length = rnorm(50000, 7, 2.5))
>  carrots$veg <- 'carrot'
>  cukes$veg <- 'cuke'
>  vegLengths <- rbind(carrots, cukes)
>  ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)

>  ggsave("foo.pdf",width=4, height=4)
>  }
>
> makegg()
>
> Matthew

Reply all
Reply to author
Forward
0 new messages