[R] Help with steam graph

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Mon Oct 3 04:33:37 CEST 2022


I think you are being dishonest. That code does not appear on hrbrmstr's 
vignette at least in a form that I recognize.

When I run your code from the first posting with all the instances of 
`com_num` replaced by `com_name` and removing the `pointer` entry in dat 
which throws an error when trying to define dat, I get

Error in `group_by()`: ! Must group by variables found in `.data`. ✖ 
Column `com_name` is not found. So I "rewind the process to the point 
where the error is reported and find

> dat %>%+ select(year, month, company, share, com_name) %>% + 
tidyr::gather(company, share, -year) # A tibble: 148 × 3 year company 
share <dbl> <chr> <chr> 1 2018 month 12 2 2019 month 1 3 2019 month 2 4 
2019 month 3 5 2019 month 4 6 2019 month 5 7 2019 month 6 8 2019 month 7 
9 2017 month 1 10 2017 month 2 # … with 138 more rows # ℹ Use `print(n = 
...)` to see more rows So the "gathering" process seems to have removed 
the `com_name` column. Can exit R without saving your workspace and then 
construct a series of R commands that will create a reproducible 
example? -- David.

On 10/2/22 10:03, Tariq Khasiri wrote:
> Actually in my main data the column name is com_num ( where 
> mistakenly I pasted the sample data here under the com_name ). So, 
> when I run the command successfully this is the error shows up -
>
>     ▆
>   1. ├─... %>% sg_legend(show = TRUE, label = "Share: ")
>   2. ├─streamgraph::sg_legend(., show = TRUE, label = "Share: ")
>   3. ├─streamgraph::sg_fill_brewer(., "PuOr")
>   4. ├─streamgraph::sg_axis_x(., 0.8)
>   5. ├─streamgraph::streamgraph(., "com_num", "n", "year")
>   6. │ └─base::data.frame(data)
>   7. ├─dplyr::ungroup(.)
>   8. ├─dplyr::tally(., wt = share)
>   9. ├─dplyr::group_by(., year, com_num)
>  10. └─dplyr:::group_by.data.frame(., year, com_num)
>  11.   └─dplyr::group_by_prepare(.data, ..., .add = .add, caller_env = 
> caller_env())
>  12.     └─rlang::abort(bullets, call = error_call)
>
> Any suggestions on how I can fix it ??
>
> On Sun, 2 Oct 2022 at 09:12, David Winsemius <dwinsemius using comcast.net> 
> wrote:
>
>     I don’t see a column with the name ‘com_num’, so the error message
>     makes complete sense.
>
>>     David
>
>     Sent from my iPhone
>
>     > On Oct 2, 2022, at 5:06 AM, Tariq Khasiri
>     <tariqkhasiri using gmail.com> wrote:
>     >
>     > Hi, i'm trying to create a steamgraph with the following data
>     by creating a
>     > unit indicator by combing the year and month. But, I'm getting
>     error as :
>     >
>     > Error in `group_by()`:
>     > ! Must group by variables found in `.data`.
>     > ✖ Column `com_num` is not found.
>     > Run `rlang::last_error()` to see where the error occurred.
>     >
>     > ### Packages needed for the code
>     > devtools::install_github("hrbrmstr/streamgraph")
>     >
>     > library(tidyverse)
>     > library(ggplot2)
>     > library(dplyr)
>     > library(steamgraph)
>     >
>     > ### Code ( The following code can be found on creator's account
>     > https://hrbrmstr.github.io/streamgraph/ )
>     >
>     > dat %>%
>     > select(year, month, company, share, com_num) %>%
>     >  tidyr::gather(company, share, -year) %>%
>     >  group_by(year, com_num) %>%
>     >  tally(wt=share) %>%
>     >  ungroup %>%
>     >  streamgraph("com_num", "n", "year") %>%
>     >  sg_axis_x(0.8) %>%
>     >  sg_fill_brewer("PuOr") %>%
>     >  sg_legend(show=TRUE, label="Share: ")
>     >
>     >
>     > ### data is like the following
>     >
>     > dput(dat)
>     > structure(list(year = c(2018, 2019, 2019, 2019, 2019, 2019, 2019,
>     > 2019, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
>     > 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
>     > 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019), month = c(12,
>     > 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,
>     > 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5), company =
>     c("ABC",
>     > "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "FGH", "FGH",
>     > "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH",
>     > "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH",
>     > "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH", "FGH"
>     > ), share = c(20, 16.5, 15, 15.5, 15.5, 16, 17, 16.5, 61, 55,
>     > 53, 53, 54, 53, 58, 54, 50, 47, 55, 50, 52, 51, 51.5, 52, 53,
>     > 54, 55, 53, 54, 50, 42, 48, 41, 40, 39, 36.5, 35), com_name = c(1,
>     > 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
>     > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), row.names = c(NA,
>     > -37L), spec = structure(list(cols = list(year =
>     structure(list(), class =
>     > c("collector_double",
>     > "collector")), month = structure(list(), class =
>     c("collector_double",
>     > "collector")), company = structure(list(), class =
>     c("collector_character",
>     > "collector")), share = structure(list(), class =
>     c("collector_double",
>     > "collector")), com_name = structure(list(), class =
>     c("collector_double",
>     > "collector"))), default = structure(list(), class =
>     c("collector_guess",
>     > "collector")), delim = ","), class = "col_spec"), problems =
>     <pointer:
>     > 0x7fd732028680>, class = c("spec_tbl_df",
>     > "tbl_df", "tbl", "data.frame"))
>     >
>     >    [[alternative HTML version deleted]]
>     >
>     > ______________________________________________
>     > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>     > https://stat.ethz.ch/mailman/listinfo/r-help
>     > PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>     > and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list