r - 重新排序单个翻转堆叠条
问题描述
我有以下数据集
example <- structure(list(selection_type = c("P", "G"), count = c(44L,
102L), var = c("ST", "ST")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
我制作了以下情节:
ggplot(example) +
geom_bar( aes(x = var, y = count, fill = selection_type), stat = "identity") +
coord_flip()
问题:我需要它倒过来。所以“P”在堆叠条的“右侧”。
我努力了:
- 在数据框上排列然后绘图,它不起作用
x = reorder(var,count)
而且x = reorder(var,-count)
也不起作用
解决方案
您可以使用 forcats 包中的 fct_rev
library(tidyverse)
example <- structure(list(selection_type = c("P", "G"), count = c(44L,
102L), var = c("ST", "ST")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(example) +
geom_bar( aes(x = var, y = count, fill = fct_rev(selection_type)), stat = "identity") +
coord_flip()
推荐阅读
- rpm - 创建提供用户可编辑配置文件的 RPM
- c - 使用 cmake 在 clion 中包含 GLIB 时未定义的引用
- spring-cloud-dataflow - Scrape 的 Prometheus Deployer 属性
- java - 如何在 textInputEditText 中添加图像(X)并清除片段中的字段?
- java - java.net.http.HttpClient 需要关闭吗?
- docker - 在 kubernetes 中的部署显示在使用为训练图像创建的 docker 图像进行对象检测时回退重新启动失败的容器
- python - 使用 python 脚本缩放 .stl 文件
- node.js - JOI.validate() 不允许“dataValues”
- c# - 更改本机“选择图标”对话框窗口的语言?
- javascript - 需要帮助制作小费计算器