- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display positive sign for X-axis labels in R using ggplot2?
By default, the positive signs are not displayed in any plot in R. It is well known that if there is no sign seen with any value then it is considered positive, therefore, we do not need the sign but to distinguish between 0 and positive values it could be done. To display positive sign for X-axis labels, we can use scale_x_continuous function.
Consider the below data frame −
Example
x<-rnorm(20) y<-rpois(20,2) df<-data.frame(x,y) df
Output
x y 1 0.6340310 3 2 1.2107246 2 3 -1.1054617 1 4 -0.5074514 2 5 1.4542955 2 6 0.5501350 3 7 -1.5630402 5 8 -0.8708720 5 9 1.8475619 0 10 -0.9714984 0 11 -0.5837147 1 12 -2.4681126 2 13 -0.2683293 3 14 0.9019841 1 15 -0.1082968 0 16 1.5797151 0 17 1.0914647 1 18 1.4080222 0 19 0.6156943 0 20 0.6528113 1
Loading ggplot2 package and creating a scatterplot between x and y−
Example
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
Creating scatterplot between x and y having positive labels of X displayed with positive signs −
Example
ggplot(df,aes(x,y))+geom_point()+scale_x_continuous(labels=function(x) sprintf("%+d",x))
Output
- Related Articles
- How to display Y-axis with Euro sign using ggplot2 in R?
- How to increase the X-axis labels font size using ggplot2 in R?
- How to create a boxplot using ggplot2 for single variable without X-axis labels in R?
- How to change ordinal X-axis label to text labels using ggplot2 in R?
- How to X-axis labels to the top of the plot using ggplot2 in R?
- How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?
- How to set the X-axis labels in histogram using ggplot2 at the center in R?
- How to change the orientation and font size of x-axis labels using ggplot2 in R?
- How to display long X-axis labels in a bar chart using plotly in R?
- How to display X-axis labels inside the plot in base R?
- How to display X-axis labels with dash in base R plot?
- How to display count on Y-axis for line chart using ggplot2 in R?
- How to display 0 at Y-axis using ggplot2 in R?
- How to display negative labels below bars in barplot using ggplot2 in R?
- How to display axes ticks and labels inside the plot using ggplot2 in R?

Advertisements