0

I want to create a ggplot with dual Y axis. On the left Y axis I want to show the average duration, while on the right Y axis - the average absolute frequency per weekday (i.e. the average number of occurances of each weekday over different dates).

        df = data.frame(c("18/10/2016","18/10/2016","14/10/2016","20/10/2016","11/10/2016"),
                        c("tuesday","tuesday","friday","thursday","tuesday"),
                        c("20","12","23","24","21"))
        colnames(df) = c("date","day","duration")

This is what I tried so far (this code works, but please see the list of issues below):

library("gtable")
library("grid")
library("ggplot2")

 #two plots
p1 <- ggplot(df, aes(day, duration)) + 
  geom_line(colour="#000099", position = "dodge", 
            stat = "summary", fun.y = "mean") +
  geom_point(size=3, colour="#CC0000") +
  ylab("Average duration") +
  theme_bw()

p2 <- ggplot(df) +
  geom_bar(aes(x=day), fill="#FF9999", colour="black",
           position = "dodge", stat = "summary", fun.y = "mean") + 
  ylab("Absolute frequency") +
  theme_bw()

# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
## DO NOT UNDERSTAND THIS LINE
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

# draw it
grid.draw(g)

There are the following problems with this code:

1) I can only see bars (p2), but I don't see p1 - a blue line with red dots.

2) The label of the right Y axis (p2) does not appear

3) And the MOST important: It looks like p2 counts total number of occurences of weekdays, e.g. 4 tuesdays. However I want to group them by dates. For example, there are 2 occurances of tuesdays, 18/10/2016, AND there is 1 occurance of tuesday, 11/10/2016. So, ON AVERAGE (2+1)/2 => 1.5.

FiofanS
  • 309
  • 2
  • 6
  • 13
  • Are you sure you provided enough data? I get a warning for p1, and p2 won't work. – Haboryme Oct 20 '16 at 11:45
  • @Haboryme: I've just copied-pasted the published code in my terminal and it worked. Maybe that's the issue with libraries? I use `library("gtable")`, `library("grid")` and `library("ggplot2")` – FiofanS Oct 20 '16 at 11:50
  • Got all 3 libraries installed now (only had ggplot2), but still getting the errors. – Haboryme Oct 20 '16 at 11:54
  • Like @Haboryme, the plots `p1` and `p2` do not appear to be correct, and `p2` has an error in it (missing a `y` aesthetic). However, the shorter answer is: don't make dual axis plots. They are almost always a bad idea. Try faceting instead, or if you are interested in the interaction of two variables, plot them directly against each other instead of over time. https://kieranhealy.org/blog/archives/2016/01/16/two-y-axes/ and http://stackoverflow.com/a/3101876/2966222 – Mark Peterson Oct 20 '16 at 13:36
  • I can't get your code to run, but here are three examples that work with ggplot2 ver 2.1.0 (dealing with points 1 and 2): [ggplot2-overlay-of-barplot-and-line-plot](http://stackoverflow.com/questions/37094586/ggplot2-overlay-of-barplot-and-line-plot/37098206#37098206), [reproduce-a-the-economist-chart-with-dual-axis](http://stackoverflow.com/questions/37347115/reproduce-a-the-economist-chart-with-dual-axis/37369764#37369764), and [how-to-use-facets-with-a-dual-y-axis-ggplot](http://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot/37336658#37336658). – Sandy Muspratt Oct 20 '16 at 20:35
  • However, they could break with the next ggplot2 version. – Sandy Muspratt Oct 20 '16 at 20:38

0 Answers0