英文:
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转义字符(如 <
和 "
)已被保留,但在实际使用时通常应该将它们还原为原始字符。
英文:
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 <- rep(LETTERS[1:5], each = 2)
airline <- rep(c("Alaska", "AM West"), times = 5)
percent_ontime <- ceiling(runif(10, 70, 99))
df <- data.frame(destination, airline, percent_ontime)
df
#> destination airline percent_ontime
#> 1 A Alaska 79
#> 2 A AM West 93
#> 3 B Alaska 82
#> 4 B AM West 96
#> 5 C Alaska 98
#> 6 C AM West 72
#> 7 D Alaska 86
#> 8 D AM West 96
#> 9 E Alaska 86
#> 10 E AM West 84
# make airline a factor and define order of levels
# df$airline <- factor(df$airline, levels = c("AM West", "Alaska"))
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)
)
) +
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", breaks = rev) +
theme_minimal()
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论