考虑以下示例:
require(ggplot2)
aa <- data.frame(date = runif(3,1,100),Change = runif(3,1,100))
bb <- data.frame(date = runif(3,1,100),Change = runif(3,1,100))
dd <- rbind(aa,bb)
dd$site <- c("aa","aa","aa","bb","bb","bb")
是否可以在同一文档中生成2个数字,其中图1应该是与网站对应的网站==&#39; aa&#39;第二个数字应该是那些与网站对应的网站==&#39; bb&#39;?
我试图在循环中生成数字,但似乎没有在pdf中绘制任何内容:
nams <- ("aa","bb")
pdf("plots.pdf",width = 6,height = 6, paper='A4',family = "Times")
for (i in 1:2){
dd2 <- dd[dd$site == nams[i],]
ggplot(dd,aes(date,Change)) +
geom_line()
}
dev.off()
答案 0 :(得分:1)
你必须在情节中使用print
来输出它:
for (i in 1:2){
dd2 <- dd[dd$site == nams[i],]
print(ggplot(dd, aes(date, Change)) +
geom_line()
)
}