Different Scales in facet_wrap

166 views
Skip to first unread message

Paul Metzner

unread,
Sep 27, 2010, 12:21:42 PM9/27/10
to ggp...@googlegroups.com
Hi!

I'm trying to illustrate the effects of data removal and log-transformation on the distribution of residuals in an otherwise unaltered LMM. When I plot with facet_wrap, the same y-axis applies to all four plots, pushing the (naturally) much smaller log-transformed values together (which, ironically, is one of the reasons why to transform at all). Is there a simple way to draw the plots with different scales? Here's the code:

print(m1 <- lmer(potato~treatment + (1|subject), french_fries), cor=FALSE)
print(m2 <- lmer(log(potato+1)~treatment + (1|subject), french_fries), cor=FALSE)
print(m3 <- lmer(potato~treatment + (1|subject), subset(french_fries, potato!=0)), cor=FALSE)
print(m4 <- lmer(log(potato)~treatment + (1|subject), subset(french_fries, potato!=0)), cor=FALSE)

res1 <- data.frame(res = resid(m1), fit=scale(fitted(m1)))
res1$DV <- 1

res2 <- data.frame(res = resid(m2), fit=scale(fitted(m2)))
res2$DV <- 2

res3 <- data.frame(res = resid(m3), fit=scale(fitted(m3)))
res3$DV <- 3

res4 <- data.frame(res = resid(m4), fit=scale(fitted(m4)))
res4$DV <- 4

xx <- rbind(res1,res2,res3,res4)
xx$DV <- factor(xx$DV)
levels(xx$DV) <- c("a)", "b)", "c)", "d")

p <-qplot(x=fit, y=res, data=xx, geom="point", facets= . ~ DV,
xlab="Fitted values", ylab="Standardized Residuals")
p + geom_hline(yintercept=0) + theme_bw()

It's not as bad in this example, because potato-y-ness goes only to 15, but you can image the same plots with values up to 3500. At least now it's reproducible for everyone.

Best,
Paul

---
Paul Metzner

Humboldt-Universität zu Berlin
Philosophische Fakultät II
Institut für deutsche Sprache und Linguistik

Post: Unter den Linden 6 | 10099 Berlin | Deutschland
Besuch: Dorotheenstraße 24 | 10117 Berlin | Deutschland

+49-(0)30-2093-9726
paul.m...@rz.hu-berlin.de
http://amor.rz.hu-berlin.de/~metznerp/

Brandon Hurr

unread,
Sep 27, 2010, 12:40:46 PM9/27/10
to Paul Metzner, ggp...@googlegroups.com
Does the scales='free' argument work?

I'm sure I've mucked up something in the code, but it's hard to tell
without your data.

ggplot(data=xx, aes(x=fit, y=res))+
geom_point()+
facet_grid(. ~ DV, scales='free')+
geom_hline(yintercept=0)+
scale_x_continuous("Fitted Values")+
scale_y_continuous("Standardized Residuals")
theme_bw()

Does that work?

Brandon

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 28, 2010, 3:48:04 AM9/28/10
to Paul Metzner, Brandon Hurr, ggp...@googlegroups.com
Hi:

Since the residuals appear to be clustered around predicted values and the sample size is fairly large, it may be useful to jitter the points and apply alpha transparency as well. On top of the previous answers, let me add the following:

g <- ggplot(xx, aes(x = fit, y = res))
g + geom_jitter(position = position_jitter(width = 0.2), alpha = 0.4) +
      geom_hline(yintercept = 0) +
      facet_wrap(~ DV, scales = 'free') +
      xlab('Fitted values') + ylab('Standardized residuals') +
      theme_bw()

The purpose of jittering is to avoid overplotting of points by spreading out the values horizontally over a wider range. I could increase the width to spread points out a bit further - you can also skip the position = argument and see what the default behavior would be. The alpha transparency allows one to see where concentrations of points exist; in the models with log responses (b and d), not only is the 'funnel effect' obvious but the transparency allows one to see that a significant portion of the residuals are associated with predicted values between 0.5 and 1.5 with low variance. You can play with the width and alpha values to get an effect that appeals to you.

HTH,
Dennis

Paul Metzner

unread,
Sep 28, 2010, 4:49:49 AM9/28/10
to Dennis Murphy, Brandon Hurr, ggp...@googlegroups.com
Thank you for the suggestion!

The main purpose of the plot is to illustrate the superiority of log-transformation and zero-removal regarding the distribution of residuals. Insofar, overplotting is ok. However, saving the plots as post-script seems to omit jittering and alpha. Is that the usual behaviour? Exporting as PNG preserves transparency, but I need to change the fonts manually after export, which is a lot easier with vector graphics. I put the dataframe (created with dput) into my dropbox, here's the current code:

p <-qplot(x=fit, y=res, data=xx, geom="point", shape=1, xlab="Fitted Values", ylab="Standardized Residuals")
p <- p + geom_hline(yintercept=0) + theme_bw(base_size=10) + facet_wrap(~DV, scales="free_y", nrow=2)
p + geom_jitter(position = position_jitter(width = 0.6), alpha = 0.2)
ggsave(file="ggdistmod.png", width=6, height=4.5)

Best,
Paul

Data: http://dl.dropbox.com/u/6281903/xx

>>> http://amor.rz.hu-berlin.de/~metznerp/<http://amor.rz.hu-berlin.de/%7Emetznerp/>


>>>
>>> --
>>> You received this message because you are subscribed to the ggplot2
>> mailing list.
>>> Please provide a reproducible example: http://gist.github.com/270442
>>>
>>> To post: email ggp...@googlegroups.com

>>> To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>


>>> More options: http://groups.google.com/group/ggplot2
>>
>> --
>> You received this message because you are subscribed to the ggplot2 mailing
>> list.
>> Please provide a reproducible example: http://gist.github.com/270442
>>
>> To post: email ggp...@googlegroups.com

>> To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>
>> More options: http://groups.google.com/group/ggplot2
>>

---
Paul Metzner

Manfred-von-Richthofen-Str. 13
12101 Berlin
Deutschland

Tel.: +49-(0)30-6730-9220
Mobil: +49-(0)17-8288-1059

paul.m...@gmail.com
http://amor.rz.hu-berlin.de/~metznerp/

Reply all
Reply to author
Forward
0 new messages