2011-11-02 34 views
3

我有這樣的R代碼裏面,它完美的作品裏工作,我得到同積位圖,如果我R中控制檯或作爲RSCRIPT格地塊不是一個函數

library(DBI); 
library(RMySQL); 
library(brew); 
library(lattice); 
con <- dbConnect(MySQL(),server credentials) 
x <- dbGetQuery(con,"SELECT name, distance FROM distances") 
bitmap("/tmp/dist_6078.bmp") 
dotplot(x$distance~x$name, col='red', xlab='name', ylab='distance', main='Distance plot') 
dev.off() 

問題是運行它,我是如果我附上<%和%>之間的所有內容並使用brew庫,則會獲得空白圖像。一切工作正常,如果我使用基本的R圖,問題只有當我使用格。

+3

你嘗試保存的情節中的變量,'p',然後打印它,'打印(對)'? – joran

+7

[R FAQ 7.22](http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f)以及http:// stackoverflow的可能重複.com/q/5450257 /和http://stackoverflow.com/q/6783120/ – Aaron

+0

是的,它在添加print()後有效。謝謝您的幫助 – SAN

回答

6

從R FAQ 7.22

格子功能,諸如xyplot()創建一個圖形對象,但不要 顯示它(同樣是GGPLOT2圖形也是如此,和網格圖形 在S-加)。用於圖形對象的print()方法產生 實際顯示

工作碼

library(DBI); 
library(RMySQL); 
library(brew); 
library(lattice); 
con <- dbConnect(MySQL(),server credentials) 
x <- dbGetQuery(con,"SELECT name, distance FROM distances") 
bitmap("/tmp/dist_6078.bmp") 
plot_obj <- dotplot(x$distance~x$name, col='red', xlab='name', ylab='distance', main='Distance plot') 
print(plot_obj) 
dev.off()