I want to set positon
with some conditions, but an error occured when I use if_else()
.
A reproducible example is below.
My question is :
How can I use conditions in position
in ggplot2.
library(dplyr)
pd_ture = position_dodge(.4)
pd_false = position_dodge(.6)
mtcars_true = mtcars %>% mutate(test = TRUE)
mtcars_false =mtcars %>% mutate(test = FALSE)
ggplot(mtcars_true, aes(factor(cyl), fill = factor(vs))) +
geom_bar(position = if_else(unique(mtcars_true$test), pd_ture, pd_false))
# Error in true[rep(NA_integer_, length(condition))] :
# object of type 'environment' is not subsettable
ggplot(mtcars_false, aes(factor(cyl), fill = factor(vs))) +
geom_bar(position = if_else(unique(mtcars_false$test), pd_false, pd_ture))
# Error in true[rep(NA_integer_, length(condition))] :
# object of type 'environment' is not subsettable