简体   繁体   中英

facet_wrap log_transforming the axis in one column

I have a 2 by 2 grid created by facet_grid with free scales (see example). I want to have a log scale for the figures in the second row of the grid but not on the first row.

Example code:

data("mtcars")

ggplot(data = mtcars, aes(y = mpg, x = as.factor(cyl))) + geom_boxplot() + facet_wrap(~ vs + am, nrow = 2, scales = "free")

scale_y_log10() seems to do something strange. Is that at all possible or do I have to use something like grid.arrange ?

We could transform then plot:

library(ggplot2)
library(dplyr)

# manual transform
plotDat <- mtcars %>% 
  mutate(mpg = if_else(vs == 1, log10(mpg), mpg), 
         cyl = as.factor(cyl))

# then plot
ggplot(data = plotDat, aes(y = mpg, x = cyl)) +
  geom_boxplot() +
  facet_wrap(~ vs + am, nrow = 2, scales = "free")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM