昨天我问这个关于存储情节的问题在一个对象内。我尝试实现第一种方法(意识到我没有指定我在原始问题中使用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() 

在这种情况下我使用了样本编号,因此只要复制代码就会运行代码。我确实花了几个小时困惑这个,但我无法弄清楚出了什么问题。它编写第一组pdfs没有问题,所以我有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),但数据没有变化。当我检查我的绘图列表时,似乎所有这些都被最近的绘图覆盖(<=>对象除外)。

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

有帮助吗?

解决方案

好的,所以如果你的情节命令改为

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)

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

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当前值。

其他提示

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

参见Hadley的书,第4.4节:

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

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

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

pltList[[pltName]]

pltList[pltName]

注意:

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

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

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

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

关于第二个问题:多页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()会返回一个可以存储和检索的对象。

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

在此相关问题上查看

scroll top