Reputation: 1112
I have a few columns of data. I would like to find the mean for two different values for each time section by user. Then I would like to graph accordingly. There should only be one value for metricA and statusB for each user for each time section. I thought it was working but when I graph I see two separate lines for statusB in the same time section for some users (which should not happen). [This isn't pertinent to my current problem, but I'm doing this in a loop.]
I generate mean values for metricA and statusB.
tempmetricAagg <- aggregate(metricA ~ username + time_chunk, data = tempdf, FUN = mean)
tempstatusBagg <- aggregate(statusB ~ username + time_chunk, data = tempdf, FUN = mean)
Then merge them (I originally did this without specifying column names, but the result has been the same)-
tempmetricAstatusBagg <- merge(tempmetricA, tempstatusB, by =c("username","time_chunk"))
Then I graph the results with ggplot:
ggplot(data=tempmetricAstatusBagg, aes(as.factor(time_chunk), metricA, group=statusB, color = statusB)) + geom_line() + facet_wrap(~ username) + scale_colour_gradient(limits=c(0, 1), low="red")
Looking closer, I believe the problem may arise in from how I call (or fail to call) the nested elements.
partfillbygroup <- split(partfill, partfill$group)
for (i in 1:length(partfillbygroup)){
cat(names(partfillbygroup[i]), "\n")
tempdf <- subset(partfillbygroup[[i]][,c("username","metricA", "statusB")])
I've also tried:
tempdf <- as.data.frame(partfillbygroup[[i]])
Here's the data from dput:
structure(list(username = structure(c(44L, 44L, 44L, 44L, 44L,
44L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 46L, 46L, 46L,
46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L,
48L, 48L, 48L, 48L, 48L, 48L, 48L, 49L, 49L, 49L, 49L, 49L, 49L,
50L, 50L), .Label = c("group21", "group216", "group218", "group219",
"group22", "group220", "group225", "group227", "group228", "group23",
"group230", "group26", "group28", "group29", "group11", "group110",
"group111", "group112", "group113", "group114", "group115",
"group116", "group117", "group118", "group119", "group12",
"group120", "group121", "group122", "group13", "group130",
"group14", "group17", "group18", "group19", "sampleuser1", "sampleuser11",
"sampleuser129", "sampleuser13", "sampleuser130", "sampleuser14", "sampleuser15", "sampleuser16",
"sampleuser17", "sampleuser18", "sampleuser19", "sampleuser20", "sampleuser21", "sampleuser24", "sampleuser26",
"sampleuser30", "sampleuser31", "sampleuser32", "sampleuser33", "sampleuser34", "sampleuser36", "sampleuser37",
"sampleuser38", "sampleuser39", "sampleuser41", "sampleuser42", "sampleuser44", "sampleuser45", "sampleuser46",
"sampleuser47", "sampleuser49", "sampleuser5", "sampleuser50", "sampleuser51", "sampleuser52", "sampleuser53",
"sampleuser54", "sampleuser55", "sampleuser58", "sampleuser59", "sampleuser6", "sampleuser61", "sampleuser63",
"sampleuser64", "sampleuser65", "sampleuser66", "sampleuser67", "sampleuser68", "sampleuser69", "sampleuser72",
"sampleuser73", "sampleuser74", "sampleuser75", "sampleuser76", "sampleuser77", "sampleuser78", "sampleuser79",
"sampleuser8", "sampleuser80", "sampleuser9"), class = "factor"), time_chunk = c(2,
3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5,
6, 6, 7), statusB = c(0, 0, 0, 0.958333333333333, 1, 1, 0,
0, 0, 0.851851851851852, 1, 0.8125, 1, 0, 0, 0, 0.290322580645161,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0.6, 1, 1, 1, 0, 0, 0, 0, 0.727272727272727,
1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0), metricA = c(0.369215384615385,
0.607138888888889, 0.527866666666667, 0.115908333333333, 0.131221739130435,
0.0860222222222222, 0.0370333333333333, 0.0946363636363636, 0.107113043478261,
0.406085185185185, 0.460740909090909, 0.42078125, 0.6807, 0.170962162162162,
0.194261290322581, 0.486108333333333, 0.22921935483871, 0.160673684210526,
0.1475625, 0.272055555555556, 0.31885625, 0.4423, 0.192307692307692,
0.1892, 0.0951933333333333, 0.12151, 0.15072, 0.226752631578947,
0.234642857142857, 0.3227, 0.0992, 0.191246153846154, 0.0694444444444444,
0.0899, 0.129172727272727, 0.144986363636364, 0.290582352941176,
0.351575, 0.153927777777778, 0.143108823529412, 0.178781818181818,
0.12222, 0.114009090909091, 0.414692857142857, 0.269341666666667,
0.361045)), .Names = c("username", "time_chunk", "statusB",
"metricA"), row.names = c(NA, -46L), class = "data.frame")
Upvotes: 0
Views: 128
Reputation: 7123
The problem occures from group = statusB
in the call for ggplot()
. To give you an example:
s21 <- tempmetricAstatusBagg[ tempmetricAstatusBagg$username == "sampleuser21", ]
ggplot(data=s21, aes(as.factor(time_chunk), metricA, group=statusB, color = statusB)) +
geom_line() +
geom_point( size = 5 ) +
scale_colour_gradient(limits=c(0, 1), low="red")
The question which arises is: How do you want to plot the color gradient on geom_line anyways, if you would not want to group the data by statusB
?
Another question: Why do you aggregate your data by username
and time_chunk
if you expect this combination to be unique?
Upvotes: 1