Function to build double y axis graph in ggplot2

后端 未结 3 950

I am testing a function to build a double y axis graph in ggplot2. It works but I can\'t get some elements from the input graphics. I have built these two graphs with two data f

相关标签:
3条回答
  • 2021-02-20 01:34

    Another smarter approach with sec_axis adapted from here.

    Advantage:

    1. Native secondary axis, only one ggplot object is created.
    2. No hacks involving explicitly tweaking axis objects.

    Disadvantage: Limits of the two axes must be defined beforehand.

    Refer to sec_axis documentation and the blog post (in Japanese) for more information.

    # You must define limits first
    
    AXIS1_MIN = 0
    AXIS1_MAX = max(Base1$value)
    AXIS2_MIN = min(Base2$value)
    AXIS2_MAX = max(Base2$value)
    
    scale_to_value1 <- function(values) rescale(values, to = c(AXIS1_MIN, AXIS1_MAX))
    scale_to_value2 <- function(values) rescale(values, to = c(AXIS2_MIN, AXIS2_MAX))
    
    ggplot() +
      geom_bar(aes(x = Month, y = value, fill = variable), data = Base1, stat="identity",colour="black",size=1) +
      geom_line(aes(x = Month, y = scale_to_value1(value),color = variable, group = variable), data = Base2, size=1.3) +
      scale_y_continuous(labels = comma,breaks=pretty_breaks(n=7),
                         limits=c(AXIS1_MIN, AXIS1_MAX),
                         sec.axis = sec_axis( ~ scale_to_value2(.), name = "value2")) +
      scale_color_manual(name = "variable", 
                        label = c("Index1", "Index2"), 
                        values = c("Index1" = "red", "Index2" = "green")) +
      scale_fill_manual(name = "variable", 
                        label = "Power",
                        values = "#FF6C91") +
      theme(axis.text.x=element_text(angle=90,colour="grey20",face="bold",size=12),
            axis.text.y=element_text(colour="grey20",face="bold",hjust=1,vjust=0.8,size=15),
            axis.title.x=element_text(colour="grey20",face="bold",size=16),
            axis.title.y=element_text(colour="grey20",face="bold",size=16)) +
      xlab('Month')+ylab('')+ ggtitle("My graph") +
      theme(plot.title = element_text(lineheight=3, face="bold", color="black",size=24)) +
      theme(legend.text=element_text(size=14),
            legend.title=element_text(size=14))
    
    0 讨论(0)
  • 2021-02-20 01:38

    First steps towards a solution:

    g1.1 <- g1 + theme_bw() + theme(legend.position="top")
    g2.1 <- g2 + theme_bw() + theme(panel.grid=element_blank()) +
        theme(panel.background = element_rect(fill = NA))
    plot(double_axis_graph(g1.1,g2.1))
    

    enter image description here

    Now we need to fix:

    • Weird grey background everywhere - edit: fixed it!
    • The legend

    And survive Hadley's wrath for doing what is fundamentally flawed.

    0 讨论(0)
  • I used some guys code to build my own here

    grid.newpage()
    
    # two plots
    p1 <- ggplot(fuel, aes(x=date, y=mpg)) + geom_line() + 
      theme_bw() + xlab("Date") + ylab("MPG (Black)")
    p2 <- ggplot(fuel, aes(x=date, y=avg.temp)) + 
      xlab("Date") + ylab("AVG Temp (Red)") +
      geom_line(colour="red") + theme_bw() + 
      theme(axis.title.y=element_text(angle = -90)) %+replace% 
      theme(panel.background = element_rect(fill = NA))
    
    # 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
    alab <- g2$grobs[[which(g2$layout$name=="ylab")]]
    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)
    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_cols(g, g2$widths[g2$layout[ia, ]$l], 
                         length(g$widths) - 1 )
    g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 2, pp$b)
    g <- gtable_add_grob(g, alab, pp$t, length(g$widths) - 1, pp$b)
    
    grid.draw(g)
    
    0 讨论(0)
提交回复
热议问题