[英]stat_count for month-year combination
I want to plot the number of occurrences of a certain date in a data frame.我想要 plot 数据框中某个日期的出现次数。 I write the following:
我写了以下内容:
dates <- c("2018-01-03", "2018-01-17", "2018-02-03", "2018-02-03", "2018-02-16",
"2018-02-26", "2018-03-03")
data <- as.data.frame(dates)
ggplot(data) +
stat_count(aes(x = (dates)))
Basically the code plots the number of occurences of each day-month-year combination in a date.基本上,代码绘制了日期中每个日-月-年组合的出现次数。 Now, I want to create a plot that counts the number of occurences of each month-year combination in a date.
现在,我想创建一个 plot 来计算日期中每个月-年组合的出现次数。 Basically, considering the picture, bars that have the same month-year should be one above the other.
基本上,考虑到图片,具有相同月份-年份的条柱应该一个在另一个之上。 Does anyone have any idea?
有人有什么主意吗?
library(tidyverse)
dates <- c("2018-01-03", "2018-01-17", "2018-02-03", "2018-02-03", "2018-02-16",
"2018-02-26", "2018-03-03")
data <- as.data.frame(dates)
data <- mutate(data,
dates = lubridate::as_date(dates),
year_month=paste(lubridate::year(dates),
str_pad(lubridate::month(dates), width = 2, pad = 0),
sep="-"))
ggplot(data) +
stat_count(aes(x = year_month))
If you want the days to be distinguishable (not sure how useful this might be with a real world example) you can use fill
, notice factor(dates)
so that dates
is treated as a factor.如果您希望日期可以区分(不确定这对现实世界的示例有多大用处),您可以使用
fill
,注意factor(dates)
以便将dates
视为一个因素。
ggplot(data) +
stat_count(aes(x = year_month, fill=factor(dates)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.