ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19405

how to change the fill color in a lattice plot?

Consider this example

library(tibble)
library(lubridate)
library(lattice)
library(latticeExtra)

df <- 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')),
       variable = c('a','a','a','b','b','b'),
       value = c(1,2,3,0,0,2))

# A tibble: 6 x 3
  time       variable value
  <date>     <chr>    <dbl>
1 2019-01-01 a            1
2 2019-01-02 a            2
3 2019-01-03 a            3
4 2019-01-01 b            0
5 2019-01-02 b            0
6 2019-01-03 b            2

I am trying to reproduce the stacked area chart from ggplot

  df %>% ggplot(aes(x = time, y = value, fill = variable)) +
  geom_area()

enter image description here

I am almost there, but the coloring is wrong in the following example

  df  %>%  group_by(time) %>% 
  mutate(value = ifelse(variable == 'a', sum(value), value)) %>% 
  ungroup() %>% 
  xyplot(value~time, data = ., group=variable,
     panel=function(x,y,...){
     panel.xyarea(x,y,...)
     panel.xyplot(x,y,...)},
     col=c("red","blue"),
     alpha=c(0.8,0.4)) 

enter image description here

Any ideas? Thanks!

Upvotes: 0

Views: 300

Answers (1)

jkrainer
jkrainer

Reputation: 423

here is one solution by using simpleTheme:

df  %>%  group_by(time) %>% 
     mutate(value = ifelse(variable == 'a', sum(value), value)) %>% 
     ungroup() %>% 
     xyplot(value~time, data = ., group=variable,
            panel=function(x,y,...){
                panel.xyarea(x,y,...)
                panel.xyplot(x,y,...)}, par.settings=simpleTheme(col=c("red", "blue")))

I hope it is what you are looking for.

Upvotes: 1

Related Questions