本文介绍了ddply和ggplot - 不生成剧情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据如下:

I have a data which is given below:

> dput(qq)
structure(list(SIC = c(50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50), AVGAT = c(380.251, 391.3885,
421.72, 431.83, 483.715, 600.0715, 698.5945, 733.814, 721.426,
706.0265, 698.41, 697.9565, 720.761, 855.5245, 1023.226, 1214.8215,
1369.7605, 1439.2765, 1602.3845, 1949.69), ADA = c(0.0223312309851002,
0.00984600086327487, 0.0199212814576842, 0.0562291585405388,
0.0155376903911516, 0.0195296616004618, 0.00650206622557842,
0.0295510054117198, 0.0471091745681615, 0.0898164879903691, 0.154998113255882,
0.0347106350470676, 0.109407241662021, 0.057428893735577, 0.0637457846236655,
0.0584883505633773, 0.0439293152619417, 0.030699982198924, 0.00900414418496609,
0.0293862740698763), NLEAD = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("SIC", "AVGAT", "ADA",
"NLEAD"), row.names = c(NA, 20L), class = "data.frame")

Whe n我运行以下代码,代码不会绘制任何内容:

When I run the following code, the code doesn't plot anything:

clusmypath <- file.path("C:", "Users", "Swordfish", "Dropbox", "aaa.pdf");
pdf(file = clusmypath);
library(ggplot2);
ww <- ddply(qq, .(SIC), function(p){ggplot(p,aes(x=AVGAT,y= ADA, color = NLEAD)) + geom_point(shape=1) ;return(p)});
dev.off();

但是,当我绘制完整数据时:

However, when I plot the full data :

clusmypath <- file.path("C:", "Users", "Swordfish", "Dropbox", "aaa.pdf");
pdf(file = clusmypath);
library(ggplot2);
ggplot(qq, aes(x = AVGAT, y = ADA, color = NLEAD)) + geom_point(shape=1)
dev.off();

我得到一个情节。我如何使ddply的一部分工作?谢谢。

I get a plot. How can I make ddply part to work? Thanks.

推荐答案

return(p)返回一个情节。 p 指的是数据框 qq 的每个子集。一般情况下,为了'返回'由 ggplot 产生的图,需要使用 print (参见)。但是,在您的特定情况下,您想要保存图的位置,您不需要 print

return(p) in your script doesn't return a plot. p refers to each subset of the data frame qq. In general, to 'return' plots produced by ggplot inside functions you need to use print (see FAQ 7.22). However, in your particular case, where you want to save plots, you don't need print.

几个PDF文件

',你可以尝试这样的事情。 d_ply 在您仅为其副作用调用函数时非常有用,例如,当我们保存图的输出时。而不是 pdf / 一些绘图 / dev.off ,您可以使用 ggsave

If you want one file per level of 'SIC', you may try something like this. d_ply is useful when you call a function only for its side effects, like here when we save the output from a plot. Instead of pdf/some-plotting/dev.off, you may use ggsave.

d_ply(qq, .(SIC), function(p){
  ggplot(p, aes(x = AVGAT, y = ADA, color = NLEAD)) + geom_point(shape = 1)
  ggsave(file = paste0(unique(p$SIC), ".pdf"))
  })

包含多个页面的文件

如果您需要一个PDF文件,每个级别的'SIC'有一页,您可以使用 base 函数 pdf ,以及 .print = TRUE d_ply

If you want one PDF file, with one page per level of 'SIC', you may use base function pdf, and the .print = TRUE argument in d_ply.

# create a new SIC variable with two levels, for a more realistic test of the function
qq$SIC2 <- rep(c(50, 100), each = 10)

pdf(file = "aaa.pdf")

d_ply(qq, .(SIC2), .print = TRUE, function(p){
ggplot(p, aes(x = AVGAT, y = ADA, color = NLEAD)) + geom_point(shape = 1)
})

dev.off()

这篇关于ddply和ggplot - 不生成剧情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:30