Paired boxplots

12 views
Skip to first unread message

alobo

unread,
Mar 10, 2011, 6:12:54 AM3/10/11
to ggplot2
Hi!

I'm using this command to get a set of boxplots for each of 2 groups:
> ggplot(a, aes(x = variable, y = value)) + geom_boxplot()+ scale_y_continuous(trans="log10") + facet_wrap(~ AGE)
(many empty elements because a is just an small subset of the actual
dataset)

But what I would actually want is one panel in which boxes were
paired, left for "OLD", and right for "MOD", so that the 2 groups can
be compared by the distribution in each element.

The only way I can think of is actually splitting each element into 2
(i.e., CaOLD and CaMOD), is there a more elegant way?

Thanks
Agus

> dput(a)
structure(list(SAMPLE = c("AS-28", "AS-29", "AS-33", "719", "721",
"733", "808", "809", "UR-1", "UR-6", "UR-7", "CHA-2R", "CHA-1F",
"CHA-E", "CHA-G", "HUD", "COP", "LOP", "LLA", "AS-28", "AS-29",
"AS-33", "719", "721", "733", "808", "809", "UR-1", "UR-6", "UR-7",
"CHA-2R", "CHA-1F", "CHA-E", "CHA-G", "HUD", "COP", "LOP", "LLA",
"AS-28", "AS-29", "AS-33", "719", "721", "733", "808", "809",
"UR-1", "UR-6", "UR-7", "CHA-2R"), AGE = c("OLD", "OLD", "OLD",
"OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "MOD",
"MOD", "MOD", "MOD", "MOD", "MOD", "MOD", "MOD", "OLD", "OLD",
"OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD",
"MOD", "MOD", "MOD", "MOD", "MOD", "MOD", "MOD", "MOD", "OLD",
"OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD", "OLD",
"OLD", "MOD"), COMPO = c("Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy",
"Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy",
"Tbas", "Bas", "Bas", "Bas", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy",
"Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy",
"Rhy", "Tbas", "Bas", "Bas", "Bas", "Rhy", "Rhy", "Rhy", "Rhy",
"Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy", "Rhy"), LOC =
c("ZVC",
"ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "UR", "UR",
"UR", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS",
"ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "UR",
"UR", "UR", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS", "ZVS",
"ZVS", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC", "ZVC",
"UR", "UR", "UR", "ZVS"), variable = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label =
c("WEIGHT",
"Ca", "Mg", "Na", "K", "Si", "Cl", "SO4", "Li", "B", "Al", "P",
"Ti", "V", "Fe", "Mn", "Co", "Ni", "Cu", "Zn", "As", "Se", "Br",
"Rb", "Sr", "Zr", "Mo", "Sn", "Ba", "Pb"), class = "factor"),
value = c(1.0524, 1.03928, 1.02676, 1.02109, 1.03339, 1.01775,
1.03453, 1.00184, 1.00984, 1.04685, 1.02286, 1.00185, 1.04961,
1.0114, 1.05039, 1.01575, 1.04406, 1.04706, 1.06055, 104.4,
20.9, 24.7, 228.4, 147.3, 30.5, 55.1, 29.8, 13.8, 11, 9.8,
129.8, 79.9, 104.6, 137.9, 148.7, 351.3, 15.9, 70.6, 4.1,
19.9, 13.4, 4.2, 6.5, 1.5, 4.3, 1.1, 2.1, 1.7, 2.3, 5.5)), .Names
= c("SAMPLE",
"AGE", "COMPO", "LOC", "variable", "value"), row.names = c(NA,
50L), class = "data.frame")
>

Brandon Hurr

unread,
Mar 10, 2011, 6:26:55 AM3/10/11
to alobo, ggplot2
How about using colour or fill to have them right next to each other?

For example, 


ggplot(a, aes(x = variable, y = value)) + geom_boxplot(aes(colour=AGE))+  scale_y_continuous(trans="log10")



B

alobo

unread,
Mar 10, 2011, 6:37:34 AM3/10/11
to ggplot2
Just perfect!
Agus

On Mar 10, 12:26 pm, Brandon Hurr <brandon.h...@gmail.com> wrote:
> How about using colour or fill to have them right next to each other?
>
> For example,
>
> ggplot(a, aes(x = variable, y = value)) +
> geom_boxplot(aes(colour=AGE))+  scale_y_continuous(trans="log10")
>
> B
>
Reply all
Reply to author
Forward
0 new messages