我有一个59101个观测值的数据表,我想制作一个exposure
列中的比例按{{ 1}}列(即 2014年1月,2014年2月,...,2020年3月)。
month.year
过去我已经可以使用ggplot生成图表,但是我发现我在准备数据方面遇到了最大的困难,因此我的代码最终变得非常初级和特别。我想让我的代码尽可能简单和干净。
我已经看到,在使用ggplot进行绘图时,融化数据是最好的方法,但是我还没有建立大脑肌肉的记忆来直觉地知道如何1)以这种方式准备数据并2)传递数据通过ggplot语法创建我需要的内容。
如果有人知道或建议如何做,将不胜感激。
答案 0 :(得分:3)
这是使用tidyverse的解决方案。要绘制很多数据,有时较小的倍数会更好。
library(data.table)
set.seed(1992)
DT <- data.table::data.table(ID=seq(1,59101),
exposure=as.factor(sample(letters[1:10],59101,replace = TRUE)),
index_date=sample(seq(as.Date('2014/01/01'), as.Date('2020/03/31'), by="day"),59101,replace = TRUE))
DT[,month.year:=as.factor(zoo::as.yearmon(index_date)),]
library(tidyverse)
DT %>% as_tibble() %>%
group_by(month.year, exposure) %>%
count() %>%
ungroup() %>%
group_by(month.year) %>%
mutate(ttl = sum(n),
pct_ttl = n / ttl,
date = lubridate::myd(month.year, truncated = 1L)) %>%
print(n = 20) %>%
ggplot(aes(date, pct_ttl, color = exposure, group = exposure)) +
geom_line() +
scale_x_date(scale_x_date(date_breaks = "4 months", date_labels = "%b %Y"))+
scale_y_continuous(label = scales::percent_format(accuracy = 1)) +
theme(axis.text.x = element_text(angle = 90))+
labs(y = "proportion", x = "")
#> # A tibble: 750 x 6
#> # Groups: month.year [75]
#> month.year exposure n ttl pct_ttl date
#> <fct> <fct> <int> <int> <dbl> <date>
#> 1 Jan 2014 a 66 793 0.0832 2014-01-01
#> 2 Jan 2014 b 83 793 0.105 2014-01-01
#> 3 Jan 2014 c 66 793 0.0832 2014-01-01
#> 4 Jan 2014 d 93 793 0.117 2014-01-01
#> 5 Jan 2014 e 76 793 0.0958 2014-01-01
#> 6 Jan 2014 f 71 793 0.0895 2014-01-01
#> 7 Jan 2014 g 87 793 0.110 2014-01-01
#> 8 Jan 2014 h 77 793 0.0971 2014-01-01
#> 9 Jan 2014 i 87 793 0.110 2014-01-01
#> 10 Jan 2014 j 87 793 0.110 2014-01-01
#> 11 Feb 2014 a 79 708 0.112 2014-02-01
#> 12 Feb 2014 b 66 708 0.0932 2014-02-01
#> 13 Feb 2014 c 69 708 0.0975 2014-02-01
#> 14 Feb 2014 d 69 708 0.0975 2014-02-01
#> 15 Feb 2014 e 69 708 0.0975 2014-02-01
#> 16 Feb 2014 f 78 708 0.110 2014-02-01
#> 17 Feb 2014 g 71 708 0.100 2014-02-01
#> 18 Feb 2014 h 67 708 0.0946 2014-02-01
#> 19 Feb 2014 i 65 708 0.0918 2014-02-01
#> 20 Feb 2014 j 75 708 0.106 2014-02-01
#> # ... with 730 more rows
由reprex package(v0.3.0)于2020-04-24创建
答案 1 :(得分:1)
由于其中包含data.table标签,因此,这是一种准备数据的方法:
DT[,
{ n = .N
.SD[, .(rel_freq = .N / n), by = exposure]},
by = month.year]
month.year exposure rel_freq
<fctr> <fctr> <num>
1: Nov 2017 g 0.10840108
2: Nov 2017 f 0.10027100
3: Nov 2017 d 0.10162602
4: Nov 2017 i 0.09485095
5: Nov 2017 e 0.11382114
---
746: Jul 2018 f 0.10506799
747: Jul 2018 c 0.10259580
748: Jul 2018 a 0.10754017
749: Jul 2018 b 0.10135970
750: Jul 2018 g 0.11248455
然后,您可以进行ggplot2调用的几个选项。
DT_relative = DT[, {n = .N; .SD[, .(rel_freq = .N / n), by = exposure]}, by = month.year]
ggplot(DT_relative, aes(x = month.year, y = rel_freq, color = exposure, group = exposure)) + geom_line()
ggplot
调用中包含数据转换ggplot(DT[, {n = .N; .SD[, .(rel_freq = .N / n), by = exposure]}, by = month.year],
aes(x = month.year, y = rel_freq, color = exposure, group = exposure)) + geom_line()
[data.table
通话链接在一起并使用ggplot(.SD)
DT[, {n = .N; .SD[, .(rel_freq = .N / n), by = exposure]}, by = month.year
][, ggplot(.SD, aes(x = month.year, y = rel_freq, color = exposure, group = exposure)) + geom_line()]
magrittr
以使用管道library(magrittr)
DT[,
{ n = .N
.SD[, .(rel_freq = .N / n), by = exposure]},
by = month.year]%>%
ggplot(., aes(x = month.year, y = rel_freq, color = exposure, group = exposure)) + geom_line()
另一个答案包括ggplot()
调用的出色格式,因此,请参阅另一个答案以了解如何使图形更美观。