How can I change the size (height/width) of the graph?

1,454 views
Skip to first unread message

Miao

unread,
May 21, 2013, 11:45:25 PM5/21/13
to ggp...@googlegroups.com
Hi, 

   How can I change the width and height of the graph (not just changing the aspect ratio), with approximately fixed figure/symbol size? 

   I would like to fit the graph to a A4 page with height > width. If I just change the aspect ratio, the resulting graph would be with very big left/right margins, which must go with the major graph area when I copy the graph. 

   Thanks, 
Miao 

library(ggplot2)
k<-0.3
y<-1:100
df1<-data.frame(x=sin(k*y), y=y)
ggplot(df1, aes(x = x, y = y))+geom_point()+theme(aspect.ratio=1.8)

Dennis Murphy

unread,
May 22, 2013, 2:09:21 AM5/22/13
to Miao, ggplot2
See either ?ggsave or the graphics device of interest, e.g., ?pdf,
?jpg, ?png. If you choose not to use ggsave(), don't forget that you
need to wrap a print() function around your ggplot when using a
graphics device function (R-FAQ 7.22).


Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Gray, Wayne

unread,
May 22, 2013, 7:53:05 AM5/22/13
to Dennis Murphy, Miao, ggplot2
I would recommend using RStudio as that provides a GUI interface to these things and makes it easy to preview and postview the results.

Wayne

jpm miao

unread,
May 23, 2013, 11:35:59 AM5/23/13
to Gray, Wayne, Dennis Murphy, ggplot2
Thanks, Dennis and Gray. I use RStudio and its GUI interface, but I am not aware that I can just adjust the width and height when previewing until I see this message. Copying graphs from the GUI in RStudio  seems to offer the best resolution compared with other methods (e.g., ggsave, tiff) when a graph needs to be pasted in Word.

(If I am wrong, please correct me)


2013/5/22 Gray, Wayne <gr...@rpi.edu>

Felix Lankester

unread,
May 23, 2013, 2:24:12 PM5/23/13
to ggplot2
Hi All,

I am totally new to ggplot, and am using it with R Studio.

I am able to create basic graphs, but when i want to add titles and I follow the hints on sites like R Cookbook, I get error messages.

eg. I have created a line graph with two lines. The first line is from a column of data "prop_vacc_sick", and the second line is from a column of data "prop_unvacc_sick".
All is fine, but when I want to add a title and use any of the prescribed methods, eg + labs(title="xxx") or + ggtitle(" xxx"), i get a message: error: could not find function "ggtitle"

HERES THE CODE:
ggplot(mcf_clin.master) + geom_line(aes(x = days.pre..post.challenge, y=prop_vacc_sick)) +
+ geom_line(data = mcf_clin.master, aes(x = days.pre..post.challenge, y=prop_unvacc_sick),
+ colour = 'red', size = 1) + labs(title="Trial 1", x="Days pre- / post-challenge", y="Proportion sick")
> + ggtitle("Average bill for 2 people")

Error: could not find function "ggtitle"

Similarly if I try to create a legend, I get error messages such as ....

Error: could not find function "theme"
Error: could not find function "guides"

I am wondering if ggplot2 has loaded properly as when I installed it there were error messages...

Warning in install.packages :
cannot open: HTTP status was '410 Gone'
Warning in install.packages :
unable to access index for repository http://cran.ru.ac.za/bin/macosx/leopard/contrib/2.12
Warning in install.packages :
package ‘ggplot2’ is not available

Loading required package: reshape
Loading required package: plyr

Attaching package: 'reshape'

The following object(s) are masked from 'package:plyr':

rename, round_any

Loading required package: grid
Loading required package: proto
Warning messages:
1: package 'ggplot2' was built under R version 2.12.1
2: package 'reshape' was built under R version 2.12.1
3: package 'plyr' was built under R version 2.12.2
4: package 'proto' was built under R version 2.12.2

ANY HELP MOST APPRECIATED,

FELIX



Doug Mitarotonda

unread,
May 23, 2013, 3:28:36 PM5/23/13
to Felix Lankester, ggplot2
Looks like you are running a very old version of R (2.12). I doubt functions like ggtitle were around back then. I would suggest upgrading to 3.0.1 and then updating all of your packages.
Reply all
Reply to author
Forward
0 new messages