r - 我如何使用数据框中的值在 ggplot 2 中绘制图形并将它们转换为四舍五入的百分比标签
问题描述
我有这些价值观
PCT_Asian | PCT_Black | PCT_Hispanic | PCT_White | PCT_Other
.26554 .25454 .145454 .22454 .23123
我想看到一个条形图,其中每个 bin 下的 x 轴标签
PCT_Asian to Asian
PCT_Black to Black
Pct_White to White and so on.....
还将值转换为适当的百分比标签
27%、25%、15%、22% 和 23%
解决方案
你可以试试
library(tidyverse)
a <- str_split("PCT_Asian | PCT_Black | PCT_Hispanic | PCT_White | PCT_Other", "[|]", simplify = T) %>% str_trim(.)
b <- str_split(".26554 .25454 .145454 .22454 .23123", " ", simplify = T) %>% as.numeric(.) %>% na.omit(.)
tibble(a, b) %>%
mutate(x = str_remove(a, "PCT_")) %>%
ggplot(aes(x, b)) +
geom_col() +
scale_y_continuous(labels = scales::percent)
推荐阅读
- r - 加载多个 excel 文件(具有不同的选项卡)并在文件和选项卡名称之后命名对象
- scala - Scala嵌套对象具有同一类的不同模板
- wso2 - 无法在没有日志分析的情况下启动 WSO2 Enterprise Integrator 6.4.0
- javascript - 为什么我的 claculator javascript 代码无法在 Safari 浏览器上运行?
- ios - Firebase 远程配置 setDefaults:不存储 NSDictionary
- excel - Office 脚本错误 - 属性不可用
- c - 如何分配足够的内存来在 C 中存储整个文本文件?
- r - 使用 rNOMADS 访问历史天气模型数据
- rpgle - 在子过程中嵌入 sql 语句时遇到编译时错误
- azure - Azure EventHub 事件格式