wom*_*ble 11 pdf statistics plot r ggplot2
我昨天问过这个问题,关于在一个物体中存储一个情节.我尝试实现第一种方法(意识到我没有指定我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每个图上的最后一个线性模型线(),但数据不会改变.当我检查我的绘图列表时,似乎所有这些都被最近的绘图覆盖(除了abline对象).
一个不太重要的次要问题是如何在每个页面上生成带有多个图的多页面PDF,但我的代码的主要目标是将图存储在我以后可以访问的列表中.
Jon*_*ang 10
好的,所以如果您的绘图命令更改为
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)
因此,当该地块产生第二次左右,而不是使用原始值,它采用了当前值x和y.
我认为您应该在中使用data参数qplot,即将向量存储在数据框中。
参见哈德利的书,第4.4节:
对数据的限制很简单:它必须是一个数据帧。这是限制性的,并且与R中的其他图形包不同。莱迪思的函数可以采用可选的数据帧,也可以直接从全局环境使用向量。...
数据作为副本而非参考存储在打印对象中。这有两个重要的后果:如果您的数据发生更改,则图将不会改变;和ggplot2对象完全是独立的,因此可以将它们保存()d到磁盘,然后进行load()和绘制,而无需该会话中的其他任何操作。