How to get axis ticks labels with different colors within a single axis for a ggplot graph?

后端 未结 2 1740
粉色の甜心
粉色の甜心 2020-12-09 05:28

Consider a simple ggplot2 graph

library(ggplot2) 
dat <- data.frame(name=c(\"apple\", \"orange\", \"plum\"),value=c(3,8,2),outlier=c(FALSE,TRUE,FALSE))
gg         


        
2条回答
  •  被撕碎了的回忆
    2020-12-09 05:35

    A simpler way (IMO) to do this is just create a conditional color vector and parse it into axis.text.y

    dat <- data.frame(name=c("apple", "orange", "plum"),value=c(3,8,2),outlier=c(FALSE,TRUE,FALSE))
    colvec <- character(dim(dat)[1])
    colvec <- ifelse(dat$outlier, "red", "black")
    
    library(ggplot2) 
    ggplot(dat) +
    geom_point(data = dat, aes(x=value,y=name)) +
    theme(axis.text.y = element_text(colour=colvec))
    

    enter image description here

提交回复
热议问题