如何在ggplot图形的单个轴上使用不同颜色的轴刻度标签? [英] How to get axis ticks labels with different colors within a single axis for a ggplot graph?
本文介绍了如何在ggplot图形的单个轴上使用不同颜色的轴刻度标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一个简单的ggplot2图
library(ggplot2)
dat < - data.frame(name = c(3,8,2),outlier = c(FALSE,TRUE,FALSE))
ggplot(dat)+ geom_point(aes(c) x = value,y = name))
有没有办法修改axis y标签的style属性(比如颜色)有条件地,例如根据 dat
?
>中的 outlier
列
结果可能类似于
在具有大量项目的图形上,此功能将大大提高图形的可读性和影响力。
解决方案
更简单的方法(IMO)可以创建一个条件颜色矢量并将其解析为 axis.text.y
dat < - data.frame(name = c(苹果,桔子,李子),值= c(3,8,2),离群值= c(假,真,假))
colvec < - character(dim(dat)[1 ])
colvec < - ifelse(dat $ outlier,red,black)
library(ggplot2)
ggplot(dat)+
geom_point (color = colvec))
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))
ggplot(dat)+geom_point(aes(x=value,y=name))
Is there a way to modify styles attributes of the axis y labels (say color) conditionally, for example depending on the outlier
column in dat
?
The result would be something like
On a graph with a large number of items this feature wold greatly improve the graph readability and impact.
解决方案
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))
这篇关于如何在ggplot图形的单个轴上使用不同颜色的轴刻度标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文