如何在 R 中使用 ggplot2 反转条形图的条形方向?


条形图的条形通常是从下到上垂直排列的,但我们也可以反转它们。虽然这不是通常的做法,但如果我们想这样做,我们是可以做到的。为此,我们将不得不反转 Y 轴上的值,结果条形将被反转。这可以通过使用 scale_y_continuous 来实现。

示例

考虑以下数据框 -

Salary_Group <-c("A","B","C","D","E")
Attendance <-c(78,82,90,95,85)
df <-data.frame(Salary_Group,Attendance)
df

输出

Salary_Group Attendance
1 A 78
2 B 82
3 C 90
4 D 95
5 E 85

创建简单的条形图 -

示例

library(ggplot2)
ggplot(df,aes(Salary_Group,Attendance))+geom_bar(stat="identity")

输出

反转图形的条形 -

示例

ggplot(df,aes(Salary_Group,Attendance))+geom_bar(stat="identity")+scale_y_continuous(trans="reverse")

输出

更新于: 2020年8月24日

965 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.