r - ggplot中分组条形图的格式
问题描述
我目前坚持格式化分组条形图。
我有一个数据框,我想将其可视化:
iteration position value
1 1 eEP_SRO 20346
2 1 eEP_drift 22410
3 1 eEP_hole 29626
4 2 eEP_SRO 35884
5 2 eEP_drift 39424
6 2 eEP_hole 51491
7 3 eEP_SRO 51516
8 3 eEP_drift 55523
9 3 eEP_hole 74403
位置应显示为颜色,值应以条形高度表示。
我的代码是:
fig <- ggplot(df_eEP_Location_plot, aes(fill=position, y=value, x=iteration, order=position)) +
geom_bar(stat="identity")
这给了我这个结果:
我想有一个正确的 y 轴标签,并且还想将我的条从最大到最小排序(忽略迭代次数)。我怎样才能做到这一点?
非常感谢您的帮助!
解决方案
我建议使用fct_reorder
from the forcats
package 在绘制之前沿指定值重新排序迭代ggplot
。使用您提供的示例数据查看以下内容:
library(ggplot2)
library(forcats)
iteration <- factor(c(1,1,1,2,2,2,3,3,3))
position <- factor(rep(c("eEP_SRO","eEP_drift","eEP_hole")))
value <- c(20346,22410,29626,35884,39424,51491,51516,55523,74403)
df_eEP_Location_plot <- data.frame(iteration, position, value)
df_eEP_Location_plot$iteration <- fct_reorder(df_eEP_Location_plot$iteration,
-df_eEP_Location_plot$value)
fig <- ggplot(df_eEP_Location_plot, aes(y=value, x=iteration, fill=position)) +
geom_bar(stat="identity")
fig
推荐阅读
- javascript - 如何等待回调完成从 ES6 代理返回
- javascript - 如何使用Vue路由器更改轮播幻灯片?
- sql - 在 Oracle DB Developer VM 中启动时 SQL Developer 崩溃
- c# - 未找到 C# .NET UserNotificationListener 元素
- time - Raspberry Pi Pico - 纳秒计时器
- gluon - Gluon Native DRM javaFX 错误,无法启动基于 DRM 的 JavaFX Native 应用程序
- python - 基于列值 0 或 1 的行的平均值
- .net - 如何在 .NET Core 5 上将 example.com 重定向到 example.com/espanol
- python - 将单个单词附加到一个集合中,以便我得到唯一的字母
- java - 配对(连接)到新设备或配对在应用程序中不起作用