简体   繁体   English

如何可视化 R 中分类变量的频率

[英]How to Visualize The frequency of a categorical variable in R

I have 2 variables in my dataframe that I am trying to use ggplot to graph.我的 dataframe 中有 2 个变量,我正在尝试使用 ggplot 绘制图形。 On the x-axis I want the date which has a daily frequency.在 x 轴上,我想要具有每日频率的日期。 On the y-axis I want the count of unique names that show up on that given day.在 y 轴上,我想要在给定日期显示的唯一名称的计数。

The variables look something like this in the dataframe. dataframe 中的变量看起来像这样。

     Date           Name

1   2016-03-01      Joe
2   2016-03-01      Joe
3   2016-03-01      Joe 
4   2016-03-01      Mark
5   2016-03-01      Sue
6   2016-03-02      Mark    
7   2016-03-02      Joe
8   2016-03-03      Joe
9   2016-03-03      Joe
10  2016-03-03      Bill

So the frequency on the y-axis on the first day would show 3, 2 on the second, and 2 on the third.因此,第一天 y 轴上的频率将显示 3,第二天显示 2,第三天显示 2。

My question is how do I produce that graph.我的问题是如何生成该图表。

count number of unique Name for each Date and then plot with geom_bar / geom_col .计算每个Date的唯一Name的数量,然后使用geom_bar / geom_col

library(dplyr)
library(ggplot2)
df %>%
  group_by(Date) %>%
  summarise(n = n_distinct(Name)) %>%
  ggplot()  + geom_col(aes(Date, n))
  #ggplot() + geom_bar(aes(Date, n), stat = "identity")

在此处输入图像描述

data数据

df <- structure(list(Date = c("2016-03-01", "2016-03-01", "2016-03-01", 
"2016-03-01", "2016-03-01", "2016-03-02", "2016-03-02", "2016-03-03", 
"2016-03-03", "2016-03-03"), Name = c("Joe", "Joe", "Joe", "Mark", 
"Sue", "Mark", "Joe", "Joe", "Joe", "Bill")), class = "data.frame",
 row.names = c(NA, -10L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 R 中的 ggplot 上 plot 分类可变频率 - How to plot categorical variable frequency on ggplot in R 如何在 R 中可视化多个分类变量与一个连续变量 - How to visualize multiple categorical variables vs one continuous variable in R 在一张图中可视化分类变量与其他变量的频率之间的关系? - visualize relationship between categorical variable and frequency of other variable in one graph? 如何在 R 中优化可视化分类回归结果 - How to Optimal Visualize Categorical Regression Results in R R如何可视化此分类百分比数据? - R How to visualize this categorical percentage data? R多个类别变量的频率表 - R Frequency table of multiple categorical variable 可视化不同样本之间的分类变量的频率? - Visualize frequency of categorical variables between different samples? 基于不同变量值的分类变量的频率表 - R - Frequency table of a categorical variable based on a different variable value- R 如何从 R 中的分类变量更改所有期望值(按频率) - How to change all values expect for the top values (by frequency) from a categorical variable in R R &amp; ggplot2 - 如何通过二进制变量 plot 的相对频率 - R & ggplot2 - how to plot relative frequency of a categorical split by a binary variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM