Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-ggplot显示二进制标志变量(0/1)随时间的分布,如标准化条形图(%)_R_Ggplot2 - Fatal编程技术网

R-ggplot显示二进制标志变量(0/1)随时间的分布,如标准化条形图(%)

R-ggplot显示二进制标志变量(0/1)随时间的分布,如标准化条形图(%),r,ggplot2,R,Ggplot2,我有一个看起来像这样的数据集 Date Remaining Volume ID 1990-01-01 0 1000 1 1990-01-01 1 2000 2 1990-01-01 1 5000 3 1990-02-01 0 200 4 1990-03-01 1 4000 5 1990-03-01 0 3000

我有一个看起来像这样的数据集

Date        Remaining  Volume   ID
1990-01-01  0          1000     1
1990-01-01  1          2000     2
1990-01-01  1          5000     3
1990-02-01  0          200      4
1990-03-01  1          4000     5
1990-03-01  0          3000     6
我根据一系列条件语句过滤数据,并将二进制标志变量分配给data.table。值
0
表示特定行条目不符合定义的要求,随后将被排除
1
-标记行保留在data.table中。该键为
ID
,对于每一行都是唯一的

我想展示两种关系

(1)月度时间序列上的堆叠标准化/百分比条形图,显示每月数据集中剩余/排除的条目百分比

f、 前。1990年1月-->2/3剩余值-->66.6%与33.3%的条目被排除在外

(2)一个叠加的标准化/百分比条形图,显示每月过滤操作剩余/排除的卷的标准化百分比

f、 前。1990年1月-->剩余8k中的2k+5k-->87.5%对12.5%的交易量被排除在外

到目前为止,我试过各种各样的方法。计算每个标志值每月发生的次数以及相应的“bucket”(0/1)卷的总和,但到目前为止我所有的尝试都失败了

# dt_1 is the original data.table
id.vec <- dt_1[ , id]
dt_2 <- dt_1
# dt_1 is filterd subsequently
id_remaining.vec <- dt_1[ , id]

dt_2 <- dt_2[id.vec %in% id_remaining.vec, REMAIN := 1]
dt_2 <- dt_2[id.vec %notin% id_remaining.vec, REMAIN := 0]
dt_2 <- dt_2[REMAIN == 1 , N_REMAIN := .N]
dt_2 <- dt_2[REMAIN == 1 , N_REMAIN_MON := .N]

# Tried the code below to no avail
ggplot(data = dt_2, aes(x = Date, y = REMAIN, color = REMAIN, fill = REMAIN)) +
  geom_bar(position = "fill", stat = "identity")
#dtu 1是原始数据表

id.vec下面是我如何使用dplyr

library(dplyr)
dt_2 %>%
  mutate(Remaining = as.character(Remaining)) %>%  # just to make the charts use scale_fill_discrete by default
  group_by(Date, Remaining) %>%
  summarize(entries = n(),
         volume = sum(Volume)) %>%
  mutate(share_entries = entries / sum(entries),
            share_volume = volume / sum(volume)) %>%
  ungroup() -> dt_2_summary

> dt_2_summary
# A tibble: 5 x 6
  Date       Remaining entries volume share_entries share_volume
  <chr>      <chr>       <int>  <int>         <dbl>        <dbl>
1 1990-01-01 0               1   1000         0.333        0.125
2 1990-01-01 1               2   7000         0.667        0.875
3 1990-02-01 0               1    200         1            1    
4 1990-03-01 0               1   3000         0.5          0.429
5 1990-03-01 1               1   4000         0.5          0.571


就像乔恩伟大思想的附录一样

我有一个加载了超过25个库的大型项目,虽然建议的代码似乎有效,但它只对share_条目有效,而对share_卷无效。
dt_2_摘要
的输出很奇怪。share_entries(共享条目)列显然是按条目总数计算的,而不是在每个组内,share_volume(共享卷)列只显示了
NAs

经过数小时的故障排除,我确定罪魁祸首是pkg
plyr
,它确实覆盖了一些功能。因此,我必须指定要使用的应用函数的版本

下面的代码帮了我的忙

library(plyr) # the culprit
library(dplyr)
dt_2 %>%
  dplyr::mutate(Remaining = as.character(Remaining)) %>%
  group_by(Date, Remaining) %>%
  dplyr::summarize(entries = n(),
         volume = sum(Volume)) %>%
  dplyr::mutate(share_entries = entries / sum(entries),
            share_volume = volume / sum(volume)) %>%
  ungroup() -> dt_2_summary

再次感谢Jon提供的精彩解决方案

真是太棒了,约翰!我特别喜欢您提出的解决方案,因为由于事先的数据争用,
ggplot()
调用非常紧凑。你的答案正是我所希望的,还有更多!非常感谢!不过还有一个简短的后续问题。在分组和汇总之前,为什么必须将
整数
保留
更改为
字符
?我现在尝试了几个版本,但我花了很多时间进行调试,因为在它最终起作用之前,我得到了一些奇怪的结果!顺便说一句,很抱歉之前拼错了你的名字,乔恩:)我把“保持”改为“仅字符”,这样它将成为进入ggplot的离散变量。否则它会以连续的比例绘制0和1,这似乎是最快的方法。啊,这是有道理的。通常我只是在
aes()
映射定义中使用
as.factor(xyz)
,该变量是我要用于着色/填充我的
geom
的变量。但是你的方式也很优雅。再次感谢乔恩!最后,这基本上只是我的情节的主干,我进一步定制了它(日期标签等)
dt_2_summary %>%
  ggplot(aes(Date, share_volume, fill = Remaining)) +
  geom_col()
library(plyr) # the culprit
library(dplyr)
dt_2 %>%
  dplyr::mutate(Remaining = as.character(Remaining)) %>%
  group_by(Date, Remaining) %>%
  dplyr::summarize(entries = n(),
         volume = sum(Volume)) %>%
  dplyr::mutate(share_entries = entries / sum(entries),
            share_volume = volume / sum(volume)) %>%
  ungroup() -> dt_2_summary