Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Scale plot elements within PDF of set width and height

Tags:

plot

r

pdf

ggplot2

Though R plots sent to a PDF can be rescaled at will in an illustration or page layout software, scientific journals often insist that the plots provided have specific dimensions.

Can the size of all plotting elements be scaled within a given PDF size directly in R?

require(ggplot2)

p <- qplot(data=iris,
           x=Petal.Width,
           y=Petal.Length,
           colour=Species)

pdf("./test_plot_default.pdf")
print(p)
graphics.off()

Produces adequate scaling of plot elements

test plot with default pdf size

However, changing PDF size elements does not cause the plot elements to scale. With smaller PDFs, the plotting elements are overly enlarged compared to the plotting space.

pdf("./test_plot_dimentionsions required by journal.pdf", width=3, height=3)
print(p)
graphics.off()

enter image description here

Using @Rosen Matev suggestion:

update_geom_default("point", list(size=1))
theme_set(theme_grey(base_size=6))
pdf("./test_plot_dimentionsions required by journal.pdf", width=3, height=3)
print(p)
graphics.off()

enter image description here

like image 243
Etienne Low-Décarie Avatar asked Jan 31 '14 16:01

Etienne Low-Décarie


2 Answers

Oddly enough, you can do this with scale= in ggsave(...)

require(ggplot2)
p <- qplot(data=iris, x=Petal.Width, y=Petal.Length, colour=Species)
ggsave("test.1.pdf",p)
ggsave("test.2.pdf",p, width=3, height=3, units="in", scale=3)

Try playing with the scale parameter and see what you get...

like image 179
jlhoward Avatar answered Nov 14 '22 19:11

jlhoward


Consider using width, height and pointsize in pdf() function.

If you like to stick with using pdf() instead of other ways like using sweave, then you would better use pdf function with more arguments, like bellow (I do not have ggplot2 installed, so simillar case is supplied).

# making scaled plot in pdf
# using paper=a4 just to see the efect

for (sc in c(0.5,0.75,1)) {
pdf(width=7*sc,height=7*sc,pointsize=12*sc,file=paste("scale",sc,".pdf",sep=""),paper="a4")
plot(sin((1:314)/100),main=paste("PDF sc",sc))
dev.off()  
}

It is quite usable, but works in some extent. Once the scale is too small, pdf will start to enforce linewidth, fontsize etc.

See the results in scale*.pdf created by example.

And for ggplot2 ...

sc <- c(0.5,0.75,1)
fi <- c("pic1.pdf","pic2.pdf","pic3.pdf")

require(ggplot2)
p <- qplot(data=iris,
x=Petal.Width,
y=Petal.Length,
colour=Species)

for (i in 1:3) {
pdf(width=7*sc[i],height=7*sc[i],pointsize=12*sc[i],file=fi[i])
print(p)
dev.off()  
}

Latex code to test the files how they look in one document:

\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphics}

\begin{document}

\begin{figure}
     \includegraphics{pic1.pdf}
 \end{figure}

\begin{figure}
     \includegraphics{pic2.pdf}
\end{figure}

\begin{figure}
    \includegraphics{pic3.pdf}
\end{figure}

\end{document}
like image 7
Petr Matousu Avatar answered Nov 14 '22 18:11

Petr Matousu