r - 根据ggplot2中最后一个分面网格的递减值对条形图的Y轴进行排序
问题描述
问题:
我正在尝试根据具有公共 Y 轴标签的最后一个方面组“Step4”的递减值对条形图的 Y 轴进行排序。有一些建议可以对它们内部的所有构面组进行排序,但是如何处理一个构面组的公共 y 轴标签和值。我附上了初始图的示例数据和代码以理解问题。提前致谢。
数据:
在此处下载示例数据
代码:
library(ggplot2)
library(reshape2)
#reading data
data <- read.csv(file = "./sample_data.csv", stringsAsFactors = TRUE)
#reshaping data in longer format using reshape::melt
data.melt <- melt(data)
#plotting the data in multi-panel barplot
ggplot(data.melt, aes(x= value, y=reorder(variable, value))) +
geom_col(aes(fill = Days), width = 0.7) +
facet_grid(.~step, scales = "free")+
theme_pubr() +
labs(x = "Number of Days", y = "X")
图表:样本数据的条形图
解决方案
汇总 last 的值'step'
并从数据中提取级别。
library(dplyr)
library(ggplot2)
lvls <- data.melt %>%
arrange(step) %>%
filter(step == last(step)) %>%
#Or
#filter(step == 'Step4') %>%
group_by(variable) %>%
summarise(sum = sum(value)) %>%
arrange(sum) %>%
pull(variable)
data.melt$variable <- factor(data.melt$variable, lvls)
ggplot(data.melt, aes(x= value, y= variable)) +
geom_col(aes(fill = days), width = 0.7) +
facet_grid(.~step, scales = "free")+
theme_pubr() +
labs(x = "Number of Days", y = "X")
推荐阅读
- java - 如何使用camera2修改帧率?
- asp.net-mvc - 如何将带有语句体的 lambda 表达式转换为表达式树
- toggle - PrimeNG 多选选择和取消选择值
- ibm-mq - 确定访问我们本地队列的远程 MQ 队列管理器
- macos - 未找到 Visual Studio 2017 导入的项目。还尝试在后备搜索路径中找到“Xamarin.Android.CSharp.targets”
- python - 用于将配置单元表加载到 Apache solr 的 Shell 脚本失败
- python - GEOPANDAS .sjoin 'index_left' 和 'index_right' 不能是正在连接的框架中的名称
- python - 如何在不使用熊猫的情况下更改列的位置
- ios - 今天的 iOS 扩展程序是否与主机应用程序一起自动安装在用户的设备上?
- json - JSON DATA格式角度6的正则表达式模式