This falls into the category of "scale transformations in ggplot2 take
place before statistical transformations, which in turn take place
before coordinate transformations". By using scale_y_continuous() in
your call, you have excluded all of the observations outside of (4.5,
5.5) **before computing a boxplot**, which is a statistical
transformation of the data. This is why you get the observed results.
As the other poster correctly diagnosed, if you change the range of
the y-scale *after* the boxplots have been constructed, then the
perceived anomaly disappears.
For the sake of readability, I'd suggest placing the medians slightly
above the median line and round them to something sensible - I doubt
that you need 8-10 digit accuracy on a graph. I'm also concerned about
zooming in on the boxplots when it is often the case that the most
interesting observations are in the extremes of the distribution, but
you may have reasonable grounds for doing that.
# Generate random dataframe of values and factors
df <- data.frame(value = rnorm(5000, mean = 5, sd = 0.5),
f = factor(sample(1:5, 5000, replace = TRUE),
levels = 1:5))
df.m <- aggregate(value ~ f, data = df,
FUN = function(x) round(median(x), 4)) # round
median to 4 digits
# Plot the dataframe value by boxplot and overlay df.m as a label directly
# Insert label 0.05 units above the median line
ggplot(data = df, aes(x = f, y = value)) + geom_boxplot() +
geom_text(data = df.m, aes(y = value + 0.05, label = value)) +
coord_cartesian(y = c(4.5, 5.5))
Dennis
> --
> --
> 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/d/optout.