如何强制变量在X轴上的特定顺序? [英] How to force specific order of the variables on the X axis?

查看:146
本文介绍了如何强制变量在X轴上的特定顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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,color =white)

结果:
Bar高度按以下顺序排列:

<$ p $ (C)PullBack(A)
1.6 0.65 0.77 0.98 6.5 5.6 10.4
AB ABC AC BC Hook $ p>

在Half_Effect中看不到高度。如何强制按照Half_Effect的降序排列EffectNames?这可以在ggplot2中完成吗?是的,它可以!请参阅下面的解决方案。

  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,color =white)
p


解决方案

详细描述



这里,订单默认是按字母顺序排列的。



按照第一次出现的顺序重新排序



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




  • 这些因素按照它们在向量中的首次出现排序(这可能是OP针对的值故意选择的) )

  • 在这个例子中, fct_inorder()把我们从在调用 factor 时明确指定级别时,输入相同的东西两次。

  • 通过只改变 x 美学来修改c> p 。基本数据不需要触及。



根据另一个变量重新排序



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



请注意 reorder() no 显式参数来颠倒顺序,所以我们需要修改控制变量 reorder(EffectNames,-Half_Effect)


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

解决方案

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

Here, the order is alphabetical by default.

Reorder the levels in order of first appearance

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

  • 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))

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))

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

这篇关于如何强制变量在X轴上的特定顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆