0

What I have is a 3-Levels Repeated Measures Factor and a continuous variable (Scores in psychological questionnaire, measured only once pre-experiment, NEO), which showed significant interaction together in a Linear Mixed Effects Model with a Dependent Variable (DV; State-Scores measured at each time level, IAS).

To see the nature of this interaction, I would like to create a plot with time levels on X-Axis, State-Score on Y-Axis and multiple curves for the continuous variable, similar to this. The continuous variable should be categorized in, say quartiles (so I get 4 different curves), which is exactly what I can't achieve. Until now I get a separate curve for each value in the continuous variable.

My goal is also comparable to this, but I need the categorial (time) variable not as separate curves but on the X-Axis.

I tried out a lot with different plot functions in R but did'nt manage to get what I want, maybe because I am not so skilled in dealing with R.

F. e.

gplot(Data_long, aes(x = time, y = IAS, colour = NEO, group = NEO)) + 
  geom_line()

from the first link shows me dozens of curves (one for each value in the measurement NEO) and I can't find how to group continuous variables in a meaningful way in that gplot function.

Edit:

Original Data:

http://www.pastebin.ca/2598926

(I hope it is not too inconvenient.)

This object (Data_long) was created/converted with the following line:

Data_long <- transform(Data_long0, neo.binned=cut(NEO,c(25,38,46,55,73),labels=c(".25",".50",".75","1.00")))

Every value in the neo.binned col seems to be set correctly with enough cases per quantile.

What I then tried and didn't work:

ggplot(Data_long, aes(x = time, y = ias, color = neo.binned)) + stat_summary(fun.y="median",geom="line")

geom_path: Each group consist of only one observation. Do you need to adjust the group >aesthetic?

I got 92 subjects and values for NEO between 26-73. Any hints what to enter for cut and labels function? Quantiles are 0% 25% 50% 75% 100% 26 38 46 55 73.

Community
  • 1
  • 1
entne
  • 137
  • 7
  • 1
    Welcome to SO! People are generally much more happy to help if you provide a [**minimal, reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Thanks. – Henrik Jan 28 '14 at 11:51
  • I am not sure what else to add, it is just these variables in a long format data.frame. – entne Jan 28 '14 at 11:58
  • 3
    Have you read the link I provided? You wish that people on SO should spend their time to help you solve your problem. Please don't add an extra task to create dummy data. That is your duty. – Henrik Jan 28 '14 at 12:04
  • OK, now I got it, you are right. See my edit. If anything else is needed, just let me know. – entne Jan 28 '14 at 13:53

2 Answers2

1

Do you mean something like this? Here, your data is binned according to NEO into three classes, and then the median of IAS over these bins is drawn. Check out ?cut.

Data_long <- transform(Data_long, neo.binned=cut(NEO,c(0,3,7,10),labels=c("lo","med","hi")))

Plot everything in one plot.

ggplot(Data_long, aes(x = time, y = IAS, color = neo.binned)) 
  + stat_summary(aes(group=neo.binned),fun.y="median",geom="line")

enter image description here And stealing from CMichael's answer you can do it all in multiple (somehow you linked to facetted plots in your question):

ggplot(Data_long,aes(x=time,y=IAS)) 
  + stat_summary(fun.y="median",geom="line") 
  + facet_grid(neo.binned ~ .)

enter image description here

ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • I am not sure. At the one Hand the variable NEO isn't involved, is it? And on the other hand, I get an error with my data: "Error in rq.fit.br(wx, wy, tau = tau, ...) : Singular design matrix" You are right with the sample data, sorry, it is just my novelty to R that I am not able to create a better fitting example. – entne Jan 28 '14 at 14:35
  • If you also split by `NEO`, then you cannot summarize anything and median or other quantiles reduce to identity. – ziggystar Jan 28 '14 at 14:58
  • By which variable have you split in your code above? My concern is to plot time x IAS split by NEO quantiles. – entne Jan 28 '14 at 15:03
  • Well, `time` on x, `IAS` on y and then quantiles over `NEO`. If you want to represent each `NEO` with different color, then you come out with only one point of data per distinguishable element in your plot. – ziggystar Jan 28 '14 at 15:13
  • Hm, when use your line for my real data object, I get the error mentioned above. I asked about NEO because it doesn't appear in your line of code. – entne Jan 28 '14 at 15:20
  • Thanks so far, this is what I need (preferable in one plot). When I apply this to my real data, it says: "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?" I got 92 subjects and values for NEO between 26-73. Any hints what to enter for cut and labels function? Quantiles are 0% 25% 50% 75% 100% 26 38 46 55 73 – entne Jan 28 '14 at 16:22
  • 1
    @entne If your smallest item is 26, then the bin 0-25 is empty. Maybe this causes the error. I cannot recommend you good values for `breaks` without seeing the data. You should be able to do this yourself. If my post answers your question, please accept it. If you find it helpful you can also upvote it. – ziggystar Jan 28 '14 at 18:07
  • Upload my original Data, the adapted code from you that I used and the error I get with that. Would be great if you had another look. See my edit. – entne Jan 29 '14 at 08:50
  • @entne The problem was that (after having enough samples per bin) for drawing the lines, the points weren't grouped correctly. So you should adjust your breaks to what you need (depends on what you want to show). See my updated code for the first example, in particular the `group` thing in `stat_summary`. For breaks I used `c(0,30,70,100)` and it worked. – ziggystar Jan 29 '14 at 09:57
0

Do you mean facetting @ziggystar initial Plot?

quantiles = quantile(Data_long$NEO,c(0.25,0.5,0.75))

Data_long$NEOQuantile = ifelse(Data_long$NEO<=quantiles[1],"first NEO Quantile",
                               ifelse(Data_long$NEO<=quantiles[2],
                                      "second NEO Quantile",
                                      ifelse(Data_long$NEO<=quantiles[3],
                                             "third NEO Quantile","forth NEO Quantile")))

require(ggplot2)
p = ggplot(Data_long,aes(x=time,y=IAS)) + stat_quantile(quantiles=c(1),formula=y ~ x)
p = p + facet_grid(.~NEOQuantile)
p

enter image description here

CMichael
  • 1,856
  • 16
  • 20