Re: ggplot2 facet

59 views
Skip to first unread message

Doug Mitarotonda

unread,
May 15, 2013, 9:55:35 AM5/15/13
to jf Zhang, ggp...@googlegroups.com
It is because of how you use `factor` in each case. `factor` with default parameters will use lexicographic ordering of the levels, as is what happened in the first case. In the second case you specified the levels, so the ordering is as you stated it. You can see this difference clearly if you use `str`:

> str(f1)
 Factor w/ 2 levels "a","b": 2 2 2 2 2 2 2 2 2 2 ...
> str(f11)
 Factor w/ 2 levels "b","a": 1 1 1 1 1 1 1 1 1 1 ...

The difference of this ordering is what is driving your differences in the plots as ggplots relies on the ordering of factors to determine the order of how to plot your data.

Best wishes,
Doug



On May 15, 2013, at 12:48 AM, jf Zhang <zhangjia...@gmail.com> wrote:

Hi everyone. I have ran qplot(x, y, facets = f1 ~ f2) to create a multiple facet graph, here f1 and f2 are two factors, but I found that if the order of levels of f1 or f2 is changed (say, from levels = c("a","b") to levels = ("b","a")) , than the output is different. Are there something wrong in my code or there exists some usages that I had yet to  master?

Here is the code:

f1 <- rep(c("b", "a"), each = 30)
f2 <- rep(c("d", "c", "d", "c"), each = 15)
f1 <- as.factor(f1)
f2 <- as.factor(f2)

x <- rnorm(15)
y1 <- -1 * x
y2 <- x
y3 <- 3 * x
y4 <- -3 * x
y <- c(y1, y2, y3, y4)
x <- c(x, x, x, x)

library(ggplot2)
# graph 1
qplot(x, y, facets = f1 ~ f2)

# graph 2
f11 <- factor(f1, levels = c("b", "a"))
f22 <- factor(f2, levels = c("d", "c"))
qplot(x, y, facets = f11 ~ f22)

These two graphs have difference in facets labels.

Thank you for attention!

Zhang Jianfeng



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Doug Mitarotonda

unread,
May 16, 2013, 11:59:42 AM5/16/13
to Hirasawa, Yasutaka, jf Zhang, ggp...@googlegroups.com
Ok, I see what you are saying. I actually don't think this is a problem with faceting, it is a problem with the way `qplot` builds the data from the environment if you don't pass it as a parameter. I suggest filing this as a bug on github and see what Hadley says. 

I modified your MWE a bit to make it easier for me to see what was going on (and you can feel free to use this in your bug submission). 

x <- c(0, 0, 1, 1)
y <- c(0, 1, 0, 1)
f1 <- c("a", "a", "b", "b")
f2 <- c("c", "d", "c", "d")
F1 <- factor(f1)
F2 <- factor(f2)
F11 <- factor(f1, levels = c("b", "a"))
F22 <- factor(f2, levels = c("d", "c"))

df <- data.frame(x, y, F1, F2, F11, F22)

qplot(x, y, facets = F1 ~ F2) # correct
ggplot(df, aes(x, y)) + geom_point(color = "red", size = 4) + facet_grid(F1 ~ F2) # correct
qplot(x, y, facets = F11 ~ F22) # wrong
ggplot(df, aes(x, y)) + geom_point(color = "blue", size = 4) + facet_grid(F11 ~ F22) # correct
qplot(x, y, facets = F11 ~ F22, data = df) # correct

So, I woud say either use ggplot or qplot by passing in the data parameter directly. 




On May 15, 2013, at 8:45 PM, "Hirasawa, Yasutaka" <Yasutaka...@netapp.com> wrote:

If we do:
> data <- data.frame(x,y,f1,f2,f11,f22)
> ggplot(data, aes(x,y)) + geom_point() + facet_grid(f1 ~ f2)
> ggplot(data, aes(x,y)) + geom_point() + facet_grid(f11 ~ f22)
They generate two consistent charts, just grid scales/labels were switched around. 
 
However,
qplot(x, y, facets = f1 ~ f2)
qplot(x, y, facets = f11 ~ f22)
as Zhang mentioned, these generate inconsistent charts and looked wrong….I think this is a qplot bug and ggplot is generating right charts.

jf Zhang

unread,
May 19, 2013, 11:20:17 PM5/19/13
to Doug Mitarotonda, Hirasawa, Yasutaka, ggp...@googlegroups.com
Thank you for positive replys and suggestions. Now I know how to avoid this problem. :-)

I'll submit this problem on github later.

Best wishes,

 Zhang Jianfeng


2013/5/16 Doug Mitarotonda <dougmit...@gmail.com>
Reply all
Reply to author
Forward
0 new messages