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

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

问题描述

我的问题与使用 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 = "身份") +geom_text(aes(label = Half_Effect), vjust = 1.5, color = "white")

结果:条形高度按以下顺序

1.6 0.65 0.77 0.98 6.5 5.6 10.4AB ABC AC BC 挂钩(B) 挂钩(C) 回拉(A)

条形高度在 Half_Effect 中没有按顺序显示.如何强制 EffectNames 的排序与 Half_Effect 的降序匹配?这可以在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$EffectNames = factor(paretoData$EffectNames,水平= c(拉回(A)",挂钩(B)",挂钩(C)",AB",BC",AC",ABC"))p=ggplot(paretoData, aes(x=EffectNames, y=Half_Effect)) +geom_bar(stat="身份") +geom_text(aes(label=Half_Effect), vjust=1.5, colour="white")p

解决方案

详述

这里的顺序默认是按字母顺序.

按照首次出现的顺序重新排列关卡

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

  • 这些因子按它们在向量中的首次出现排序(这可能是 OP 根据 Half_Effect 的值有意选择的,所以这里并不奇怪.)
  • 在此示例中,fct_inorder() 使我们免于键入两次相同的内容,而这在调用 factor 时需要明确指定级别.
  • 情节 p 仅通过更改 x 美学进行了修改.无需触及底层数据.

根据另一个变量重新排列级别

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

此处,级别按 Half_Effect 的递增值排序.我们可以通过使用来自基础 R 的 reorder() 而不是 fct_reorder() 来实现相同的效果.

为了按照OP要求的降序显示级别,我们可以这样做

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

请注意,reorder() 没有 显式参数来反转顺序,因此我们需要修改控制变量 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天全站免登陆