使用另一个变量更改ggplot的facet标签。

huangapple go评论89阅读模式
英文:

ggplot change facet label using another variable

问题

I'm trying to use the names of a different variable as my facet labels in ggplot. However, this variable has subscripts and spaces, the latter I think are resulting in errors when I try to use this variable as a label. How do I fix this? Changing the variable names in grps is not an option.

grpOrder <- c("B", "AB", "A")
data.frame(y = sample(0:20, 30, replace = TRUE), x = sample(0:20, 30, replace = TRUE), grps = fct_relevel(rep(c("A", "B", "AB"), each = 10), grpOrder), labs = rep(c("Aaaaaa[1]", "Bbbb[3]", "Ababa Bbbaaa"), each = 10)) %>% ggplot(aes(x = x, y = y)) + geom_point() + facet_wrap(~grps, labeller = label_parsed(labels = labs))
Error in parse(text = as.character(values)) : :2:0: unexpected end of input 1: { ^

I've tried putting '~' as spaces, but that didn't help:

data.frame(y = sample(0:20, 30, replace = TRUE), x = sample(0:20, 30, replace = TRUE), grps = fct_relevel(rep(c("A", "B", "AB"), each = 10), grpOrder), labs = rep(c("Aaaaaa[1]", "Bbbb[3]", "Ababa~Bbbaaa"), each = 10)) %>% ggplot(aes(x = x, y = y)) + geom_point() + facet_wrap(~grps, labeller = label_parsed(labels = labs))
Error in parse(text = as.character(values)) : :2:0: unexpected end of input 1: { ^

I have to use grps as the faceting variable because the facets are in a very specific order. There are a LOT of facets in my actual dataset, so recoding everything manually is not an option.

英文:

I'm trying to use the names of a different variable as my facet labels in ggplot. However, this variable has subscripts and spaces, the latter I think are resulting in errors when I try to use this variable as a label. How do I fix this? Changing the variable names in grps is not an option.

  1. grpOrder &lt;- c(&quot;B&quot;, &quot;AB&quot;, &quot;A&quot;)
  2. data.frame(y = sample(0:20, 30, replace = TRUE),
  3. x = sample(0:20, 30, replace = TRUE),
  4. grps = fct_relevel(rep(c(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10), grpOrder),
  5. labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa Bbbaaa&quot;), each = 10)) %&gt;%
  6. ggplot(aes(x = x, y = y)) +
  7. geom_point() +
  8. facet_wrap(~grps, labeller = label_parsed(labels = labs))
  9. Error in parse(text = as.character(values)) :
  10. &lt;text&gt;:2:0: unexpected end of input
  11. 1: {
  12. ^

I've tried putting '~' as spaces, but that didn't help:

  1. data.frame(y = sample(0:20, 30, replace = TRUE),
  2. x = sample(0:20, 30, replace = TRUE),
  3. grps = fct_relevel(rep(c(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10), grpOrder),
  4. labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa~Bbbaaa&quot;), each = 10)) %&gt;%
  5. ggplot(aes(x = x, y = y)) +
  6. geom_point() +
  7. facet_wrap(~grps, labeller = label_parsed(labels = labs))
  8. Error in parse(text = as.character(values)) :
  9. &lt;text&gt;:2:0: unexpected end of input
  10. 1: {
  11. ^

I have to use grps as the faceting variable because the facets are in a very specific order. There are a LOT of facets in my actual dataset, so recoding everything manually is not an option.

答案1

得分: 0

你的代码中存在一些语法问题。你收到的错误消息是因为 labels_parsed 无法访问你传递的数据框,并且不使用数据屏蔽,所以它试图解析“函数” labs 而不是“列” labs

由于你已经有了一个名为 labs 的列,只需按照该列进行分面化,并将 labels_parsed 作为函数而不是调用传递即可。

如果你需要标签与组的顺序相同,可以提前执行以下操作:

  1. library(tidyverse)
  2. df <- data.frame(y = sample(0:20, 30, replace = TRUE),
  3. x = sample(0:20, 30, replace = TRUE),
  4. grps = fct_relevel(rep(c("A", "B", "AB"), each = 10),
  5. c("B", "AB", "A")),
  6. labs = rep(c("Aaaaaa[1]", "Bbbb[3]", "Ababa~Bbbaaa"), each = 10))
  7. df$labs <- factor(df$labs,
  8. as.character(unique(df$labs))[unique(as.numeric(df$grps))])
  9. ggplot(df, aes(x = x, y = y)) +
  10. geom_point() +
  11. facet_wrap(~labs, labeller = label_parsed)

使用另一个变量更改ggplot的facet标签。

创建于2023-06-14,使用 reprex v2.0.2

  1. <details>
  2. <summary>英文:</summary>
  3. You don&#39;t quite have the syntax right here. The error message you are getting is because `labels_parsed` doesn&#39;t have access to the data frame you have piped in and doesn&#39;t use data masking, so it is trying to parse the _function_ `labs`, not the _column_ `labs`.
  4. Since you already have a `labs` column, simply facet by that and pass `labels_parsed` as a function rather than a call.
  5. If you need the labels to be in the same order as the groups, you can do this in advance:
  6. ``` r
  7. library(tidyverse)
  8. df &lt;- data.frame(y = sample(0:20, 30, replace = TRUE),
  9. x = sample(0:20, 30, replace = TRUE),
  10. grps = fct_relevel(rep(c(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10),
  11. c(&quot;B&quot;, &quot;AB&quot;, &quot;A&quot;)),
  12. labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa~Bbbaaa&quot;), each = 10))
  13. df$labs &lt;- factor(df$labs,
  14. as.character(unique(df$labs))[unique(as.numeric(df$grps))])
  15. ggplot(df, aes(x = x, y = y)) +
  16. geom_point() +
  17. facet_wrap(~labs, labeller = label_parsed)

使用另一个变量更改ggplot的facet标签。

<sup>Created on 2023-06-14 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月15日 05:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定