0

I'm pretty new to R and I've been trying to figure out how to do this for a while.

What I need to do is take very simple long form data and produce a couple graphs. Here is an example of what I have---also, pardon me but I have no idea how to format on here so I'm giving you how it appears in basic raw data.

 pm.data= data.frame() 

Score, Behavior, Period, Date
1, 2, 1, 9/01/2001
 3, 2, 3,  9/01/2001
2, 3, 4, 9/05/2004
4, 1, 6, 9/05/2004..............and so on

Okay so the first number is a behavior score (1-5), the second represents which target behavior being observed (identified as a 1, 2, or 3), Period represents what time period the behavior is observed(1st, 3rd, 4th, or 6th) and the date is m/d/y. For each date, there are 12 scores (4 periods, 3 behaviors to score..4x3)

So by using

totals = aggregate(cbind(Score) ~ Date, data = pm.data, FUN = sum)

I was able to get sums of Score by Date. And then I used ggplot to show the total behavior ratings for each day (Date2 is an object I had to make after aggregate() rearranged my dates to start with most recent date instead of latest)

newtotals = totals[order(as.Date(totals$Date, format = "%m/%d/%Y")),]
newtotals$Date2 = factor(newtotals$Date, as.character(newtotals$Date))
setattr(newtotals, "row.names", c("1":"14"))

  ggplot(data = newtotals, aes(x = Date2, y = Score)) + 
  geom_point() + 
  geom_hline(yintercept = 48, color = "#990000", linetype = "dashed")  +
  geom_smooth(method = "lm", se = FALSE, aes(group = 1)) + 
  geom_vline(xintercept = as.numeric(newtotals$Date2[4]), linetype = "dashed") +
  xlab("Dates by Observation Days") + 
  ylab("Total Behavior Rating") + 
  theme_bw()

Now I need to graph Score sums by Date for each Period (so a graph that shows the total behavior score throughout the Dates of observation for each period). And I need to plot behavior as individual lines on each graph so I can show which behaviors were observed and to what degree during which periods. So I should end up with 4 graphs (one for each Period), with Dates on X, Sum of Scores by behavior on Y, color = Behavior, as well as these hlines and vlines.

I know this may be somewhat confusing to understand so if you can't get what I mean I understand.

  • Welcome to Stack Overflow. To format code here, just place four spaces in front of it. Additional spaces can be used to preserve indentation. –  Nov 23 '14 at 03:43
  • Also see [how to make a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tip on improving your question. What exactly have you tried and what exactly didn't work? Can you sketch the desired output for a specific sample input? – MrFlick Nov 23 '14 at 05:49
  • It isn't very clear exactly what you are struggling with, but I think you just want to add `facet_wrap(~ Period)` to your plot. If this isn't what you want, try removing bits from your question to strip it down to a minimal problem. Providing data to let us run the code would help too. – Richie Cotton Nov 23 '14 at 05:50
  • Can you provide your dataset? Either upload it somewhere and post a link, or, if it's not too large, post the output of `dput(pm.data)` directly in your question. – jlhoward Nov 23 '14 at 17:57
  • https://github.com/AndrewThayer/PM-Data That should provide a link to the github where I just uploaded the dataset. I have ran facet_wrap. Here's what I have: `######################Graphs of behavior by Period? pm.data$Behavior = factor(pm.data$Behavior) levels(pm.data$Behavior) pm.data$Period = factor(pm.data$Period) levels(pm.data$Period) ggplot(data = pm.data, aes(x = Date, y = Score, color = Behavior)) + geom_line(aes(group = 1)) + facet_wrap(~Period)` But facet_wrap puts all of the graphs together. I just need to pull out one graph at a time – Andrew Jordan Thayer Nov 23 '14 at 18:24
  • Also, I apologize for the messy display. I was exhausted last night and tired of staring at this screen. I'll try to go back and edit what I meant to say. But I think if you guys look at the data set, run the code I posted with the facet_wrap, you'll see kind of what I need. I've been thinking about using `facet_wrap(Period~Behavior)` but that output is a little messy – Andrew Jordan Thayer Nov 23 '14 at 18:29

0 Answers0