0

I've created histograms (using ggplot2) for price distributions of securities' data and I would like to display them side-by-side in one window for comparison. I've queried how to do this and have landed on par(mfrow=c(n,m)) but for whatever reason, when I call the histograms I have created, they each occupy an individual window (rather than a grid) and each consecutive call replaces the previous histogram. For example (see code below):

par(mfrow=c(3,3))

ggplot(data=BR_Pricing_F_M, aes(x=BR_Pricing_F_M$BR_Z7_F_M))+
  geom_histogram(breaks=seq(BR_Z7_F_M_L-0.01, BR_Z7_F_M_H+0.01, by=0.01),
                 col="white",
                 fill="blue",
                 alpha= 1)+
  labs(title="Z7 1 Month Price Distribution", x="Price Levels", 
       y="Frequency") 

ggplot(data=BR_Pricing_F_Q, aes(x=BR_Pricing_F_Q$BR_Z7_F_Q))+
  geom_histogram(breaks=seq(BR_Z7_F_Q_L-0.01, BR_Z7_F_Q_H+0.01, by=0.01),
                 col="white",
                 fill="red",
                 alpha= 1)+
  labs(title="Z7 Last Quarter’s Price Distribution", x="Price Levels", 
       y="Frequency") 

The above code yields two separate charts, when, according to the 3x3 matrix I specified with my par call, I should be able to populate a 3x3 grid with my ggplot2 generated histograms.

I've also tried assigning the plots to their own variable:

BR_Pricing_F_M_Hist <-ggplot(data=BR_Pricing_F_M, aes(x=BR_Pricing_F_M$BR_Z7_F_M))+
  geom_histogram(breaks=seq(BR_Z7_F_M_L-0.01, BR_Z7_F_M_H+0.01, by=0.01),
                 col="white",
                 fill="blue",
                 alpha= 1)+
  labs(title="Z7 1 Month Price Distribution", x="Price Levels", 
       y="Frequency")

BR_Pricing_Z7_F_Q_Hist <- ggplot(data=BR_Pricing_F_Q, aes(x=BR_Pricing_F_Q$BR_Z7_F_Q))+
  geom_histogram(breaks=seq(BR_Z7_F_Q_L-0.01, BR_Z7_F_Q_H+0.01, by=0.01),
                 col="white",
                 fill="red",
                 alpha= 1)+
  labs(title="Z7 Last Quarter’s Price Distribution", x="Price Levels", 
       y="Frequency") 

par(mfrow=c(3,3))

BR_Pricing_F_M_Hist

BR_Pricing_F_Q_Hist

But the result is the same. Can anyone advise as to what I might be doing wrong?

Many thanks in advance!

Kuba_R
  • 11
  • 2
  • Read about cowplot package. – zx8754 Sep 25 '17 at 20:35
  • 1
    see also https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html#arranging-multiple-plots-on-a-page – baptiste Sep 25 '17 at 20:39
  • @baptiste Nice vignette! Thanks for sharing. – Henrik Sep 25 '17 at 20:46
  • See also https://cran.r-project.org/web/packages/lemon/vignettes/legends.html which includes `grid_arrange_shared_legend` which @baptiste refers to. – MrGumble Sep 26 '17 at 09:57
  • @baptiste, I am the author of the lemon package. It was based on revisions of the page you linked, that I found sjackman to be the first author of the function, but if I am mistaken, I will gladly correct it. – MrGumble Sep 26 '17 at 20:52
  • @MrGumble I vaguely remember being surprised when the reference from Rpubs was added, but maybe it had a different name elsewhere, or maybe they were indeed first to wrap the earlier code in a function. Doesn't matter in the end, it's fine. – baptiste Sep 26 '17 at 21:23
  • @baptiste Thanks, I'll rephrase it a bit next time I look at it. But I've packed it so its easier to use, added top and left margins, and now non-ggplot2 objects are sent directly on to arrangeGrob. – MrGumble Sep 26 '17 at 21:50
  • @baptiste et al... a couple follow up questions... a) is there an easy way to overlay a cumulative frequency graph on a histogram using ggplot2? b) how do I get every "price" to show up on the x-axis rather than every 5 prices (bins are ticked every 5 prices currently). c) is there a way to have a tick on the y-axis which corresponds to the exact height of each bar in the histogram? – Kuba_R Sep 27 '17 at 23:46

0 Answers0