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

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

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.

grpOrder &lt;- c(&quot;B&quot;, &quot;AB&quot;, &quot;A&quot;)
data.frame(y    = sample(0:20, 30, replace = TRUE),
           x    = sample(0:20, 30, replace = TRUE),
           grps = fct_relevel(rep(c(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10), grpOrder),
           labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa Bbbaaa&quot;), each = 10)) %&gt;% 
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~grps, labeller = label_parsed(labels = labs))
Error in parse(text = as.character(values)) : 
  &lt;text&gt;: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(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10), grpOrder),
           labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa~Bbbaaa&quot;), each = 10)) %&gt;% 
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~grps, labeller = label_parsed(labels = labs))
Error in parse(text = as.character(values)) : 
  &lt;text&gt;: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.

答案1

得分: 0

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

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

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

library(tidyverse)

df <- 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), 
                              c("B", "AB", "A")),
           labs = rep(c("Aaaaaa[1]", "Bbbb[3]", "Ababa~Bbbaaa"), each = 10))
  
df$labs <- factor(df$labs, 
                  as.character(unique(df$labs))[unique(as.numeric(df$grps))])

ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~labs, labeller = label_parsed)

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

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


<details>
<summary>英文:</summary>

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`.

Since you already have a `labs` column, simply facet by that and pass `labels_parsed` as a function rather than a call. 

If you need the labels to be in the same order as the groups, you can do this in advance:
``` r
library(tidyverse)

df &lt;- data.frame(y    = sample(0:20, 30, replace = TRUE),
           x    = sample(0:20, 30, replace = TRUE),
           grps = fct_relevel(rep(c(&quot;A&quot;, &quot;B&quot;, &quot;AB&quot;), each = 10), 
                              c(&quot;B&quot;, &quot;AB&quot;, &quot;A&quot;)),
           labs = rep(c(&quot;Aaaaaa[1]&quot;, &quot;Bbbb[3]&quot;, &quot;Ababa~Bbbaaa&quot;), each = 10))
  
df$labs &lt;- factor(df$labs, 
                  as.character(unique(df$labs))[unique(as.numeric(df$grps))])

ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  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:

确定