在一列中对轴进行对数变换,使用facet_wrap进行分面呈现。

4

我有一个由facet_grid创建的2x2网格,其中包含自由比例尺(请参见示例)。我想在网格的第二行图形上使用对数刻度,但不在第一行上使用。

示例代码:

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() 似乎做了一些奇怪的事情。这是否有可能,还是我必须使用像 grid.arrange 这样的东西?


1
@zx8754的回答是一个好的、快速有效地达到你的结果的方法。然而,作为原则问题,facets被设计成具有共同刻度的小倍数。因此,你有点逆流而上。对于一个整洁的解决方案,例如分别使用y轴标签,你将不得不产生单独的图形。你可以用grid.arrage组合它们,或者看看cowplot::plot_grid以便轻松对齐。 - Axeman
1个回答

3
我们可以对其进行转换再绘制:
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")

1
我猜你得做一些智能的方面标注。 - Axeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接