I am having a hard time for what should be a trivial task.
I'd like to make a faceted scatterplots with two datasets at 2 sites. I have a "standard" wide dataframe, proceeding from a merge and then I'd like to melt it in a smart way for a faceted scatterplot. But I am struggling.
## synthetic dataset
bb <- cbind.data.frame("date.hh" = seq.Date(as.Date("1900-01-01"), length.out = 10, by = "day"),
"var_110.siteA" = rnorm(10), "var_110.siteB" = rnorm(10), "var_200.siteA" = rnorm(10), "var_200.siteB" = rnorm(10))
## one of my attempts:
bb %>%
tidyr::pivot_longer(., cols = -c(date.hh)) %>% ## make it all long, but keep festa and five as separated columns
tidyr::separate(., col = name, sep = "_", into = c("var", "prop")) %>%
tidyr::separate(., col = prop, sep = "\\.", into = c("prop", "site"))
## this results:
# A tibble: 40 x 5
date.hh var prop site value
<date> <chr> <chr> <chr> <dbl>
1 1900-01-01 var 110 siteA 0.353
2 1900-01-01 var 110 siteB 0.521
3 1900-01-01 var 200 siteA 0.258
4 1900-01-01 var 200 siteB -0.261
5 1900-01-02 var 110 siteA -0.802
6 1900-01-02 var 110 siteB 0.631
7 1900-01-02 var 200 siteA 0.620
8 1900-01-02 var 200 siteB 0.875
9 1900-01-03 var 110 siteA 0.150
10 1900-01-03 var 110 siteB 0.107
# … with 30 more rows
then I am stuck. I have fiddled with several pivot_wider, group_by etc with no luck.
I'd like a dataset as the following one, and it should not be so distant.
siteA siteB prop date.hh
0.353 0.521 110 1900-01-01
0.258 -0.261 200 1900-01-01
-0.802 0.631 110 1900-01-02
## etc...
Afterwards I can ggplot it using x = siteA, y = siteB and facet = prop.
I hope I made myself clear. Any help is appreciated.
Thx, AB