Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing format of some axis labels in ggplot2 according to condition

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.

like image 547
PaoloCrosetto Avatar asked Dec 16 '13 11:12

PaoloCrosetto


People also ask

How do I customize axis labels in R?

Key ggplot2 R functions p + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.

How do I make my axis labels bigger in ggplot2?

To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element.


1 Answers

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)))

enter image description here

like image 188
Didzis Elferts Avatar answered Sep 25 '22 12:09

Didzis Elferts