- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 set the X-axis labels in histogram using ggplot2 at the center in R?
The boundary argument of geom_histogram function and breaks argument of scale_x_continuous function can help us to set the X-axis labels in histogram using ggplot2 at the center. We need to be careful about choosing the boundary and breaks depending on the scale of the X-axis values. Check out the below example to understand how it works.
Example
Consider the below data frame −
Example
> x<-rpois(20,5) > df<-data.frame(x) > df
Output
x 1 5 2 7 3 6 4 4 5 7 6 7 7 10 8 3 9 6 10 6 11 5 12 4 13 4 14 6 15 7 16 4 17 1 18 11 19 6 20 9
Loading ggplot2 package and creating the histogram −
Example
> library(ggplot2) > ggplot(df,aes(x))+ geom_histogram(binwidth=1)
Output
Creating the histogram with X-axis labels at the center −
Example
>ggplot(df,aes(x))+geom_histogram(binwidth=1,boundary=-0.5)+ scale_x_continuous(breaks=1:11)
Output
- Related Articles
- How to increase the X-axis labels font size using ggplot2 in R?
- How to X-axis labels to the top of the plot using ggplot2 in R?
- How to display positive sign for X-axis labels in R using ggplot2?
- How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?
- How to change ordinal X-axis label to text labels using ggplot2 in R?
- How to change the orientation and font size of x-axis labels using ggplot2 in R?
- How to create a boxplot using ggplot2 for single variable without X-axis labels in R?
- How to set the Y-axis tick marks using ggplot2 in R?
- How to center labels in a Matplotlib histogram plot?
- How to apply manually created x-axis labels in a histogram created by hist function in R?
- How to change the color of X-axis label using ggplot2 in R?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to display 0 at Y-axis using ggplot2 in R?
- How to display the curve on the histogram using ggplot2 in R?
- How to set the chart title at the bottom using ggplot2 in R?

Advertisements