12

我昨天问了这个关于在对象中存储图的问题。我尝试实施第一种方法(意识到我没有qplot()在原始问题中指定我正在使用)并注意到它没有按预期工作。

library(ggplot2)               # add ggplot2

string = "C:/example.pdf"      # Setup pdf
pdf(string,height=6,width=9)

x_range <- range(1,50)         # Specify Range

# Create a list to hold the plot objects.
pltList <- list()
pltList[]

for(i in 1 : 16){

# Organise data 
y = (1:50) * i * 1000                       # Get y col
x = (1:50)                                  # get x col
y = log(y)                                  # Use natural log

# Regression
lm.0 = lm(formula = y ~ x)                  # make linear model
inter = summary(lm.0)$coefficients[1,1]     # Get intercept
slop = summary(lm.0)$coefficients[2,1]      # Get slope

# Make plot name
pltName <- paste( 'a', i, sep = '' )

# make plot object    
p <- qplot(
    x, y,   
    xlab = "Radius [km]", 
    ylab = "Services [log]",
    xlim = x_range,
    main = paste("Sample",i)
) + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)        

print(p)     

pltList[[pltName]] = p       
}

# close the PDF file
dev.off() 

在这种情况下,我使用了示例编号,因此如果它只是被复制,代码就会运行。我确实花了几个小时对此感到困惑,但我无法弄清楚出了什么问题。它可以毫无问题地编写第一组 pdf,因此我有 16 个带有正确绘图的 pdf。

然后当我使用这段代码时:

string = "C:/test_tabloid.pdf"
pdf(string, height = 11, width = 17)

grid.newpage()
pushViewport( viewport( layout = grid.layout(3, 3) ) )

vplayout <- function(x, y){viewport(layout.pos.row = x, layout.pos.col = y)}

counter = 1

# Page 1
for (i in 1:3){    
    for (j in 1:3){     
         pltName <- paste( 'a', counter, sep = '' )   
         print( pltList[[pltName]], vp = vplayout(i,j) )
         counter = counter + 1
     }
 }

 dev.off()

我得到的结果是abline每个图表上的最后一个线性模型线 ( ),但数据没有改变。当我检查我的情节列表时,似乎所有这些都被最近的情节覆盖(abline对象除外)。

一个不太重要的次要问题是如何生成一个多页 pdf,每页上有几个图,但我的代码的主要目标是将图存储在一个列表中,以便以后访问。

4

5 回答 5

11

好的,所以如果您的绘图命令更改为

p <- qplot(data = data.frame(x = x, y = y),
           x, y,   
           xlab = "Radius [km]", 
           ylab = "Services [log]",
           xlim = x_range,
           ylim = c(0,10),
           main = paste("Sample",i)
           ) + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)           

然后一切都按预期工作。这就是我怀疑正在发生的事情(尽管哈德利可能会澄清一些事情)。当 ggplot2 “保存”数据时,它实际上所做的是保存数据框和参数的名称。所以对于我给出的命令,你得到

> summary(pltList[["a1"]])
data: x, y [50x2]
mapping:  x = x, y = y
scales:   x, y 
faceting: facet_grid(. ~ ., FALSE)
-----------------------------------
geom_point:  
stat_identity:  
position_identity: (width = NULL, height = NULL)

mapping: group = 1 
geom_abline: colour = red, size = 1 
stat_abline: intercept = 2.55595281266726, slope = 0.05543539319091 
position_identity: (width = NULL, height = NULL)

但是,如果您未data在 qplot 中指定参数,则所有变量都会在当前范围内进行评估,因为没有附加(读取:已保存)数据框。

data: [0x0]
mapping:  x = x, y = y
scales:   x, y 
faceting: facet_grid(. ~ ., FALSE)
-----------------------------------
geom_point:  
stat_identity:  
position_identity: (width = NULL, height = NULL)

mapping: group = 1 
geom_abline: colour = red, size = 1 
stat_abline: intercept = 2.55595281266726, slope = 0.05543539319091 
position_identity: (width = NULL, height = NULL)

因此,当第二次生成绘图时,它不使用原始值,而是使用 和的当前值。xy

于 2009-11-30T20:14:08.283 回答
5

我认为您应该使用 in 中的data参数qplot,即将您的向量存储在数据框中。

参见 Hadley 的书,第 4.4 节:

对数据的限制很简单:它必须是一个数据框。这是限制性的,与 R 中的其他图形包不同。Lattice 函数可以采用可选的数据框或直接从全局环境中使用向量。...

数据作为副本存储在绘图对象中,而不是参考。这有两个重要的后果:如果您的数据发生变化,则绘图不会;和 ggplot2 对象是完全独立的,因此它们可以保存()到磁盘,然后加载()并绘制,而不需要该会话中的任何其他内容。

于 2009-11-30T19:38:20.790 回答
2

您的代码中有一个关于列表下标的错误。它应该是

pltList[[pltName]]

不是

pltList[pltName]

笔记:

class(pltList[1])
[1] "list"

pltList[1] 是一个包含 pltList 的第一个元素的列表

class(pltList[[1]])
[1] "ggplot"

pltList[[1]] 是 pltList 的第一个元素

于 2009-11-30T16:42:42.543 回答
1

对于您的第二个问题:多页 pdf 很简单——请参阅help(pdf)

 onefile: logical: if true (the default) allow multiple figures in one
          file.  If false, generate a file with name containing the
          page number for each page.  Defaults to ‘TRUE’.

对于您的主要问题,我不明白您是否要将绘图输入存储在列表中以供以后处理,或者绘图输出。如果是后者,我不确定plot()返回一个可以存储和检索的对象。

于 2009-11-30T16:07:10.920 回答
1

关于第二个问题的另一个建议是使用 Sweave 或 Brew,因为它们可以让您完全控制如何显示多页 pdf。

看看这个相关的问题

于 2009-11-30T16:44:49.403 回答