%load_ext sage import os DATA = os.getcwd() + '/' ; DATA # ChromeではPDFをimgタグで表示できないので、pngに変換するように変更 2012/06/27 # # Rのグラフをsageで表示するためのユーティリティ関数 import time def preGraph(pdfFile): filename = DATA+pdfFile r.pdf(file='"%s"' %filename) return pdfFile def offGraph(): r.dev_off() def postGraph(pdfFile, fac=0.75): r.dev_off() width = int(640*fac) # html(''%(pdfFile, time.time(), width)) pngFile = convertPdf2Png(pdfFile) html(''%(pngFile, time.time(), width)) def getGraph(pdfFile, fac=0.75): width = int(640*fac) # return ''%(pdfFile, time.time(), width) pngFile = convertPdf2Png(pdfFile) return ''%(pngFile, time.time(), width) def convertPdf2Png(pdfFile): pngFile = pdfFile.replace(".pdf", ".png") result = os.popen("cd %s; convert -density 600 -geometry 1000 %s %s"%(DATA, pdfFile, pngFile)).read() # print result return pngFile from IPython.display import Image, display from IPython.display import display, Math def my_show(obj): return display(Math(latex(obj))) class ListTable(list): """ Overridden list class which takes a 2-dimensional list of the form [[1,2,3],[4,5,6]], and renders an HTML Table in IPython Notebook. """ def _repr_html_(self): html = [""] for row in self: html.append("") for col in row: html.append("".format(col)) html.append("") html.append("
{0}
") return ''.join(html) # table = ListTable() # table.append([getGraph(fig_3_7a, fac=0.5), getGraph(fig_3_7b, fac=0.5)]) # table from IPython.display import HTML # fig 11.1 fig11_1 = preGraph("fig11.1.pdf") r('attach(mtcars)') r('plot(wt, mpg, main="Basic Scatter plot of MPG vs. Weight", xlab="Car Weight (lbs/1000)", ylab="Miles Per Gallon ", pch=19)') r('abline(lm(mpg~wt), col="red", lwd=2, lty=1)') r('lines(lowess(wt,mpg), col="blue", lwd=2, lty=2)') postGraph(fig11_1, fac=0.6) os.system("convert -resize 400x fig11.1.png fig11.1b.png") display(Image('fig11.1b.png')) # fig 11.3 fig11_3 = preGraph("fig11.3.pdf") r('pairs(~mpg+disp+drat+wt, data=mtcars, main="Basic Scatter Plot Matrix")') postGraph(fig11_3, fac=0.6) os.system("convert -resize 400x fig11.3.png fig11.3b.png") display(Image('fig11.3b.png')) # 散布図がつぶれてしまったような場合の対処方法 r('set.seed(1234)') r('n <- 10000') r('c1 <- matrix(rnorm(n, mean=0, sd=.5), ncol=2)') r('c2 <- matrix(rnorm(n, mean=3, sd=2), ncol=2)') r('mydata <- rbind(c1, c2)') r('mydata <- as.data.frame(mydata)') r('names(mydata) <- c("x", "y")') fig11_7 = preGraph("fig11.7.pdf") r('with(mydata, plot(x, y, pch=19, main="Scatter Plot with 10,000 Observations"))') postGraph(fig11_7, fac=0.6) os.system("convert -resize 400x fig11.7.png fig11.7b.png") display(Image('fig11.7b.png')) # fig 11.8 fig11_8 = preGraph("fig11.8.pdf") r('with(mydata, smoothScatter(x, y, main="Scatterplot Colored by Smoothed Densities"))') postGraph(fig11_8, fac=0.6) os.system("convert -resize 400x fig11.8.png fig11.8b.png") display(Image('fig11.8b.png')) r('ftable(Titanic)') # fig 11.23 #r('install.packages("vcd")') r('library(vcd)') fig11_23 = preGraph("fig11.23.pdf") r('mosaic(Titanic, shade=TRUE, legend=TRUE)') postGraph(fig11_23, fac=0.6) os.system("convert -resize 400x fig11.23.png fig11.23b.png") display(Image('fig11.23b.png'))