Loops, dataframes and ggplot

前端 未结 1 483
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 17:31

I would like to display multiple plots on the same page using ggplot, and the multiplot function described here: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page

相关标签:
1条回答
  • 2021-01-16 18:25

    Below you find an alternative: using the melt function from reshape2 and then faceting with facet_wrap.

    require(ggplot2)
    require(reshape2)
    data.melt <- melt(rawdata1, id.var='Period')
    ggplot(data.melt, aes(Period, value)) + 
      geom_line() + 
      facet_wrap(~variable, scales='free_y')
    

    If you want to use multiplot instead, you could do the following:

    out <- lapply(names(rawdata1)[-1], 
                   function(index) ggplot(rawdata1) + 
                     geom_line(aes_string(x = 'Period', y = index)) + 
                     ggtitle(label = index))
    multiplot(plotlist = out, cols = 2)
    
    0 讨论(0)
提交回复
热议问题