How to customize the axis of a Bar Plot in R - GeeksforGeeks

18 Jul, 2021
Barplots in R programming language can be created using the barplot() method. It takes as input a matrix or vector of values. The bar heights are equivalent to the values contained in the vector. 
Syntax: barplot(H, xlab, ylab, main, names.arg, col)
The names.args attribute in the barplot() method can be used to assign names to the x-axis labels. Numeric or character labels can be assigned which are plotted alternatively on the display window. 
Example: Labeling the X-axis of the barplot
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1)
Output
The ylim parameter of the barplot() method can be used to set limits to portray on the display window. It contains a vector containing lower and higher limit. 
Example: Setting the Y-axis limit of the bar plot
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 , ylim= c(0,50) )
Output
The xlim parameter of the barplot() method can be used to set limits to portray on the display window. It contains a vector containing lower and higher limit. 
Example: Setting the X-axis limit
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 , xlim= c(0,50) )
Output
The log parameter can be set to display the axis and its corresponding values on the logarithmic scale. Setting the log value equivalent to character string y displays the modifications on the y axis. 
Example: Plotting logarithmic Y-axis
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 , log = "y" )
Output
The log parameter can be set to display the axis and its corresponding values on the logarithmic scale. Setting the log value equivalent to character string x displays the modifications on the x-axis. 
Example: Plotting logarithmic X-axis
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 , log = "x" )
Output
The names.arg attribute can be renamed to assign a new set of labels to the x-axis arguments.
Example: Renaming the group labels
R
# creating a data framedata_frame <- data.frame(col1 = 1:5,                         col2 = 5:9,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg =c(  "Grp1","Grp2","Grp3","Grp4","Grp5"))
Output
The orientation of the axis labels can be changed using the las attribute. The following specification symbols are used to specify the orientation : 
0: always parallel to the axis
1: always horizontal
2: always perpendicular to the axis
3: always vertical.
Example: Adding label orientation
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 , las=3)
Output
The xlab and ylab attributes contain character strings, which assign the respective names to the axes of the bar plots. 
Example: Adding axis labels
R
# creating a data framedata_frame <- data.frame(col1 = 1:20,                         col2 = 1:20,                         col3 = 1)  # printing the data frameprint ("Original DataFrame")print (data_frame)  # plotting a barplotbarplot(data_frame$col2, names.arg = data_frame$col1 ,        xlab = "Integers", ylab = "Numbers")
Output:
Picked
R-Charts
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement