英文:
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)) :
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)) :
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 <- 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)) :
<text>: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)) :
<text>: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)
创建于2023-06-14,使用 reprex v2.0.2
<details>
<summary>英文:</summary>
You don't quite have the syntax right here. The error message you are getting is because `labels_parsed` doesn't have access to the data frame you have piped in and doesn'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 <- 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)
<sup>Created on 2023-06-14 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论