未显示绘图,并且未在R Studio中生成PDF文件

时间:2016-06-09 01:02:26

标签: r rstudio

#Hi当我在RStudio中运行这段代码时,我无法在绘图窗口中并排看到4个图。我也无法在R的工作目录中看到名为“fig_more_anscombe.pdf”的pdf文件。我在桌面上有一个R的工作目录,文件夹名为“Adv Data Sciences”

# The Anscsombe Quartet in R

# demonstration data from
# Anscombe, F. J. 1973, February. Graphs in statistical analysis. 
#  The American Statistician 27: 17â21.

# define the anscombe data frame
anscombe <- data.frame(
   x1 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
   x2 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
   x3 = c(10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5),
   x4 = c(8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8),
   y1 = c(8.04, 6.95,  7.58, 8.81, 8.33, 9.96, 7.24, 4.26,10.84, 4.82, 5.68),
   y2 = c(9.14, 8.14,  8.74, 8.77, 9.26, 8.1, 6.13, 3.1,  9.13, 7.26, 4.74),
   y3 = c(7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73),
   y4 = c(6.58, 5.76,  7.71, 8.84, 8.47, 7.04, 5.25, 12.5, 5.56, 7.91, 6.89))

# show results from four regression analyses
with(anscombe, print(summary(lm(y1 ~ x1))))
with(anscombe, print(summary(lm(y2 ~ x2))))
with(anscombe, print(summary(lm(y3 ~ x3))))
with(anscombe, print(summary(lm(y4 ~ x4))))

# place four plots on one page using standard R graphics
# ensuring that all have the same scales
# for horizontal and vertical axes
pdf(file = "fig_more_anscombe.pdf", width = 8.5, height = 8.5)
par(mfrow=c(2,2),mar=c(3,3,3,1))
with(anscombe, plot(x1, y1, xlim=c(2,20),ylim=c(2,14), 
  pch = 19, col = "darkblue", cex = 2, las = 1))
title("Set I")

with(anscombe,plot(x2, y2, xlim=c(2,20),ylim=c(2,14), 
  pch = 19, col = "darkblue", cex = 2, las = 1))
title("Set II")

with(anscombe,plot(x3, y3, xlim=c(2,20),ylim=c(2,14), 
  pch = 19, col = "darkblue", cex = 2, las = 1))
title("Set III")

with(anscombe,plot(x4, y4, xlim=c(2,20),ylim=c(2,14), 
  pch = 19, col = "darkblue", cex = 2, las = 1))
title("Set IV")
dev.off()

par(mfrow=c(1,1),mar=c(5.1, 4.1, 4.1, 2.1))  # return to plotting defaults``

0 个答案:

没有答案