Mark Miller Mark Miller - 1 year ago 119
R Question

ggplot boxplot: log10 y axis AND forced 0-100 range

I'd like to plot some data histogram-style. The data is scaled so that one single observation has a value of 100. However, the data will be broken up by category into separate sections of a document, so the maximum value of most groups will not be 100.

I'd like my plots to use a log10 y axis, and to have use a fixed 0 to 100 scale.

The following honors the log transformation, but ignores the 0-100 range with a warning. I have tried other permutations of coord_cartesian(ylim = c(0, 100)) and scale_y_continuous(trans = log10_trans()), with no luck yet.


toread <- "general specific satisfaction
fruit apple 9
fruit apple 8
fruit banana 8
fruit banana 7
fruit pear 6
veg carrot 7
veg celery 4
veg turnip 3
veg turnip 2
veg turnip 1
grain pasta 6
grain quinoa 3
grain brownrice 2
grain brownrice 6"

foodprefs <- read.table(textConnection(toread), header = TRUE)

foodprefs$pct.max <- (foodprefs$satisfaction / max(foodprefs$satisfaction)) * 100

as.character(foodprefs$general))), function( {

temp <- foodprefs[foodprefs$general == , ]

ggplot(temp, aes(x = specific, y = pct.max)) +
geom_boxplot() +
ylim(0, 100) +
scale_y_log10() +

Answer Source

Your code has two problems:

  1. Invoking ylim() establishes a scale for the y-axis, which is why you get the warning about specifying a second scale (the log scale), which will overwrite the first. This is why your ylims weren't "sticking".
  2. log10(0) = Infinite, which cannot be plotted, so that limit is invalid for the log scale function.

As an aside, your code can be simplified with the plyr package. The following code uses scale_y_log10 to specify the limits, and solves both problems. Additionally, the use of plyr makes the code cleaner.

dlply(foodprefs, .(general), function( {

    ggplot(, aes(x = specific, y = pct.max)) +
        geom_boxplot() +
        scale_y_log10(limits = c(1, 100)) +

enter image description here

I'm not sure that a log scale makes for a great visualization here, but there you go.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download