r - 使用 ggplot 和 r 显示分类变量的分布
解决方案
使用泰坦尼克号数据集,可以使用
library(tidyverse)
data("Titanic")
Titanic %>%
as.data.frame() %>% # transform from a table to dataframe
gather(variable, value, -Freq) %>% # change to long format
group_by(variable, value) %>%
summarise(Freq = sum(Freq)) %>% # get the freq for each level of each variable
ggplot(aes(variable, Freq, fill = value)) +
geom_col(position = position_stack()) +
geom_text(aes(label = paste0(value, " (", Freq, ")")), vjust = 1,
position = position_stack()) +
theme(legend.position = "none")
推荐阅读
- javascript - 构建一个 javascript 库 / 未捕获的 SyntaxError: import not found: elementToSVG
- javascript - 升级到 Webpack 5 后如何阻止 webpack 创建 dist/main.js?
- python - Putting list of value into a dataframe as a row
- ruby-on-rails - Hotwire turbolinks 和事件监听器的问题
- r - 在反应式上下文中捕获函数的标准输出和返回值
- plugins - 替换 jenkins 中的“静态分析收集器插件”和“静态分析实用程序”
- angular - 关闭弹出窗口后如何刷新数据表
- java - Hibernate - JPA 提取查询需要很长时间才能完成
- scala - 线程“main”中的异常 java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)[Ljava/lang/Object;
- android - app如何获取导航栏的模式