Different y-Axis Labels facet_grid and sizes

后端 未结 1 528
栀梦
栀梦 2020-12-18 06:54

I\'am stuck with the following problem:

I want to display different characteristics of a timeseries in one plot (with multiple subplots). To align the chart areas an

1条回答
  •  天涯浪人
    2020-12-18 07:28

    You can change the grobs of your plot to do this

    library(ggplot2)
    library(grid)
    library(scales)
    
    #plot
    p <- ggplot(df_melt, aes(x=date, y=value)) + 
                geom_line() + 
                facet_grid(variable~., scales = "free")
    
    # change facet heights
    g1 <- ggplotGrob(p) 
    g1$heights[[3]] <- unit(2, "null") 
    
    # change labels - create second plot with percentage labels (nonsese % here)
    p2 <- ggplot(df_melt, aes(x=date, y=value)) + 
                  geom_line() + 
                  facet_grid(variable~., scales = "free") +
                  scale_y_continuous(labels = percent_format())
    
    g2 <- ggplotGrob(p2) 
    
    #Tweak axis - overwrite one facet
    g1[["grobs"]][[2]] <- g2[["grobs"]][[2]]
    
    grid.newpage()
    grid.draw(g1)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题