Reputation: 132
I'm graphing data from an experiment I ran with multiple participants who repeated the same trial several times. Naturally, I'm using facet_grid()
from the ggplot2
package to plot each trial for each participant in its own sub-graph. The graph is beautiful, but it seems to be failing to represent the data I am feeding it. To wit, the x-axis in my graph is categorical, and I have re-ordered the levels using factor()
, but while the axes are correct, the data in the plots does not follow the intended ordering. Stranger still, I'm using color to differentiate between values, but the color bindings in the legend seem to be off. So my question is ... what can I do to get the expected behavior?
Here's some sample code (data omitted):
library(ggplot2)
#pulling out variable for color
group <- df$Subvalue
#assigning an order to levels
v <- df$Item
v <- factor(v, levels = c("a", "o", "e", "u", "i", "A"))
p <- ggplot(df, aes(x=v, y=Duration, color=group)) +
geom_point(shape=1)
p
#adding the facets
p + facet_grid(Repetition ~ Participant)
And here's part of my graph (it didn't come out very well in the pdf-ifying, so just use it to get a gestalt):
Upvotes: 0
Views: 173
Reputation: 22847
Maybe this is what you want? You need to leave your data in the dataframe, pulling it out just make things harder to keep in sync (as Hugh mentioned). Also you need to be explicit about what colors you want.
library(ggplot2)
# generate some fake data
n <- 500
iv <- sample(c("a", "o", "e", "u", "i", "A"),n,replace=T)
dv <- rnorm(n,100,50)
sv <- sample(c("group-1","group-2"),n,replace=T)
rv <- sample(1:2,n,replace=T)
pv <- sample(1:14,n,replace=T)
df <- data.frame(Item=iv,Duration=dv,Subvalue=sv,Repetition=rv,Participant=pv)
#assigning an order to levels
df$v <- factor(df$Item, levels = c("a", "o", "e", "u", "i", "A"))
p <- ggplot(df, aes(x=v, y=Duration, color=Subvalue)) +
geom_point(shape=1) +
scale_color_manual(values=c("group-1"="red","group-2"="blue")) +
facet_grid(Repetition ~ Participant)
p
Yielding:
Upvotes: 2