需要重新排列成对的条形图。

huangapple go评论59阅读模式
英文:

Need to reorder paired bar chart

问题

如何更改成对条形的顺序?

我想要让阿拉斯加(浅蓝色)始终在AM West(深蓝色)的上方。

ggplot(data=Overview_All_flights_by_Airline_and_Destination1, 
       aes(x= Percent_Ontime, y=reorder(Destination, +Percent_Ontime), 
            fill=Airline)) +
  geom_bar(stat="identity", color="black", position=position_dodge()) +
  geom_text(aes(label=Percent_Ontime), vjust=-0.3, 
                position = position_dodge(0.9), size=3.5) +
  scale_fill_brewer(palette="Paired") +
  theme_minimal()  

需要重新排列成对的条形图。

英文:

How do i change the order of the paired bars?

I would like Alaska (light blue) to be above the AM West (dark blue) for every pair.

ggplot(data=Overview_All_flights_by_Airline_and_Destination1, 
       aes(x= Percent_Ontime, y=reorder(Destination, +Percent_Ontime), 
            fill=Airline)) +
  geom_bar(stat="identity", color="black",position=position_dodge()) +
  geom_text(aes(label=Percent_Ontime), vjust=-0.3, 
                position = position_dodge(0.9), size=3.5) +
  scale_fill_brewer(palette="Paired") +
  theme_minimal()  

需要重新排列成对的条形图。

答案1

得分: 1

以下是翻译好的内容:

"What makes things difficult is that y- and x-axis are flipped."
"问题在于y轴和x轴被颠倒了。"

"One option is to define airline as a factor and setting the order of levels."
"一种选择是将 airline 定义为一个因子,并设置 levels 的顺序。"

"Using forcats library allows, you can reverse things back from what was changed by flipping axis. With forcats::fct_rev y- axis and fill needs to be reversed."
"使用 forcats 库,您可以将通过颠倒轴进行的更改恢复。使用 forcats::fct_rev 来颠倒y轴和填充。"

"The order of the legend items can also be adopted with rev."
"图例项的顺序也可以使用 rev 来采用。"

"I have made a dummy DF, which comes close to your own DF"
"我创建了一个与您的数据框接近的虚拟数据框。"

"Make artificial DF"
"创建人工数据框"

"set.seed(123)"
"设置随机数生成的种子为123"

"df <- data.frame(destination, airline, percent_ontime)"
"df <- data.frame(destination, airline, percent_ontime)"

"make airline a factor and define order of levels"
"将航空公司转化为因子,并定义级别的顺序"

"df$airline <- factor(df$airline, levels = c("Alaska", "AM West"))"
"df$airline <- factor(df$airline, levels = c("Alaska", "AM West"))"

"ggplot(data = df, aes(x = percent_ontime, y = fct_rev(destination), fill = fct_rev(airline))) +"
"ggplot(data = df, aes(x = percent_ontime, y = fct_rev(destination), fill = fct_rev(airline))) +"

"geom_bar(stat = "identity", color = "black", position = position_dodge()) +"
"geom_bar(stat = "identity", color = "black", position = position_dodge()) +"

"geom_text(aes(label = percent_ontime), vjust = -0.3, position = position_dodge(0.9), size = 3.5) +"
"geom_text(aes(label = percent_ontime), vjust = -0.3, position = position_dodge(0.9), size = 3.5) +"

"scale_fill_brewer(palette = "Paired", breaks = rev) +"
"scale_fill_brewer(palette = "Paired", breaks = rev) +"

"theme_minimal()"
"theme_minimal()"

请注意,上述代码中的HTML转义字符(如 &lt;&quot;)已被保留,但在实际使用时通常应该将它们还原为原始字符。

英文:

What makes things difficult is that y- and x-axis are flipped.
One option is to define airline as a factor and setting the order of levels.
Using forcats library allows, you can reverse things back from what was changed by flipping axis. With forcats::fct_rev y- axis and fill needs to be reversed.
The order of the legend items can also be adopted with rev.

I have made a dummy DF, which comes close to your own DF

library(ggplot2)
library(forcats)
set.seed(123)
# Make artificial DF
destination &lt;- rep(LETTERS[1:5], each = 2)
airline &lt;- rep(c(&quot;Alaska&quot;, &quot;AM West&quot;), times = 5)
percent_ontime &lt;- ceiling(runif(10, 70, 99))

df &lt;- data.frame(destination, airline, percent_ontime)
df
#&gt;    destination airline percent_ontime
#&gt; 1            A  Alaska             79
#&gt; 2            A AM West             93
#&gt; 3            B  Alaska             82
#&gt; 4            B AM West             96
#&gt; 5            C  Alaska             98
#&gt; 6            C AM West             72
#&gt; 7            D  Alaska             86
#&gt; 8            D AM West             96
#&gt; 9            E  Alaska             86
#&gt; 10           E AM West             84
# make airline a factor and define order of levels
# df$airline &lt;- factor(df$airline, levels = c(&quot;AM West&quot;, &quot;Alaska&quot;))
df$airline &lt;- factor(df$airline, levels = c(&quot;Alaska&quot;, &quot;AM West&quot;))

ggplot(
  data = df,
  aes(
    x = percent_ontime,
    y = fct_rev(destination),
    fill = fct_rev(airline)
  )
) +
  geom_bar(stat = &quot;identity&quot;, color = &quot;black&quot;, position = position_dodge()) +
  geom_text(aes(label = percent_ontime),
    vjust = -0.3,
    position = position_dodge(0.9), size = 3.5
  ) +
  scale_fill_brewer(palette = &quot;Paired&quot;, breaks = rev) +
  theme_minimal()

需要重新排列成对的条形图。<!-- -->

huangapple
  • 本文由 发表于 2023年2月26日 23:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定