Hello my goal is to create a plot with 3 box plots. I have a data frame with a column of numerical data and a factor variable with 2 levels. I am trying to group those levels into an additional level. What R code can I use to make a factor variable with 3 levels— “var1”, “var2”, “var1+var2”.
library(tidyverse)
set.seed(42)
(exampl_start <- tibble(
fac = factor(rep(1:2, 5)),
num = sample(10, 10, TRUE)
))
ggplot(
data = exampl_start,
aes(x = fac, y = num)
) +
geom_boxplot()
(ex1 <- mutate(exampl_start,
fac =fct_expand(fac, "3")))
#either
(ex2a <- bind_rows(
ex1,
mutate(ex1, fac = fct_recode(fac,
"3" = "1",
"3" = "2"
))
))
#or
(ex2b <- bind_rows(
ex1,
mutate(ex1, fac="3"
))
%>% mutate(fac=factor(fac)))
#result
ggplot(
data = ex2,
aes(x = fac, y = num)
) +
geom_boxplot()
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.