如何只删除一些刻面标签?

问题描述 投票:10回答:2

使用facet_wrap,是否可以只删除一些刻面标签?在以下示例中,我希望Species标签仅出现在每行的第一列中。我知道我可以使用labeller函数,但不能使用如何更改单个标签。

data(iris)
library(tidyr)
library(ggplot2)

dat <- iris %>%
  gather(var, val, Sepal.Length:Petal.Width) 

ggplot(dat) +
  geom_point(aes(x = 1, y = val)) +
  facet_wrap(Species~var)

enter image description here

r ggplot2 facet-wrap
2个回答
5
投票

它并不完美,但我会发布这个希望仍然比没有好。

使用as_labeller()labeller()可以满足您的需求。

更新

最简单的解决方案是将Speciesvar分成两个labellers函数。

facet_labeller_top <- function(variable, value) {
  c(
    "Setosa", 
    "",
    "",
    "",
    "Versicolor", 
    "",
    "",
    "",
    "Virginica", 
    "",
    "",
    ""
  )
}

facet_labeller_bottom <- function(variable, value) {
  c(
    "Petal.Length", 
    "Petal.Width",
    "Sepal.Length",
    "Sepal.Width",
    "Petal.Length", 
    "Petal.Width",
    "Sepal.Length",
    "Sepal.Width",
    "Petal.Length", 
    "Petal.Width",
    "Sepal.Length",
    "Sepal.Width"
  )
}

结果:

ggplot(dat) +
  geom_point(aes(x = 1, y = val)) +
  facet_wrap(Species~var, labeller = labeller(Species=as_labeller(facet_labeller_top),
                                              var = as_labeller(facet_labeller_bottom)))

enter image description here


1
投票

我不知道我是否理解得很好,但我会尝试:

您可以使用facet_grid而不是facet_wrap

这是代码:

data(iris)
library(tidyr)
library(ggplot2)

dat <- iris %>%
  gather(var, val, Sepal.Length:Petal.Width) 

ggplot(dat) +
  geom_point(aes(x = 1, y = val)) +
  facet_grid(Species~var)

这是结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.