【发布时间】:2022-01-14 11:05:25
【问题描述】:
我正在按月查找颜色频率。我想用每个月的每种颜色的百分比制作一个折线图。这是我的数据:
ID color1 color2 color3 date
55 red blue NA 2020-03-15
67 yellow NA NA 2020-05-02
83 blue yellow NA 2020-05-17
78 red yellow blue 2020-05-15
43 green NA NA 2021-01-27
29 yellow green NA 2021-01-03
我需要这样的东西来绘制图表。我需要当月的文章数作为分母。所以如果ID有多种颜色(比如03/2020中的IDs都是蓝色和红色),总的百分比可以大于100。
Month n freq_blue freq_red freq_yellow freq_green %_blue %_red _yellow %_green
03-2020 1 1 1 0 0 100 100 0 0
04-2020 0 0 0 0 0 0 0 0 0
05-2020 3 2 1 3 0 66.7 33.3 100 0
06-2020 0 0 0 0 0 0 0 0 0
07-2020 0 0 0 0 0 0 0 0 0
08-2020 0 0 0 0 0 0 0 0 0
09-2020 0 0 0 0 0 0 0 0 0
10-2020 0 0 0 0 0 0 0 0 0
11-2020 0 0 0 0 0 0 0 0 0
12-2020 0 0 0 0 0 0 0 0 0
01-2021 2 0 0 1 2 0 0 50 100
【问题讨论】:
-
到目前为止您尝试过什么?一些代码可以帮助你更清楚你到底想要做什么
-
df$date % mutate(month = month(date), year = year(date)) df2 % group_by(month,year) %>% mutate(count=length(unique(PMID))) df2% pivot_longer(cols = starts_with("color")) %>% filter(!is.na( value)) %>% group_by(month, year, value) %>% count() %>% group_by(month, year) %>% mutate(percent = n/count) %>% ungroup() %>% complete (年,月 = 1:12,值 = c(“蓝色”,“红色”,“黄色”,“绿色”),填充 = 列表(n = 0,百分比 = 0))%>%
-
pivot_wider(id_cols = c(month, year), names_from = value, values_from = c(n, percent))
-
这就是我目前所尝试的,一行中的字符太多,抱歉
-
你可以edit这个问题来包含你的代码,这样会更容易理解