0

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): ggplot broken into facets, colored and re-ordered

sautedman
  • 112
  • 10
  • `facet_grid` function is from `ggplot2` package –  Jan 04 '16 at 08:10
  • Your use of `v` is weird. Try `df$Item <- factor(df$Item, levels = c("a", "o", "e", "u", "i", "A"))` then `ggplot(df, aes(x=Item, y=Duration, color=group)) + geom_point(shape=1) + facet_grid(Repetition ~ Participant)`. Also I'm not sure your chart design is optimum here. – Hugh Jan 04 '16 at 08:12
  • 3
    Do not "pull[...] out variable for color". ggplot2 looks for everything specified inside `aes` inside the data.frame supplied as `data` first. If you supply data outside this data.frame you might lose the relations between your data. – Roland Jan 04 '16 at 08:15
  • 1
    Please provide a reproducible example. – Roland Jan 04 '16 at 08:15
  • 2
    [This question/answer](http://stackoverflow.com/questions/32543340/behavior-ggplot2-aes-in-combination-with-facet-grid-when-passing-variable-wi) is closely related and the answer has a nice explanation of what happens and why in this sort of situation. – aosmith Jan 04 '16 at 17:10

1 Answers1

2

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:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • That was really helpful, thanks! You might want to consider getting rid of the `library(reshape2)` package call. Perhaps this is another question, but let's say I wanted to go a little further and just graph the difference between the red and blue sub-categories for each category on the x-axis. What would I have to do? – sautedman Jan 04 '16 at 16:57
  • Will get back to you on the diff question. – Mike Wise Jan 04 '16 at 18:07