How can I adjust the y axis limits to show this data clearly? Normally
I would use coord_cartesian for this, however that only allows me to
set a single pair of y limits that applies to all facets (unless I
have missed something).
An example is:
test <- data.frame(animal = rep(c("cattle","sheep","none"),120),
irrigation = rep(rep(c("dry","wet"),each=3),60), paddock =
rep(c(1:3),each=120), value = c(1:358,2000,5000))
ggplot(test, aes(reorder(animal,value,mean),value)) +
stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data =
mean_cl_normal, geom = "pointrange") + facet_grid(paddock ~ ., scales
= "free")
On this example, how would I either:
- Manually set ymax to around 80, 210 and 1000 for the three facets,
or
- Persuade scales = "free" to scale the axis to the visible data only
rather than the full dataset.
For reference, this question is related to the following two previous
posts but not answered in them:
http://groups.google.com/group/ggplot2/browse_thread/thread/6ded9538cd848279
http://groups.google.com/group/ggplot2/browse_thread/thread/bb96b59a99c947b1/1c766aabd12bf81b
> posts but not answered in them:http://groups.google.com/group/ggplot2/browse_thread/thread/6ded9538c...http://groups.google.com/group/ggplot2/browse_thread/thread/bb96b59a9...
--
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
"Beware of dynamite!"
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
ggplot(data=wormfake, aes(harvest, tha/10, colour = irrigation)) +
geom_point(aes(shape = N), position = position_jitter(width = 0.1),
alpha = 1/2) +
geom_errorbar(aes(ymin=min/10, ymax=max/10), data=wormfake2, width =
0.1, size = 1) +
geom_line(aes(group = irrigation), data = wormfake2, size = 1) +
facet_wrap(~ invertebrate, nrow = 2)
A few comments:
* I don't think free scales help here (maybe they do in your real
data), and aligning the data in columns makes it very hard to compare
species
* Given that there isn't a significant nitrogen effect, you might
consider dropping it from this plot altogether
* I like being able to see the raw data, but faintly, in the
background. This plot still needs tweaking to make the lines and
error bars pop out over the raw data - see another attempt below.
ggplot(data=wormfake, aes(harvest, tha/10, colour = irrigation)) +
geom_point(aes(shape = N), position = position_jitter(width = 0.1),
alpha = 1/2) +
geom_errorbar(aes(ymin=min/10, ymax=max/10), data=wormfake2, width =
0.12, size = 1.5, colour = "black") +
geom_line(aes(group = irrigation), data = wormfake2, size = 1.5,
colour = "black") +
geom_errorbar(aes(ymin=min/10, ymax=max/10), data=wormfake2, width =
0.1, size = 1) +
geom_line(aes(group = irrigation), data = wormfake2, size = 1) +
facet_wrap(~ invertebrate, nrow = 2)
* Lines can be very effective at forming groups. The shape of the
lines here is also helpful for detecting interactions.
* Are you sure you want 95% confidence intervals? People will try and
use them to determine if dry is different to irrigated for a given
harvest/species combination. If you want to use them for comparison
you want (IIRC) 84% CIs
Hadley
I took a different direction from Hadley; there are several graphs to
choose from below. The primary thing I did differently was to facet by
both harvest and invertebrate so that the comparisons among nitrogen
and irrigation would be easier to visualize. Comparisons across the
columns would give some indication of how harvests compared for a
given invertebrate.
(And I thank Hadley for pointing out the dynamite plot references -
that's usually my rant :)
# The first two plots use geom_pointrange() - one should not
# take the overlap (or lack thereof) between apposed pointranges
# as representing (non)significant differences. These plot the
# min and max from the wormfake1 data frame; the large point
# represents the group average.
# irrigation is the x variable, N the color aesthetic
ggplot(data = wormfake1, aes(irrigation, tha, colour = N)) +
geom_pointrange(aes(ymin = min, ymax = max),
position = position_dodge(width = 0.4),
width = 0.2, size = 1) +
facet_grid(invertebrate ~ harvest, scales = "free_y") +
scale_colour_manual(breaks = levels(wormfake1$N),
values = c("grey50","grey20"), legend=TRUE) +
labs(x = NULL,
y = "Total mass of earthworms (kg / square m)",
colour = "Nitrogen")
# switch: N is the x variable, irrigation the color aesthetic
ggplot(data = wormfake1, aes(N, tha, colour = irrigation)) +
geom_pointrange(aes(ymin = min, ymax = max),
position = position_dodge(width = 0.4),
width = 0.2, size = 1) +
facet_grid(invertebrate ~ harvest, scales = "free_y") +
scale_colour_manual(breaks = levels(wormfake1$irrigation),
values = c("grey50","grey20"), legend=TRUE) +
labs(x = NULL,
y = "Total mass of earthworms (kg / square m)",
colour = "Irrigation")
# Interaction plot between N and irrigation within panel.
# Again, one should not attach too much importance to
# the nonparallelism of profiles since no measure of variation
# is present in the plot
ggplot(data = wormfake1, aes(irrigation, tha, colour = N)) +
geom_point(size = 3) +
geom_line(aes(group = N), size = 1) +
facet_grid(invertebrate ~ harvest, scales = "free_y") +
scale_colour_manual(breaks = levels(wormfake1$N),
values = c("grey50","grey20"), legend=TRUE) +
labs(x = NULL,
y = "Total mass of earthworms (kg / square m)",
colour = "Nitrogen")
# Assuming that the nitrogen effect is nonsignificant, one can
# compare the distributions by irrigation type in each panel
# with boxplots as follows (n = 20 per group):
ggplot(data = wormfake, aes(irrigation, tha, colour = irrigation,
fill = irrigation)) +
geom_boxplot(outlier.size = 0) + # since points included
geom_point(position = position_jitter(width = 0.2)) +
facet_grid(invertebrate ~ harvest, scales = "free_y") +
scale_colour_manual(breaks = levels(wormfake$irrigation),
values = c("grey40","grey20")) +
scale_fill_manual(breaks = levels(wormfake$irrigation),
values = c("grey70","grey50")) +
labs(x = NULL,
y = "Total mass of earthworms (kg / square m)",
colour = "Irrigation", fill = 'Irrigation')
HTH,
Dennis