- 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 X-axis labels to the top of the plot using ggplot2 in R?
Usually, a plot created in R or any of the statistical analysis software have X-axis labels on the bottom side but we might be interested in showing them at the top of the plot. It can be done for any type of two-dimensional plot whether it is a scatterplot, bar plot, etc. This is possible by using scale_x_continuous function of ggplot2 package in R.
Example
set.seed(123) x<-runif(10,2,3) y<-rpois(10,2) df<-data.frame(x,y) df
Output
x y 1 2.287578 5 2 2.788305 2 3 2.408977 3 4 2.883017 2 5 2.940467 0 6 2.045556 4 7 2.528105 1 8 2.892419 0 9 2.551435 1 10 2.456615 5 library(ggplot2)
Example
Creating a simple scatterplot −
ggplot(df,aes(x,y))+geom_point()
Output
Creating a scatterplot with X-axis labels on the top of the plot −
Example
ggplot(df,aes(x,y))+geom_point()+scale_x_continuous(position="top")
Output
- Related Articles
- How to increase the X-axis labels font size 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 display positive sign for X-axis labels in R using ggplot2?
- How to change the orientation and font size of x-axis labels using ggplot2 in R?
- How to change ordinal X-axis label to text labels using ggplot2 in R?
- How to plot the X-axis labels on the upper-side of the plot in R?
- How to display X-axis labels inside the plot in base R?
- How to change the adjustment of the plot title using ggplot2 to align it above the Y-axis labels in R?
- How to extract axes labels for the plot drawn using ggplot2 in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to change the automatic sorting of X-axis of a bar plot using ggplot2 in R?
- How to convert the X-axis label in a bar plot to italic using ggplot2 in R?
- How to display axes ticks and labels inside the plot using ggplot2 in R?
- How to display X-axis labels with dash in base R plot?

Advertisements