Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

r - How to force specific order of the variables on the X axis?

My question has to do with order of plot bars 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 heights are in 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)

Bar heights are not in order seen in Half_Effect. How to force ordering of EffectNames to match descending order of 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Elaborating eipi10's comment, the reordering of levels can by accomplished conveniently using Hadley's forcats package. In addition, the reordering can be done within the call to aes() instead of manipulating the underlying data. This offers additional flexibilty in finding a suitable graphical display.

Initial plot

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, the order is alphabetical by default.

Reorder the levels in order of first appearance

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

enter image description here

  • The factors are ordered by their first appearance in the vector (which probably was choosen intentionally by the OP with respect to the value of Half_Effect, so no real surprise here.)
  • In this example, fct_inorder() saves us from typing the same stuff twice which would be required when explicitely specifying the levels in a call to factor.
  • The plot p was modified by only changing the x aesthetics. The underlying data needn't to be touched.

Reorder the levels according to another variable

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

enter image description here

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

To show the levels in decreasing order as requested by the OP we can do

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

enter image description here

Note that reorder() has no explicit parameter to reverse the order so we would need to modify the controlling variable reorder(EffectNames, -Half_Effect).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...