0

I am trying to arrange graphs in a 2x2 grid layout with year on the X axis and Cost on the Y axis. When I format by individual graph my formatting, namely the dollar sign is there, but when I arrange it in the grid layout the dollar sign and commas disappear.

Code below:

rat<-qplot(tallc$Group.1,COSTS,data=tallc, geom=c("point","smooth"), 
method="lm",
           xlab= "YEAR",
           ylab= "COST",
           scale_x_continuous(limits = c(1999,2012)),
           main= "Total Costs from 1999 to 2012")      
rat1 <- rat+scale_x_continuous(breaks=c(1999,2000,2001,2002,2003,2004,2005,2006,
           2007,2008,2009,2010,2011,2012))
rat1 + scale_y_continuous(labels=dollar)

the moment I use

pushViewport(viewport(layout = grid.layout(2, 2)))
print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(b, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(c, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(rat1, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))

Dollar sign disappears.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
URA
  • 1
  • 2
    Your data is not reproducible. We cannot see the `tallc` object. Please include data or fake data that looks like the real data. See [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for guidance. – SlowLearner Aug 16 '13 at 21:12

1 Answers1

0

Here are a couple of missing items (and the answer):

 tallc<-data.frame(COSTS=rnorm(9), Group=1999:2007)  # fake data
 require(ggplot2) # obvious
 require(scales) # not so obvious
 # first part of code
 ##### The missing step:
 rat1 <- rat1 + scale_y_continuous(labels=dollar)
 require(grid)
 # Second part: grid code
 # success with plotting dollar-scale in lower right corner
 # (despite errors from the missing a,b, and c objects.)

You never assigned the result of adding the scale_y_continuous with the "dollar sign" to rat1, so rat1 remained in the same state it was in after adding only the x-scale.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • This worked was not storing it as a separate object. DWin would you know how to standardize the y scales. I have two separate plots with two different x axis, one set of values are in calander years and the other are in fiscal years, I am trying to set the y breaks (units in dollars) to be the same for comparison of the two graphs. Attached is the code, but the y axises are different tried using breaks, lengths, and ylim option but it did not work. Code below. – URA Aug 18 '13 at 22:31
  • code for plot 1 rat<-qplot(tallc$Group.1,COSTS,data=tallc, geom=c("point","smooth"), method="lm", xlab= "CYYEAR", ylab= "COST", main= "Total Reported Costs") rat1<-rat+scale_x_continuous(breaks=c(1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012)) rat1 + scale_y_continuous(labels=dollar) ratf<-ratf + scale_y_continuous(labels=dollar) – URA Aug 18 '13 at 22:32
  • code for plot 2 brain<-qplot(tallf$Group.1,COSTS,data=tallf, geom=c("point ","smooth"), method="lm", xlab= "FYEAR", ylab= "COST", main= "Total Reported Costs") brain1<-brain+scale_x_continuous(breaks=c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012)) brainf<-brain1 + scale_y_continuous(labels=dollar) brainf – URA Aug 18 '13 at 22:38
  • issue here is that the y axis is different for both plots and I want it to be the same – URA Aug 18 '13 at 22:39
  • tallc-sample date Group.1 COSTS EVENTS 1 1999 166304548 652 2 2000 822939665 730 3 2001 472072396 566 – URA Aug 18 '13 at 22:42
  • Basically trying to set the breaks of the two y axis to be the same. – URA Aug 18 '13 at 22:43
  • If you use `+ylim( range(c(tallf$COSTS, tallc$COSTS)))` to create new plot objects then the "breaks" should end being the same. You should NOT use the comments to a question to add new code for a new question. (I also think you should have asked a new question, since this really has nothing to do with your first question.) – IRTFM Aug 19 '13 at 15:22