banner



How To Organize Bar Chart In Descending Order

In this article, we will discuss how to change the order of bars in bar chart in R programming language. We can change the social club of bars by using two plots ggplot and barplot.

Method 1: Ggplot re-ordering

Firstly create a sample dataset and plot the graph-Manual. At present permit'due south reorder them accordingly.

Dataset in Employ:

Employee Bacon Details

Reordering in ggplot is done using theme() function. Within this, we apply axis.text.x with the appropriate value to re-gild accordingly. By default, geom_bar uses stat="bin". This makes the summit of each bar equal to the number of cases in each group,  If yous want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic.

  • geom_bar() : Use for the Bar charts

Syntax:

geom_bar( mapping = Nix, data = Zero, stat = "count", color='blue'; fill='yellow' width = NULL, na.rm = Imitation, orientation = NA, show.fable = NA,…,)

  • Theme: Themes can exist used to give plots a consequent customized look.

Syntax:

theme( line, rect, text, title, attribute.ratio, axis.text.x, axis.text.x.top, axis.text.x.lesser,)

Parameter:

  • line  : all line elements (element_line())
  • rect  :all rectangular elements (element_rect())
  • text  : all text elements (element_text())
  • title  :all title elements: plot, axes, legends (element_text(); inherits from text)
  • aspect.ratio  :attribute ratio of the panel.
  • axis.text.ten : labels forth with axes (element_text()). Specify all centrality labels (axis.text), labels

Manually Social club

To reorder bars manually, you have to pass stat="identity" in the geom_bar() function.

Example:

R

library (ggplot2)

gfg.information <- data.frame (

GFG_Id = c (one:7),

GFG_Name = c ( "Damon" , "Joe" , "Jen" , "Ryan" , "Bonnie" , "Stefan" , "William" ),

GFG_Sal = c (6200,5152,6110,7290,8485,7654,2341))

print (gfg.data)

ten <- ggplot (gfg.data, aes (x = GFG_Name, y = GFG_Sal))

10 <- x + geom_bar (stat= "identity" , color= 'lightgreen' ,fill up= 'lightgreen' )

x <- x + theme (axis.text.x= element_text (angle=45, hjust=0.9))

ten

Output:

Re-ordering the in ascending and descending guild

The reorder part is used here to change the order of the graphs.

Syntax :

ggplot(dataframe name, aes(x=reorder(column1,±column2),y=column2)

Here if you lot want ascending order and so you lot'll use '+' plus sign, if you want in descending order and so you should employ '-' minus sign.

Note: Column2 must be the column with numeric data.

Instance: Let u.s.a. starting time show the same bar plot in ascending gild.

R

library (ggplot2)

gfg.data <- information.frame (

GFG_Id = c (1:vii),

GFG_Name = c ( "Damon" , "Joe" , "Jen" , "Ryan" , "Bonnie" , "Stefan" , "William" ),

GFG_Sal = c (6200,5152,6110,7290,8485,7654,2341))

print (gfg.information)

x <- ggplot (gfg.data, aes (x = reorder (GFG_Name, +GFG_Sal), y = GFG_Sal))

x <- x + geom_bar (stat= "identity" , color= 'cerise' ,fill= 'red' )

x <- x + theme (axis.text.ten= element_text (angle=45, hjust=0.9))

10

Output:

Example: Now allow'due south look at descending ordered plot

R

library (ggplot2)

gfg.data <- information.frame (

GFG_Id = c (1:7),

GFG_Name = c ( "Damon" , "Joe" , "Jen" , "Ryan" , "Bonnie" , "Stefan" , "William" ),

GFG_Sal = c (6200,5152,6110,7290,8485,7654,2341))

print (gfg.data)

ten <- ggplot (gfg.data, aes (x = reorder (GFG_Name, -GFG_Sal), y = GFG_Sal))

ten <- x + geom_bar (stat= "identity" , color= 'violet' ,fill= 'violet' )

x <- x + theme (axis.text.x= element_text (angle=45, hjust=0.nine))

x

Output:

Method 2: Using Barplot re-ordering

Firstly create a sample dataset and plot the graph. Now allow us look at the reordering of the bars.

Dataframe in apply:

In R, barplot() function

Syntax:

barplot(height, name.args = NULL, col = NULL, main = Nix)

Parameter:

  • height: You can specify either a Vector or a Matrix of values.
  • proper name.args :a Vector of names you want to plot beneath each bar or group of bars in an R bar chart.

Manually Ordering

For transmission ordering pass the order to the x-centrality while creating the dataframe.

Example:

R

GFG_ID   <- c (1:7)

GFG_Sal  <- c (6200,5152,6110,7290,8485,7654,2341)

GFG_Name <- c ( "Dia" , "Joe" , "Rex" , "Ryan" , "Bex" , "Stef" , "Max" )

X <- data.frame (GFG_ID,GFG_Name,GFG_Sal)

X

png (file = "barchart_months_salary.png" )

barplot (GFG_Sal,names.arg=GFG_Name,xlab= "Employee Name" ,

ylab= "Salary Range" ,col= "black" ,

main= "Bacon chart" ,border= "Red" )

dev.off ()

Output

Re-ordering the graph in ascending and descending order

The function used here is the order (order returns a permutation that rearranges its first statement into ascending or descending order)

Syntax –

order(…, na.last = True, decreasing = FALSE)

Parameter:

na.last: for decision-making the treatment of NAs

Allow united states of america commencement look at the ascending ordered plot.

Instance:

R

GFG_ID  <- ( LETTERS [1:7])

GFG_Sal  <- c (6200,5152,6110,7290,8485,7654,2341)

data <- data.frame (GFG_Sal, GFG_ID)

barplot (information[ order (information[,1],decreasing= FALSE ),

][,1],names.arg=data[ order (data[,1],decreasing= FALSE ),]

[,2],col= "bluish" ,xlab= "Employee Proper name Initial Letter" ,

ylab= "Salary Range" ,main= "Salary chart" ,border= "blackness" )

Output

Now let united states look at the descending ordered plot.

Example:

R

GFG_ID  <- ( Messages [1:seven])

GFG_Sal  <- c (6200,5152,6110,7290,8485,7654,2341)

data <- information.frame (GFG_Sal, GFG_ID)

barplot (data[ gild (information[,ane],decreasing= TRUE ),

][,1],names.arg=data[ gild (data[,ane],decreasing= Truthful ),

][,ii],col= "blueish" ,xlab= "Employee Proper noun Initial Letter" ,

ylab= "Salary Range" ,main= "Salary chart" ,border= "blackness" )

Output


Source: https://www.geeksforgeeks.org/how-to-change-the-order-of-bars-in-bar-chart-in-r/

0 Response to "How To Organize Bar Chart In Descending Order"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel