如何从两个变量的频率生成时间曲线的平稳状态图

时间:2019-07-15 21:31:16

标签: r

具有数据框的结果:

dframe <- structure(list(stock = c("Google, Yahoo", "Google", "Google, Yahoo", 
                                   "Google, Yahoo", "Amazon, Google", "Amazon, Google", "Amazon, Google"
), investor = c("Nathalie", "George", "Nathalie", "George", "Melanie", 
                "George", "Melanie"), year = c("2017", "2018", "2017", "2017", 
                                               "2018", "2018", "2017"), n = c(2L, 1L, 2L, 1L, 1L, 1L, 1L)), class = c("tbl_df", 
                                                                                                                      "tbl", "data.frame"), row.names = c(NA, -7L))
# A tibble: 7 x 4
  stock          investor year      n
  <chr>          <chr>    <chr> <int>
1 Google, Yahoo  Nathalie 2017      2
2 Google         George   2018      1
3 Google, Yahoo  Nathalie 2017      2
4 Google, Yahoo  George   2017      1
5 Amazon, Google Melanie  2018      1
6 Amazon, Google George   2018      1
7 Amazon, Google Melanie  2017      1

有没有一种友好的方法可以生成时间演变图(年列),该图将显示成对的股票投资人列的频率(第n列)?

1 个答案:

答案 0 :(得分:1)

library(tidyverse)
dframe %>%
  separate_rows(stock) %>%
  count(stock, investor, year, wt = sum(n)) %>%
  ggplot(aes(year, n, fill = interaction(investor, stock))) +
  geom_col()

enter image description here