Mark Miller - 2 months ago 14

R Question

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

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.

`library(ggplot2)`

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)

closeAllConnections()

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

lapply(sort(unique(

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

temp <- foodprefs[foodprefs$general == one.cat , ]

ggplot(temp, aes(x = specific, y = pct.max)) +

geom_boxplot() +

ylim(0, 100) +

scale_y_log10() +

coord_flip()

})

Answer

Your code has two problems:

- 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". `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.

```
library(plyr)
dlply(foodprefs, .(general), function(one.cat) {
ggplot(one.cat, aes(x = specific, y = pct.max)) +
geom_boxplot() +
scale_y_log10(limits = c(1, 100)) +
coord_flip()
})
```

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

Source (Stackoverflow)

Comments