R ggplot:创建比例的线图

R ggplot:创建比例的线图,r,ggplot2,data.table,R,Ggplot2,Data.table,我有一个包含59101个观察值的数据表,我想通过月.年列(即2014年1月,2014年2月,…,2020年3月)中的唯一值,绘制一个线条图,而不是条形图 库(data.table) 种子集(1992) DT ID暴露指数\日期月份年份 1:1 g 2017-11-2017年11月26日 2:2我2019-10-11 2019年10月 3:3 2015-02-27 2015年2月 4:4 2016年10月4日至10月18日 5:5 e 2019-06-06 2019年6月 ---

我有一个包含59101个观察值的数据表,我想通过
月.年
列(即2014年1月,2014年2月,…,2020年3月)中的唯一值,绘制一个线条图,而不是条形图

库(data.table)
种子集(1992)
DT
ID暴露指数\日期月份年份
1:1 g 2017-11-2017年11月26日
2:2我2019-10-11 2019年10月
3:3 2015-02-27 2015年2月
4:4 2016年10月4日至10月18日
5:5 e 2019-06-06 2019年6月
---                                     
59097:59097 e 2015-07-22 2015年7月
59098:59098 j 2017-09-04 2017年9月
59099:59099A 2018-04-26 2018年4月
59100:59100 a 2019-12-02 2019年12月
59101:59101 g 2014-11-04 2014年11月
我希望我的数据看起来像我附加在问题上的图像(见下图)

在过去,我已经能够用ggplot生成绘图,但我发现我在准备数据方面最为困难,因此我的代码最终是相当初级和临时的;我希望我的代码尽可能简单和干净

我已经看到,当使用ggplot绘图时,融合数据是最好的方法,但我还没有建立大脑肌肉记忆来本能地知道如何1)以那种方式准备数据,2)通过ggplot语法传递数据以创建我需要的


如果有人知道或有关于如何做到这一点的建议,我们将不胜感激。

这里有一个使用tidyverse的解决方案。需要绘制的数据很多,有时候小倍数会更好

库(data.table)
种子集(1992)
DT%作为不兼容()%>%
集团单位(月、年、风险敞口)%>%
计数()%>%
解组()%>%
分组单位(月.年)%>%
突变(ttl=和(n),
pct_ttl=n/ttl,
日期=润滑油::myd(月.年,截断=1L))%>%
打印(n=20)%>%
ggplot(aes(日期、pct\U ttl、颜色=曝光、组=曝光))+
geom_线()+
缩放日期(缩放日期(日期间隔=“4个月”,日期标签=“%b%Y”))+
比例连续(标签=比例::百分比格式(精度=1))+
主题(axis.text.x=元素\文本(角度=90))+
实验室(y=”比例“,x=”)
#>#A tible:750 x 6
#>#组:月.年[75]
#>月.年风险n ttl pct\U ttl日期
#>                       
#>2014年1月1日a 66 793 0.0832 2014-01-01
#>2014年1月2日b 83 793 0.105 2014-01-01
#>2014年1月3日c 66 793 0.0832 2014-01-01
#>2014年1月4日d 93 793 0.117 2014-01-01
#>2014年1月5日e 76 793 0.0958 2014-01-01
#>2014年1月6日f 71 793 0.0895 2014-01-01
#>2014年1月7日g 87 793 0.110 2014-01-01
#>2014年1月8日h 77 793 0.0971 2014-01-01
#>2014年1月9日i 87 793 0.110 2014-01-01
#>2014年1月10日j 87 793 0.110 2014-01-01
#>2014年2月11日a 79 708 0.112 2014-02-01
#>2014年2月12日b 66 708 0.0932 2014-02-01
#>2014年2月13日c 69 708 0.0975 2014-02-01
#>2014年2月14日d 69 708 0.0975 2014-02-01
#>2014年2月15日e 69 708 0.0975 2014-02-01
#>2014年2月16日f 78 708 0.110 2014-02-01
#>2014年2月17日g 71 708 0.100 2014-02-01
#>2014年2月18日h 67 708 0.0946 2014-02-01
#>2014年2月19日i 65 708 0.0918 2014-02-01
#>2014年2月20日j 75 708 0.106 2014-02-01
#> # ... 还有730行

由(v0.3.0)于2020年4月24日创建,由于其中包含标签,因此以下是准备数据的方法:

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
  • ggplot
    调用中包括数据转换
  • 将另一个
    [data.table
    调用并使用
    ggplot(.SD)
  • 连接
    magrittr
    以使用管道

  • 另一个答案为
    ggplot()
    调用提供了很好的格式,因此请参阅另一个答案,了解如何使图形看起来更漂亮。

    这是一个很好的答案;)快速建议,也许您可以使用
    date\u breaks=“4个月”,date\u labels=“%b%Y”
    scale\u x\u date
    。不客气;)。因此,由于
    month.year
    已经订购,您可以通过将
    month.year
    传递为x,并使用
    scale\u discrete
    设置适当的标签,例如:
    DT%>%count(month.year,exposure)%%>%ggplot(aes(x=month.year,y=n,group=exposure,color=exposure))+geom_line()+scale_x_离散(breaks=levels(DT$month.year)[seq(1,length(levels(levels(DT$month.year)),by=4)]
    ugh-这是一个更好的答案。:)您的也非常好,因为它会考虑到月。年尚未订购,如果您希望显示不同的时间框架。
    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(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()
    
    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()]
    
    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()