根据条件改变ggplot2中某些轴标签的格式 [英] Changing format of some axis labels in ggplot2 according to condition

查看:154
本文介绍了根据条件改变ggplot2中某些轴标签的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot,我想根据预定义的条件只突出显示一些特定的x轴标签。



我知道轴文本是由

p>

 主题(axis.text = element_text(...))

但这适用于轴的所有标签。我想要的是,格式更改只应用于条件= 1的标签。 可以包含 ifelse()函数在元素文本()中有不同的标签。

  ggplot(iris,aes(Species,Petal.Length))+ geom_boxplot()+ 
theme(axis.text.x =
element_text(face = ifelse(levels(iris $ Species)==setosa,bold,italic)))

或者,您可以在 element_text()中提供与层数相同长度的值向量。

  ggplot(iris,aes(Species,Petal.Length))+ geom_boxplot()+ 
theme(axis.text.x = element_text(face = c(bold,斜体,粗体),
size = c(11,12,13)))


I have a ggplot and I want to highlight only some specific x-axis labels according to a predefined condition.

I know that axis text is controlled by

theme(axis.text = element_text(...))

but this applies to all labels of the axis. What I want is that the formatting change be applied only the labels that have condition = 1.

解决方案

You can include for example ifelse() function inside element_text() to have different labels.

ggplot(iris,aes(Species,Petal.Length))+geom_boxplot()+
  theme(axis.text.x=
          element_text(face=ifelse(levels(iris$Species)=="setosa","bold","italic")))

Or you can provide vector of values inside element_text() the same length as number of levels.

ggplot(iris,aes(Species,Petal.Length))+geom_boxplot()+
 theme(axis.text.x = element_text(face=c("bold","italic","bold"),
                                   size=c(11,12,13)))

这篇关于根据条件改变ggplot2中某些轴标签的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆