[英]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.