【问题标题】:displaying distribution of categorical variable using ggplot and r使用 ggplot 和 r 显示分类变量的分布
【发布时间】:2018-11-20 14:43:32
【问题描述】:

就通用泰坦尼克数据集简化我的问题:

如何获得数据集中所有属性的以下图

如果可能,我还想获取每个类别的计数或百分比。

提前感谢您的帮助。

问候,特鲁提

【问题讨论】:

  • 一旦你把你的数据转换成正确的格式,你应该可以使用like this的东西了。

标签: r ggplot2 bar-chart


【解决方案1】:

使用泰坦尼克号数据集,这可以使用

来完成
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")

【讨论】:

  • 这有帮助。谢谢你:)
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多