0

I have the same example as before (interpolation of grouped data using data.table), but here I am trying to use ggplot2 to create a single .pdf plot for each site_no. Instead of the data for each site_no printed on a single .pdf, all of the data from all of the plots are printed on each site_no named .pdf. Can you assist?

Thank you.

Irucka

library(data.table)
library(ggplot2)

tempbigdata1 <- data.table(c(14.80, 14.81, 14.82), c(7900, 7920, 7930),      c("02437100", "02437100", "02437100"))

tempbigdata2 <- data.table(c(9.98, 9.99, 10.00), c(816, 819, 821), c("02446500", "02446500", "02446500"))

tempbigdata3 <- data.table(c(75.65, 75.66, 75.67), c(23600, 23700, 23800), c("02467000", "02467000", "02467000"))

tempsbigdata <- rbind(tempbigdata1, tempbigdata2, tempbigdata3)

setnames(tempsbigdata,c("depth", "discharge", "site_no"))

setkey(tempsbigdata, site_no)

tempsbigdata
depth       discharge   site_no
1: 14.80    7900        02437100
2: 14.81    7920        02437100
3: 14.82    7930        02437100
4:  9.98    816         02446500
5:  9.99    819         02446500
6: 10.00    821         02446500
7: 75.65    23600       02467000
8: 75.66    23700       02467000
9: 75.67    23800       02467000

ratingsplot <- tempsbigdata[,list(discharge,depth),by=site_no]

for (u in unique(ratingsplot$site_no)) {pdf(file = file.path("/plots/",   paste0(u, "_rating.pdf", sep = "")))
p <- ggplot(ratingsplot, aes(x = discharge, y = depth, group = site_no))
print(p <- p + geom_point() + theme_bw())
dev.off()
}  
4

2 回答 2

1

You need to actually subset the data.table. E.g.:

p <- ggplot(ratingsplot[J(site_no=u)], aes(x = discharge, y = depth, group = site_no))

(Also, no need for slashes in a call to file.path; these are added by the function.)

于 2013-08-17T03:01:59.170 回答
1

Try modifying the data argument to ggplot(ratingsplot[site_no==u],...

 p <- ggplot(ratingsplot[site_no==u], 
             aes(x = discharge, y = depth, group = site_no))

Other plotting paradigms have a subset argument that accepts a logical expression interpreted in the environment of the data argument, but I didn't see one documented or linked to from the `?ggplot help page.

于 2013-08-17T03:49:50.250 回答