How to force a special order of variables on the x axis?

My question is about the order of the graphs when using ggplot.

EffectNames = c("Pull Back(A)","Hook(B)","Peg(C)","AB","BC","AC","ABC") Half_Effect = c(10.4, 6.5, 5.6, 1.6, 0.98, .77, .65) paretoData = cbind(EffectNames, Half_Effect) paretoData = as.data.frame(paretoData) ggplot(paretoData, aes(x = EffectNames, y = Half_Effect)) + geom_bar(stat = "identity") + geom_text(aes(label = Half_Effect), vjust = 1.5, colour = "white") 

Result: Bar height in the following order

 1.6 0.65 0.77 0.98 6.5 5.6 10.4 AB ABC AC BC Hook(B) Peg(C) PullBack(A) 

Half_Effect does not display bar heights. How do I force EffectNames to descend in Half_Effect? Can this be done in ggplot2? Yes it can! See Solution below.

 EffectNames=c( "Pull Back(A)","Hook(B)", "Peg(C)","AB", "BC", "AC", "ABC") Half_Effect=c( 10.4,6.5,5.6,1.6,0.98,.77,.65 ) paretoData=data.frame(EffectNames, Half_Effect) paretoData paretoData$EffectNames = factor(paretoData$EffectNames, levels=c("Pull Back(A)","Hook(B)", "Peg(C)","AB", "BC", "AC", "ABC")) p=ggplot(paretoData, aes(x=EffectNames, y=Half_Effect)) + geom_bar(stat="identity") + geom_text(aes(label=Half_Effect), vjust=1.5, colour="white") p 
+5
source share
1 answer

When developing the eipi10 comment , level reordering can be done using the Hadley forcats . In addition, reordering can be performed by calling aes() instead of manipulating the underlying data. This provides additional flexibility in finding the right graphic display.

Initial schedule

 paretoData <- data.frame( EffectNames = c("Pull Back(A)", "Hook(B)", "Peg(C)", "AB", "BC", "AC", "ABC"), Half_Effect = c(10.4, 6.5, 5.6, 1.6, 0.98, .77, .65)) library(ggplot2) p <- ggplot(paretoData, aes(x = EffectNames, y = Half_Effect)) + geom_bar(stat = "identity") + geom_text(aes(label = Half_Effect), vjust = 1.5, colour = "white") p 

enter image description here

Here is the default alphabetical order.

Reorder in order of first appearance

 library(forcats) p + aes(x = fct_inorder(EffectNames)) 

enter image description here

  • Factors are ordered by their first appearance in the vector (which was probably intentionally chosen by the OP relative to the Half_Effect value, so it’s not surprising here.)
  • In this example, fct_inorder() saves us from entering the same material twice, which would be required if we explicitly specified the levels when calling factor .
  • The plot of p was changed by changing the aesthetics of x . No need to touch the baseline data.

Reorder levels by another variable

 p + aes(x = fct_reorder(EffectNames, Half_Effect)) 

enter image description here

Here the levels are ordered, increasing the value of Half_Effect . We could achieve the same effect using reorder() from the R base instead of fct_reorder() .

To show the levels in descending order at the request of OP , we can do

 p + aes(x = fct_reorder(EffectNames, Half_Effect, .desc = TRUE)) 

enter image description here

Note that reorder() does not have an explicit parameter to reorder, so we will need to change the reorder(EffectNames, -Half_Effect) control variable reorder(EffectNames, -Half_Effect) .

+11
source

Source: https://habr.com/ru/post/1258652/


All Articles