使用 facet_wrap() 时在 ggplot 中手动标记轴
Manually label axis in ggplot when using facet_wrap()
我正在尝试手动标记我的 x 轴,但是当我对绘图进行分面并释放 x 轴的比例时,标签变得不正确。我想知道是否有任何方法可以防止这种情况发生而不必事先手动更改我的字符串。
当我使用 scale_x_discrete()
手动标记 x 轴时,标签是正确的
library(tidyverse)
df <- data.frame(trial = rep(c("a", "b", "c"), each = 30),
values = rnorm(n = 90, mean = 0, sd = 1),
variable = c(rep("n", times = 60), rep("m", times = 30))
)
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c("A", "B", "C")) +
theme(legend.position = "none")
Plot 1
然而,当我对情节进行分面时,标签变得不正确。
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c("A", "B", "C")) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
Plot 2
试试这个:
library(ggplot2)
library(dplyr)
#Code
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(breaks=c('a','b','c'),labels = c("A", "B", "C")) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
输出:
或者您也可以尝试使用 mutate()
:
格式化变量
#Code 2
df %>%
mutate(trial=factor(trial,levels = unique(trial),labels = c("A", "B", "C"))) %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
相同的输出。或遵循 @cookesd 的重要建议(非常感谢并感谢他):
#Code 3
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c('a' = 'A', 'b' = 'B', 'c' = 'C')) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
相同的输出。
我们可以使用 fct_recode
从 forcats
library(forcats)
library(dplyr)
library(ggplot2)
df %>%
mutate(trial = fct_recode(trial, A= 'a', B = 'b', C = 'c')) %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
-输出
我正在尝试手动标记我的 x 轴,但是当我对绘图进行分面并释放 x 轴的比例时,标签变得不正确。我想知道是否有任何方法可以防止这种情况发生而不必事先手动更改我的字符串。
当我使用 scale_x_discrete()
手动标记 x 轴时,标签是正确的
library(tidyverse)
df <- data.frame(trial = rep(c("a", "b", "c"), each = 30),
values = rnorm(n = 90, mean = 0, sd = 1),
variable = c(rep("n", times = 60), rep("m", times = 30))
)
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c("A", "B", "C")) +
theme(legend.position = "none")
Plot 1
然而,当我对情节进行分面时,标签变得不正确。
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c("A", "B", "C")) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
Plot 2
试试这个:
library(ggplot2)
library(dplyr)
#Code
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(breaks=c('a','b','c'),labels = c("A", "B", "C")) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
输出:
或者您也可以尝试使用 mutate()
:
#Code 2
df %>%
mutate(trial=factor(trial,levels = unique(trial),labels = c("A", "B", "C"))) %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
相同的输出。或遵循 @cookesd 的重要建议(非常感谢并感谢他):
#Code 3
df %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
scale_x_discrete(labels = c('a' = 'A', 'b' = 'B', 'c' = 'C')) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
相同的输出。
我们可以使用 fct_recode
从 forcats
library(forcats)
library(dplyr)
library(ggplot2)
df %>%
mutate(trial = fct_recode(trial, A= 'a', B = 'b', C = 'c')) %>%
ggplot(aes(x = trial, y = values)) +
geom_violin(aes(fill = trial)) +
facet_wrap(~ variable, scales = "free_x") +
theme(legend.position = "none")
-输出