更改 facet_grid 中的分面标签并同时添加 geom_vline 时出错
Error occurs when changing facet labels in facet_grid and add geom_vline at the same time
我试图在不修改数据框的情况下更改分面标签,并在绘图中添加一条垂直线以增强可理解性。
library(ggplot2)
df <- data.frame(weeks = rep(-3:3, each = 2),
is_manual = rep(c(TRUE, FALSE), times = 7),
value = c(rnorm(7, 10), rnorm(7, 20)))
# Plotting
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
给我这个,效果很好:
现在我想更改分面标签,以便每个人都知道什么是 TRUE 和 FALSE。
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(ifelse(is_manual, "Manual", "Uploaded") ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
,但它 returns 一个错误:
Error in ifelse(is_manual, "Manual", "Uploaded"): object 'is_manual' not found
但是,一旦我删除 geom_vline
部分,它就可以正常工作,这意味着应该可以找到 'is_manual'。
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(ifelse(is_manual, "Manual", "Uploaded") ~ .)
我可以通过
解决
df$is_manual <- ifelse(df$is_manual, "Manual", "Uploaded")
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
,但它改变了我的基础数据。
有没有一种方法可以在不更改数据框内容的同时更改分面标签和添加垂直线?或者这是一个需要报告的错误?
只是猜测,但也许 geom_vline
正在从传递给 ggplot
的原始数据框 df
中获取分面标签名称,而不是从 [= 中使用的更新公式中获取14=],当 geom_vline
无法弄清楚刻面在哪里时导致错误。
在任何情况下,无需更改基础数据,您可以使用 dplyr
管道 (%>%
) 即时更新它并避免错误:
library(tidyverse)
ggplot(df %>% mutate(is_manual = ifelse(is_manual, "Manual", "Uploaded"),
aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
我试图在不修改数据框的情况下更改分面标签,并在绘图中添加一条垂直线以增强可理解性。
library(ggplot2)
df <- data.frame(weeks = rep(-3:3, each = 2),
is_manual = rep(c(TRUE, FALSE), times = 7),
value = c(rnorm(7, 10), rnorm(7, 20)))
# Plotting
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
给我这个,效果很好:
现在我想更改分面标签,以便每个人都知道什么是 TRUE 和 FALSE。
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(ifelse(is_manual, "Manual", "Uploaded") ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
,但它 returns 一个错误:
Error in ifelse(is_manual, "Manual", "Uploaded"): object 'is_manual' not found
但是,一旦我删除 geom_vline
部分,它就可以正常工作,这意味着应该可以找到 'is_manual'。
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(ifelse(is_manual, "Manual", "Uploaded") ~ .)
我可以通过
解决df$is_manual <- ifelse(df$is_manual, "Manual", "Uploaded")
ggplot(df, aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)
,但它改变了我的基础数据。
有没有一种方法可以在不更改数据框内容的同时更改分面标签和添加垂直线?或者这是一个需要报告的错误?
只是猜测,但也许 geom_vline
正在从传递给 ggplot
的原始数据框 df
中获取分面标签名称,而不是从 [= 中使用的更新公式中获取14=],当 geom_vline
无法弄清楚刻面在哪里时导致错误。
在任何情况下,无需更改基础数据,您可以使用 dplyr
管道 (%>%
) 即时更新它并避免错误:
library(tidyverse)
ggplot(df %>% mutate(is_manual = ifelse(is_manual, "Manual", "Uploaded"),
aes(x = weeks, y = value)) + geom_line() +
facet_grid(is_manual ~ .) +
geom_vline(xintercept = 0, color = "blue", linetype = 2)