- 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
Create ggplot2 graph with reversed Y-axis and X-axis on top in R.
To create ggplot2 graph with reversed Y-axis and X-axis on top, we can use scale_y_reverse and scale_x_continuous function of ggplot2 package.
For Example, if we have a data frame called df that contains two columns say X and Y and we want to create the scatterplot between X and Y with reversed Y-axis and X-axis on top then we can use the below command −
ggplot(df,aes(X,Y))+geom_point()+scale_y_reverse()+scale_x_continuous(position="top")
Example
Following snippet creates a sample data frame −
x<-rnorm(20) y<-rnorm(20) df<-data.frame(x,y) df
The following dataframe is created
x y 1 0.59312447 0.4993337 2 0.08696615 -0.1242276 3 1.11337560 -0.5706901 4 0.01421750 0.3663344 5 -0.84509560 0.4541131 6 -0.65373363 0.9613303 7 -0.35945721 -0.1857298 8 0.59160684 0.3575833 9 -1.26637220 -1.1135318 10 0.38272955 0.8173167 11 0.06691834 0.3234260 12 0.16622601 1.2409558 13 0.46761776 -0.9447498 14 1.58349520 -0.4018503 15 0.58928505 -2.6192820 16 0.36153663 2.1236007 17 0.53978229 -0.5937517 18 -1.03483870 1.0795689 19 -1.17331809 -1.0425776 20 -1.12299141 -0.7738698
To load ggplot2 package and create scatterplot between x and y on the above created data frame, add the following code to the above snippet −
x<-rnorm(20) y<-rnorm(20) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
If you execute all the above given snippets as a single program, it generates the following Output −
To create scatterplot between x and y with reversed Y-axis and X-axis on top on the above created data frame, add the following code to the above snippet −
x<-rnorm(20) y<-rnorm(20) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+scale_y_reverse()+scale_x_continuous(position="top")
Output
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- How to represent all values of X-axis or Y-axis on the graph in R using ggplot2 package?
- Create stacked bar chart with percentages on Y-axis using ggplot2 in R.
- How to create a plot with reversed Y-axis labels in base R?
- How to create cumulative sum chart with count on Y-axis in R using ggplot2?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- How to plot values with log scales on x and y axis or on a single axis in R?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to increase the length of Y-axis values for ggplot2 graph in R?
- How to create different Y-axis for group levels using ggplot2 in R?
- How to display Y-axis with Euro sign using ggplot2 in R?
- How to create a barplot with gaps on Y-axis scale in R?
- How to X-axis labels to the top of the plot using ggplot2 in R?
- Transform the element along with x-axis and y-axis with CSS
- How to display count on Y-axis for line chart using ggplot2 in R?
- Transform the element by using x-axis, y-axis, and z-axis with CSS3
