Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot Size - Using ggplot2 in IPython Notebook (via rmagic)

I have started integrating R usage into Notebook to get, from my perspective, the best of both worlds (data management in python while exploiting the comparative analytical/graphical advantages of R). Unfortunately I am hung up on a seemingly easy element, adjusting plot size for ggplot2 graphics. Adjusting plot sizes is pretty straightforward with pandas, and in a purely R environment (like RStudio), I can adjust plots with dev.new() or PNG(), etc. However, attempting to do this is Notebook makes my computer go nuts (I am running Ubuntu 13.04 on an ASUS U46E). Furthermore, this is crucial, I want to keep the graphics inline so that I can pass the script in its entirety to my colleagues.

When trying dev.new(), my computer locked up and I had to switch to a different virtual terminal to reboot. I tried to adjust x11() options, my browser became temporarily unresponsive while the graphics went a bit haywire. Ultimately, I was granted control again, but I have no idea why this happened.

Does anyone know why this may have occurred? Additionally, does anyone know how to adjust the plot size of ggplot2 objects rendered from within IPython Notebook? I am afraid I can't share the data, but I can tell you that I was attempting to plot three numeric variables faceted by fund center (a categorical variable). These plots did execute as written ... until I tried to adjust the size. Here is my example code:

%%R
#x11(width=500,height=300) << didn't work
#dev.new() << tried before setting size parameters, and it locked up my laptop
#Plot total expenses by unit
print('*****Expenses by Unit*****')
print(smu)
print(ggplot(smu,aes(x=fy,y=as.numeric(as.character(totexp)),group=fund,colour=fund))+geom_line(size=2)+
        ggtitle('Total Expenses'))
#Plot expense components
print(ggplot(smu,aes(fy))+
      geom_line(aes(y=as.numeric(as.character(fixed)),colour='fixed',group=fund,colour=fund))+
      geom_line(aes(y=as.numeric(as.character(var)),colour='variable',group=fund,colour=fund))+
        geom_bar(aes(y=as.numeric(as.character(incadj)),group=1),stat='identity')+
        facet_grid(.~fund)+
        ggtitle('Components of Expenditure'))
like image 521
Marvin Ward Jr Avatar asked Jul 01 '13 21:07

Marvin Ward Jr


People also ask

How do I change the size of a plot in Jupyter notebook?

Next, to increase the size of the plot in the jupyter notebook use plt. rcParams[“figure. figsize”] method and set width and height of the plot.


1 Answers

The rmagic command has optional arguments to specify the size of the plot. The default is a width and height of 480 pixels. Thus, the code below replicates the default settings:

%%R -w 480 -h 480 -u px
library(ggplot2)
dat <- data.frame(x = rnorm(10), y = rnorm(10), 
                  lab = sample(c('A', 'B'), 10, replace = TRUE))
x <- ggplot(dat, aes(x = x, y = y, color = lab)) + geom_point()
print(x)

And this code below creates a plot with a width of 50 cm and a height of 25 cm:

%%R -w 50 -h 25 -u cm
library(ggplot2)
dat <- data.frame(x = rnorm(10), y = rnorm(10), 
                  lab = sample(c('A', 'B'), 10, replace = TRUE))
x <- ggplot(dat, aes(x = x, y = y, color = lab)) + geom_point()
print(x)

You can also specify the size in inches or millimeters.

like image 121
John Blischak Avatar answered Sep 27 '22 17:09

John Blischak