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

bee*_*oot 10 r ggplot2 facet-wrap

使用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

RLa*_*ave 5

这一点都不完美,但是我希望这种希望总比没有好。

使用as_labeller()和labeller()可能会为您提供所需的东西。

更新资料

最简单的解决方案是拆分Species和合并var两个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"
  )
}
Run Code Online (Sandbox Code Playgroud)

结果:

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)))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

数据示例:

library(tidyr)
library(ggplot2)

dat <- iris %>%
  gather(var, val, Sepal.Length:Petal.Width) 
Run Code Online (Sandbox Code Playgroud)