如何使用不同的轴ggplot2从两个不同的图创建一个单独的图

时间:2016-12-30 09:06:28

标签: r plot ggplot2

我在组合两个地块时遇到了问题。我只是想用这两个制作一个情节。

Here is an image of plots

甚至可以将这些情节组合在一起吗? (我在一个图中使用facet_grid)。没有facet_grid我只是使用gtable但在这种情况下我无助

最终结果:

Final plot

以下情节代码

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

0 个答案:

没有答案