1

I would like to make the same plot for each column of data in a data frame below called df.

Edit: to clarify, I would like to make a new plot for each column (Cop, CopN, Harp) etc as in my actual data I have many (i.e. far to many to try and grid on one plot) Id like to be able to create the plot below for each column.

Sample data:

day <- c('5-Aug', '5-Aug','5-Aug','10-Aug','10-Aug','10-Aug','17-Aug','17-Aug','17-Aug')
station <- c(1:3,1:3,1:3)
Cop.Mean <- c(382, 1017, 1519, 698, 5398, 2458, 346, 5722, 6253)
CopN.Mean <- c(233, 167, 530, 36, 124, 20, 427, 1768, 1486)
Harp.Mean <- c(20, 482, 10, 8, 4014, 7, 4, 46, 1)
df <- data.frame(day,station,Cop.Mean, CopN.Mean, Harp.Mean)

My plot:

library(ggplot2)
library(scales)
Cop.Plot <- ggplot(data=df, aes(x=station, y=Cop.Mean)) +
         geom_point() + facet_grid(.~day) 
         print(Cop.Plot)

I would like to make this plot for each of the three columns of data I have in this example (i.e. df[3:5]) but haven't been able to figure out how to refer to multiple columns within aes(). (i.e.

aes(x=station, y=df[3:5]) 

doesn't work. I've also tried

i=df[3:5] 
aes(x=station, y=i)

but I think I am likely not using the correct approach. Would anyone be so kind as to point me in the right direction? This seems like an extremely useful tool to learn how to do, and I am very eager to do so!

wonderoustree
  • 87
  • 2
  • 11
  • 3
    in ggplot2, one often works with long-format data.frame. In you example you would first `reshape2::melt(df, id=1:2)` before passing it to ggplot2 – baptiste Nov 22 '13 at 20:22

3 Answers3

2
library(ggplot2)
library(reshape2)
moltendf <- melt(df, id.vars=c("day","station"))
allplots <- ggplot(data=moltendf, aes(x=station, y=value)) +
             geom_point() + facet_grid(variable ~ day)

but I am not clear if you want them all one one big facteted plot or you want to *apply and create/save many different plots per cop.type. If it is the latter then with the molten data you can:

plotfun <- function(x,y) { 
                 a <- ggplot(data=x, aes(x=station, y=value)) +
                 geom_point() + facet_grid(.~ day)
                 pdf(paste0(y,".pdf"))
                 print(a)
                 dev.off()
}

mapply(plotfun, split(moltendf, moltendf$variable), as.list(1:3))
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • is paste0() supposed to be paste()? Also, see my edits in my question - I do want to use sapply or similar to create and save many plots (one for each column of data). Thank you very much for the code - however, I'm not sure what I need to do with it: when I enter it nothing happens. Is there something I'm missing? Thank you! – wonderoustree Nov 22 '13 at 21:24
  • 1
    no, see `?paste0`. Also check your working directory to see where the plots are saved. – user1317221_G Nov 22 '13 at 21:27
  • Sorry, to be more clear, it does save three pdf files in my working directory, but these pdf files do not seem to contain any data and I get an error when trying to open one: "There was an error opening this document. This file cannot be opened because it has no pages." Am I missing something in your code where I need to add in something? Thank you very much for your help. – wonderoustree Nov 22 '13 at 21:34
  • 1
    i ommited `print(a)` earlier, I have corrected this in my edit, so I would try it again with the code in my answer now, it works for me. :) – user1317221_G Nov 22 '13 at 21:35
  • 1
    Aha! Brilliant. I am new to R and programming in general, and can't wait to apply (tee hee) what you've shown me elsewhere. – wonderoustree Nov 22 '13 at 21:42
  • 3
    an alternative with shorter syntax, `pdf("all.pdf"); plyr::d_ply(moltendf, "variable", "%+%", e1=allplots, .print=TRUE); dev.off()`. – baptiste Nov 22 '13 at 21:50
  • Is there a way to get R to paste the variable name (i.e. Cop.Mean) when it saves the pdf rather than 1.pdf 2.pdf etc? – wonderoustree Nov 22 '13 at 22:06
  • yes change the `as.list(1:3)` to something like `list("Cop.Mean","opportunity for selection","chocolate pie")`. It is just a list of names. try pasting that last bit of code by itself to see what i mean. for your case you might want `as.list(unique(moltendf$variable)` – user1317221_G Nov 22 '13 at 22:21
  • I see, thank you. When I have the last line be mapply(plotfun, split(df, df$Bug), as.list(1:3)) it saves three individual pdfs like I want, labeled 1 2 and 3.pdf. However when the last line reads mapply(plotfun, split(df, df$Bug), as.list("Cop", "CopN", "Harp")) it only saves the first pdf, albeit correctly labeled as Cop.pdf. I was also curious if there was a way to automatically have it save each plot using the variable level name rather than hand typing it in. Or should I ask that as a new question? – wonderoustree Nov 22 '13 at 22:36
  • you need `list("Cop", "CopN", "Harp")` . It can be difficult when starting with lists to know why. Compare `list(1:3)` with `as.list(1:3)` to try and see the difference and read up on lists – user1317221_G Nov 22 '13 at 22:47
  • Thank you very much. In case it's useful, I found that mapply(plotfun, split(moltendf, moltendf$variable), levels(moltendf$variable)) appropriately labels the output pdf files Cop.Mean.pdf, CopN.Mean.pdf etc. Yay! Thanks so much for the help. Much appreciated. – wonderoustree Nov 22 '13 at 22:51
1

As mentioned by baptiste, the reshape2 package will help you out. Here's the full code:

library(reshape2)
df <- melt(df, id = c("day", "station"))
Cop.Plot <- ggplot(data = df, aes(x = station, y = value)) + geom_point()
Cop.Plot + facet_grid(variable ~ day)

I recommend casting day as a factor and sorting levels chronologically via:

day <- factor(day, levels = c('5-Aug', '10-Aug', '17-Aug'))

Otherwise, they are sorted alphabetically.

ramhiser
  • 3,342
  • 3
  • 23
  • 29
1

Is this what you had in mind?

library(reshape2)
ggdata <- melt(df,id=1:2,variable.name="Measure",value.name="Mean")
ggplot(ggdata,aes(x=station, y=Mean)) +
  geom_point() + 
  facet_grid(Measure~day)

Produces this:

enter image description here

Note that the days are in the wrong order.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Actually, I am trying to make a separate plot for each species (Cop, CopN, Harp). See my edits to my question above - I have far too many species in my actual data set to do this, although this looks helpful and I will likely use elsewhere so thank you! – wonderoustree Nov 22 '13 at 21:15