1

Consider this simple example

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

mydf <- tibble(time = c(ymd('2019-01-01'),
                        ymd('2019-01-02'),
                        ymd('2019-01-03'),
                        ymd('2019-01-04'),
                        ymd('2019-01-05')),
       var1 = c(2,2,2,2,1),
       var2 = c(2,1,1,4,5),
       var3 = c(200, 200, 400, 500, 230)) 

Now this works

p1 <- mydf %>% 
  barchart(var1 + var2 ~ time, 
           data = ., 
           stack = TRUE,
           horiz = FALSE,
           par.settings = simpleTheme(col = c('red', 'blue'),
                                      fill = c('red', 'blue'),
                                      alpha = c(0.2)),
           auto.key = TRUE)

enter image description here

and this works as well

p2 <- mydf %>% 
  xyplot(var3 ~ time, data = ., type = 'l')

enter image description here

However, combining them with latticeExtra::doubleYscale() does not work. The line is invisible (see below)

latticeExtra::doubleYScale(p1, p2, use.style = FALSE)

Strangely enough, the dual y scale is there, but the line is missing. Any ideas?

Thanks!!

enter image description here

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • mate, you gotta stop using lattice, JK. – M-- Aug 22 '19 at 22:34
  • :D lattice is great buddy. ggplot is good but has some insane limitations (dual axes) – ℕʘʘḆḽḘ Aug 22 '19 at 22:36
  • It looks like a bug to me. If I remove `horiz = FALSE` I can get two plots to show (almost) however, the barchart will be distorted. Jokes aside, it seems to me that `lattice` is not maintained very well and I have experienced variety of bugs with it. I am with you on dual axis limitation of ggplot tho, it is annoying. You can get around it using some hacky methods (posted on SO). I personally would go for that hacky solution once in a while, rather than dealing with bugs always. Cheers. – M-- Aug 22 '19 at 22:54
  • On the reason for the limitations please see this post: https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales/3101876#3101876 – hannes101 Sep 03 '19 at 06:43
  • ah please stop with these BS ggplot arguments. every field on earth uses double Y axes... – ℕʘʘḆḽḘ Sep 03 '19 at 12:14

2 Answers2

1

Using ggplot2, you could do:

library(tidyr)
library(ggplot2)

df1 <- mydf %>% 
  select(-var3) %>% 
  pivot_longer(
    cols = c(var1, var2), 
    names_to = "type",
    values_to = "value"
  )

df2 <- mydf %>% 
  select(time, var3)

ggplot(df1) +
  geom_col(aes(x = time, y = value, fill = type)) +
  geom_line(data = df2, aes(x = time, y = var3/100), size = 2) +
  ylab("var1, var2") +
  scale_y_continuous(sec.axis = sec_axis(~.*100, name = "var3"))

Plot using ggplot2, with two y-axis

M--
  • 25,431
  • 8
  • 61
  • 93
jmcastagnetto
  • 441
  • 2
  • 7
1

I simplified your data a bit.

Using as.layer (also from latticeExtra) rather than doubleYScale:

library(lattice)
library(latticeExtra)      

  mydf <- data.frame(t=1:5,x=c(2,2,2,2,1),
             y=c(2,1,1,4,5),z=c(200,200,400,500,230))

    p1 <- barchart(x+y~t,mydf,stack=TRUE,horiz=FALSE,
       par.settings = simpleTheme(col = c('red', 'blue'),
                                           fill = c('red', 'blue'),
                                           alpha = c(0.2)),
                auto.key = TRUE)

    p2 <- xyplot(z~t,mydf,type="l")

    p1+as.layer(p2,x.same=TRUE,y.same=FALSE,outside=TRUE)

I trust it also works with lubridated objects and tibbles.

EDIT: to clarify as.layer is also in latticeExtra package and add the plot.

bar chart plus xyplot

DaveTurek
  • 1,297
  • 7
  • 8