绘图大小 - 在IPython Notebook中使用ggplot2(通过rmagic) [英] Plot Size - Using ggplot2 in IPython Notebook (via rmagic)

查看:132
本文介绍了绘图大小 - 在IPython Notebook中使用ggplot2(通过rmagic)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始将R用法集成到Notebook中,从我的角度来看,它是两全其美的(Python中的数据管理,同时利用R的比较分析/图形优势)。不幸的是,我挂在一个看似简单的元素上,为ggplot2图形调整绘图大小。调整绘图大小对于熊猫来说非常简单,在纯粹的R环境(如RStudio)中,我可以使用dev.new()或PNG()等调整绘图。但是,试图做到这一点的是笔记本电脑让我的电脑变得疯狂(我在华硕U46E上运行Ubuntu 13.04)。此外,这是至关重要的,我想保持内联图形,以便我可以将整个脚本全部传递给我的同事。



当尝试dev.new()时,我的电脑被锁定,我不得不切换到另一个虚拟终端来重新启动。我试图调整x11()选项,但当图形变得有点失常时,浏览器暂时无响应。最终,我再次获得控制权,但我不知道为什么会发生这种情况。

有谁知道为什么会发生这种情况?此外,有没有人知道如何调整从IPython Notebook内渲染的ggplot2对象的图大小?恐怕我不能共享这些数据,但我可以告诉你,我试图绘制由基金中心(一个分类变量)构成的三个数值变量。这些情节确实按照书面执行......直到我试图调整大小。这是我的示例代码:

  %% R 
#x11(width = 500,height = 300)< <没有工作
#dev.new()<<尝试设置大小参数之前,它锁定了我的笔记本电脑
#Plot按单位
打印('*****费用由单位*****')
print( smu)
print(ggplot(smu,aes(x = fy,y = as.numeric(as.character(totexp)),group = fund,color = fund))+ geom_line(size = 2)+
ggtitle('Total Expenses'))
#Plot费用组件
print(ggplot(smu,aes(fy))+
geom_line(aes(y = as.numeric(as (颜色='固定',group =基金,颜色=基金))+
geom_line(aes(y = as.numeric(as.character(var)),color ='variable' ,group = fund,color = fund))+
geom_bar(aes(y = as.numeric(as.character(incadj)),group = 1),stat ='identity')+
facet_grid (。〜fund)+
ggtitle('Expenditure Components'))


解决方案

rmagic命令具有可选参数来指定图的大小。默认值是480像素的宽度和高度。因此,下面的代码复制了默认设置:

  %% 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)

下面的代码创建一个宽度为50厘米,高度为25厘米的图:

  %% 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 aes(x = x,y = y,color = lab))+ geom_point()
print(x)

您还可以以英寸或毫米为单位指定尺寸。


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'))

解决方案

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.

这篇关于绘图大小 - 在IPython Notebook中使用ggplot2(通过rmagic)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆