How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?

R ProgrammingServer Side ProgrammingProgramming

The values of the categorical variable can be represented by numbers, by characters, by a combination of numbers and characters, by special characters, by numerical signs or any other method. But when we create the bar plot, if the size of a label name is large then we might want to reduce it by representing it with a different word or character or sign that gives the same meaning and it can be done by using expression argument inside scale_x_discrete.


Consider the below data frame −

 Live Demo

> x<-c("0","100","150","200","Greater than 200")
> y<-c(25,28,32,25,37)
> df<-data.frame(x,y)
> df


   x  y
1 0   25
2 100 28
3 150 32
4 200 25
5 Greater than 200 37

Creating the bar plot −

> library(ggplot2)
> ggplot(df,aes(x,y))+geom_bar(stat="identity")


Now suppose, you want to replace “Greater than 200” with >=200 then it can be done as shown below −

> ggplot(df,aes(x,y))+geom_bar(stat="identity")+
+ scale_x_discrete(labels=c("0","100","150","200",expression("">=200)))

Published on 04-Sep-2020 11:56:12