简体   繁体   English

使用 labeller 添加单元以使用 ggplot2 和 facet_wrap 剥离标签

[英]Using labeller to add units to strip labels with ggplot2 and facet_wrap

I am making faceted plots in ggplot2. The groups I am faceting by have names that should logically include units, and I'd like to automatically add these units to the strip labels without manually writing out a new vector with all the labels - ie replace labels of "1", "2", "3" with "1 mg", "2 mg", "3 mg".我正在 ggplot2 中制作分面图。我所分面的组的名称在逻辑上应该包含单位,我想自动将这些单位添加到条带标签中,而无需手动写出包含所有标签的新向量 - 即替换“1”、“2”、“3”的标签带有“1 mg”、“2 mg”、“3 mg”。 I was able to do this using code analogous to the simplified example below.我能够使用类似于下面简化示例的代码来执行此操作。 But it's still a little clunky to have to define the label vector separately and I'm wondering if anyone knows of a way to do this within the labeller function itself?但是必须单独定义 label 向量仍然有点笨拙,我想知道是否有人知道在贴标机 function 本身中执行此操作的方法? It seems like this would be a fairly common scenario so I was surprised not to find more examples of how others have done it online.这似乎是一个相当普遍的场景,所以我很惊讶没有找到更多其他人如何在网上完成它的例子。 Thanks in advance!提前致谢!

df <- tibble(
  group = factor(rep(1:3, times = 5)),
  output = sample(1:10, 15, replace = TRUE)
  )

labs <- paste(levels(df$group), "mg")
names(labs) <- levels(df$group)

df %>%
  ggplot()+
  geom_boxplot(aes(y = output))+
  facet_wrap(vars(group), labeller = labeller(group = labs)

Using a lambda or anonymous function in labeller you could do:labeller中使用 lambda 或匿名 function 你可以这样做:

library(ggplot2)

ggplot(df) +
  geom_boxplot(aes(y = output))+
  facet_wrap(vars(group), labeller = labeller(group = ~ paste(.x, "mg")))

I think you would typically modify the dataframe before creating the plot to do that.我认为您通常会在创建 plot 之前修改 dataframe 来执行此操作。 This might not be what you are looking for but I would think this is the most practical solution这可能不是您要找的,但我认为这是最实用的解决方案

df2 <- 
  df %>%
  mutate(
    group2 = paste(group, "mg")
  )

ggplot(df2) +
  geom_boxplot(aes(y = output)) +
  facet_wrap(vars(group2))

You can create your own labeller function, as described here: https://ggplot2.tidyverse.org/reference/labeller.html您可以创建自己的标签器 function,如下所述: https://ggplot2.tidyverse.org/reference/labeller.html

For example,例如,

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- tibble(
  group = factor(rep(1:3, times = 5)),
  output = sample(1:10, 15, replace = TRUE)
)

unit_labeller <- function(string) {
  labeled_string <- paste(string, "mg")
  return(labeled_string)
}

df %>%
  ggplot()+
  geom_boxplot(aes(y = output))+
  facet_wrap(vars(group), labeller = labeller(group = unit_labeller))

Created on 2023-01-31 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2023-01-31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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