1

I have a problem in combining two plots. I simply want to make one plot with those two.

Here is an image of plots

Is it even possible to combine those kind of plots together? (I'm using facet_grid in one plot). Without the facet_grid i simply used gtable but in this case I'm helpless

Final result:

Final plot

Code of plots below

#Bar plot
    SortData <- with(data,data[order(Month,Fac1,Alpha),])
    SortData$Month2 <- factor(SortData$Month, labels = c("January","February","March","April","May")) 

    p1<- ggplot(SortData, aes(x = Alpha, y = Count, fill = Fac1))+
      geom_bar(stat="identity") +
      facet_grid(~Month2)+

      scale_fill_manual(labels = c("Yes","No"),
                        values = c("#006600","#FF0000"))+
      scale_y_continuous(name="",
                         breaks = seq(0,8000,1000),
                         labels = seq(0,8000,1000),
                         limits = c(0,8000))+ 
      labs(x="")+
      theme_bw()+
      theme(legend.position = "bottom",
            legend.title = element_blank(),
            panel.grid.major.x = element_blank(),
            panel.border = element_blank(), 
            axis.text = element_text(face = "bold"),
            plot.title = element_text(lineheight  = 1, face = "bold"))


    #Line plot
    p2 <- ggplot(data, aes(Month, Fac2,colour = Alpha, group = Alpha))+ 
      geom_line(size =1.5) +
      geom_point(size = 5)+
      scale_linetype("")+
      scale_x_continuous(name = "",
                         breaks = seq(201601,201610,1),
                         labels = 1:10)+
      scale_y_continuous(name="",
                         breaks = seq(0,0.18,0.02),
                         labels = percent,
                         limits = c(0,0.18))+
      scale_color_manual("Info ",values = c("#008080","#FF00FF","#808000"), labels = c("A","B","C"))+
      theme_classic()+
      theme(panel.background = element_rect(fill = NA),
            axis.text = element_text(face = "bold"),
            legend.position = "bottom")
Krisow15
  • 11
  • 2
  • Could you include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610)? – Jaap Dec 30 '16 at 09:09
  • Do you mean that you want the lineplot on top of the barplot with a secondary axis on the side? – Marijn Stevering Dec 30 '16 at 09:15
  • 1
    Using double y-axis is generally a bad idea. See [this Q&A](http://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y-axis-on-the-right) for a discussion about this and some possible solutions. – Jaap Dec 30 '16 at 09:16
  • Thats what I want to do! :D ofc the line plot should relate to second Y axi on the right (axi with scale of plot p2) – Krisow15 Dec 30 '16 at 09:17
  • I have added a dataset – Krisow15 Dec 30 '16 at 09:22
  • Also see [here](http://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot/40746716#40746716). – Axeman Dec 30 '16 at 09:28
  • Ofc the line plot doesnt have to be over the bar plot :) It can just be "on" it. It's related to data provided. I just want that the bar plot use the left Y axi and the line plot use right Y axi. – Krisow15 Dec 30 '16 at 09:28
  • 1
    The secondary axis is not a problem. The actual problem is creating a line plot connecting separate facets. That requires a lot of work at the grid level because it is not supported by ggplot2 at all (and simply a bad idea). – Roland Dec 30 '16 at 09:35
  • Thats what I wanted to know! @Roland Do you have any other idea how to present those data? I have tried putting Month on X axis in bar plot and also adding group = Alpha in aes(), then there is a need to add geom_bar(position = "dodge"). Then, I combined those plots using gtable but the data on bar plot rev. I mean red one on green position and green on red. – Krisow15 Dec 30 '16 at 10:08

0 Answers0