发生的事情确实很奇怪,如果我尝试根据满足的条件分配轴标签的颜色,则返回的图的轴标签的颜色与标准不匹配。
例如
p <- ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
geom_boxplot()
p + theme(axis.text.x = element_text(color = ifelse(mtcars$cyl==4, "red", "black"), angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
返回箱形图,其中“ 8”标记为红色,“ 4”和“ 6”为黑色。但是,如果我在任何情况下都设置了不正确的条件,例如:
p <- ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
geom_boxplot()
p + theme(axis.text.x = element_text(color = ifelse(mtcars$cyl=="something", "red", "black"), angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
所有标签均为黑色。
如果我选择一个都满足的条件,它们都是红色的,例如:
p <- ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
geom_boxplot()
p + theme(axis.text.x = element_text(color = ifelse(mtcars$cyl < 100, "red", "black"), angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
但是,只要其中一些符合条件,我都会得到随机结果。
p <- ggplot(mtcars, …Run Code Online (Sandbox Code Playgroud)