1

I am pushing lattice to its limits.

Consider this example

tibble(time = c(ymd('2019-01-01'),
                      ymd('2019-01-02'),
                      ymd('2019-01-03'),
                      ymd('2019-01-01'),
                      ymd('2019-01-02'),
                      ymd('2019-01-03'),
                      ymd('2019-01-01'),
                      ymd('2019-01-02'),
                      ymd('2019-01-03')),
             variable = c('a','a','a','b','b','b', 'c','c','c'),
             value = c(1,2,3,0,0,2,2,4,3)) %>% 
  ggplot(aes(x = time, y = value, fill = variable)) + geom_area()

enter image description here

Using the nice solution in how to create a stacked area chart in lattice? does not work here, perhaps because we have multiple areas.

Can we still do it with lattice? Thanks!

M--
  • 25,431
  • 8
  • 61
  • 93
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • related: https://stackoverflow.com/questions/56579576/how-to-change-the-fill-color-in-a-lattice-plot for future reference – M-- Jun 13 '19 at 14:53

1 Answers1

2
library(dplyr)
library(lubridate)
library(lattice)
library(latticeExtra)

df1 <- tibble(time = c(ymd('2019-01-01'),
                       ymd('2019-01-02'),
                       ymd('2019-01-03'),
                       ymd('2019-01-01'),
                       ymd('2019-01-02'),
                       ymd('2019-01-03'),
                       ymd('2019-01-01'),
                       ymd('2019-01-02'),
                       ymd('2019-01-03')),
              variable = c('a','a','a','b','b','b', 'c','c','c'),
              value = c(1,2,3,0,0,2,2,4,3))

df2 <- df1 %>% group_by(time) %>% mutate(val=cumsum(value))

xyplot(val~time, df2, group=variable,
       panel=function(x,y,...){
         panel.xyarea(x,y,...)
         panel.xyplot(x,y,...)},
       alpha=c(0.9,0.6,0.3)) 

Created on 2019-06-13 by the reprex package (v0.3.0)

M--
  • 25,431
  • 8
  • 61
  • 93
  • nice! but are you able to set the colors using the `par.settings` trick? it does not work for me – ℕʘʘḆḽḘ Jun 13 '19 at 14:47
  • thanks bro. pretty useful. ggplot is pretty useless for time series – ℕʘʘḆḽḘ Jun 13 '19 at 14:53
  • @ℕʘʘḆḽḘ you can use `par.settings=simpleTheme(col=c("red", "blue","green")), alpha=c(1,0.3,0.1)` not perfect tho. And you kidding about `ggplot` and `timeseries` right? – M-- Jun 13 '19 at 14:58
  • yeah... the problem is that you cant really choose the colors because they are overlayed... on my real chart this is super messy... maybe there is an alternative solution here? – ℕʘʘḆḽḘ Jun 13 '19 at 15:01
  • @ℕʘʘḆḽḘ what's your beef with `ggplot`? that would do the job. If you want to have `a` at the bottom and `c` at top, just refactor the data. – M-- Jun 13 '19 at 15:05
  • thanks bro. just kidding about ggplot. but lattice is great for dual axis and quick ts viz – ℕʘʘḆḽḘ Jun 13 '19 at 15:22
  • maybe some additional subtleness. do you know how to add a legend/key that maps to the colors? – ℕʘʘḆḽḘ Jun 13 '19 at 15:50
  • @ℕʘʘḆḽḘ maybe this would work: https://stackoverflow.com/questions/35805016/xyplot-time-series-with-positive-values-in-green-negative-in-red-in-r – M-- Jun 13 '19 at 16:06
  • @ℕʘʘḆḽḘ Last comment. I am crazy, but you can try to make couple shp files with the points for each of those areas and plot those shape files and then add an axis and make the labels to show what you actually need. All I can think of. Again, that's crazy. It's much easier to fool ggplot and secondary axis with different scale than doing what I just said. – M-- Jun 13 '19 at 16:27