How to use ylim()/xlim() function In R Language

ylim()/xlim() function: Convenience function to set the limits of the y axis/x-axis.

Syntax:

ylim(...)
xlim(...)

Parameters:

  • …: if numeric, will create a continuous scale, if factor or character, will create a discrete scale.

Example:

In this, example, we will be using xlim() function to get the zoom into view of the ggplot2 bar plot of the given data without removing any initial data in the R programming language. 

R




# load the packages
library("ggplot2")
  
# create the dataframe with letters and numbers
gfg < -data.frame(
    x=c('A', 'B', 'C', 'D', 'E', 'F'),
    y=c(4, 6, 2, 9, 7, 3))
  
# plot the given data
# with A,B and C as titles to the bars
plot < -ggplot(gfg, aes(x, y, fill=x)) +
geom_bar(stat="identity")
plot+xlim('A', 'B', 'C')


Output:

Zoom into ggplot2 Plot without Removing Data in R

In this article, we will discuss how to zoom into the ggplot2 plot without removing data using ggplot2 package in the R programming language. The approaches to the zoom into ggplot2 plot without removing data are as follows:

  • Zoom in to ggplot2 plot without Removing data using ylim()/xlim() function
  • Zoom in to ggplot2 plot without Removing data using coord_cartesian() function

Similar Reads

Method 1: Using ylim()/xlim() function

ylim()/xlim() function: Convenience function to set the limits of the y axis/x-axis....

Method 2: Using coord_cartesian() function

...

Contact Us