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.