如何更改格子图中的填充颜色? [英] how to change the fill color in a lattice plot?

查看:71
本文介绍了如何更改格子图中的填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此示例

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

我正在尝试从ggplot

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

我快到了,但是在下面的示例中着色是错误的

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)) 

有什么想法吗?谢谢!

推荐答案

这是使用simpleTheme的一种解决方案:

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")))

我希望这是您想要的.

这篇关于如何更改格子图中的填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆