[R] ggplot2: color histograms by quintile

John Kane jrkrideau at inbox.com
Thu Aug 1 16:43:29 CEST 2013


There is a problem with your example data set. "Series" has only one value so there is no faceting.  Also you refer to "trim.index$Rate" . Where is it coming from or is trim.index just another name for "thing"

John Kane
Kingston ON Canada


> -----Original Message-----
> From: david_txertudi at yahoo.fr
> Sent: Thu, 1 Aug 2013 01:23:37 +0100 (BST)
> To: r-help at r-project.org
> Subject: [R] ggplot2: color histograms by quintile
> 
> Hello,
> 
> I have a basic panel of histograms as follows, whose current colors don't
> matter:
> 
> 
> binsize=diff(range(thing$Rate))/64
> ggplot(thing, aes(x=Rate, fill=Series)) +
> geom_histogram(binwidth=binsize) + facet_grid(Series~.,scales="free")+
>   labs(fill="Index") +
>   xlab("Growth Rate (%)") +
>   theme(axis.title.y=element_blank(),legend.position=c(1,.64),
> legend.justification=c(1,1),strip.text.y = theme_blank()) +
>   scale_x_continuous(breaks=c(-10,-5,-2:10,15,20)) +
>   geom_vline(xintercept=0, linetype="dotted")
> rm(binsize)
> 
> 
> 
> What I would like to do is color each of the four histograms by its own
> deciles.  Essentially
> quantile(trim.index$Rate,c(0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1)) would give me
> the over-all deciles, but I would like them broken down by the elements
> of the Series variable, and then applied to the histograms as shading or
> coloring.  Does this makes sense?  I've dumped the first 100 rows of data
> below.
> 
> Thanks in advance for any help you're able to provide.
> 
> 
> David
> 
> 
> 
>  structure(list(Trials = 1:100, Year = c(2005L, 2008L, 2006L,
> 2007L, 2006L, 2004L, 2004L, 2003L, 2007L, 2005L, 2008L, 2006L,
> 2011L, 2005L, 2004L, 2003L, 2010L, 2002L, 2008L, 2005L, 2005L,
> 2004L, 2006L, 2011L, 2011L, 2008L, 2006L, 2004L, 2002L, 2003L,
> 2009L, 2004L, 2003L, 2011L, 2006L, 2002L, 2007L, 2010L, 2005L,
> 2008L, 2011L, 2008L, 2010L, 2005L, 2004L, 2009L, 2002L, 2008L,
> 2002L, 2006L, 2003L, 2007L, 2006L, 2006L, 2002L, 2002L, 2010L,
> 2008L, 2008L, 2003L, 2003L, 2009L, 2007L, 2009L, 2004L, 2005L,
> 2011L, 2010L, 2005L, 2008L, 2008L, 2008L, 2007L, 2008L, 2008L,
> 2007L, 2002L, 2009L, 2011L, 2002L, 2002L, 2006L, 2007L, 2007L,
> 2002L, 2009L, 2007L, 2003L, 2010L, 2010L, 2009L, 2003L, 2010L,
> 2003L, 2007L, 2006L, 2010L, 2005L, 2004L, 2010L), Month = c(12L,
> 4L, 5L, 3L, 9L, 12L, 4L, 3L, 6L, 10L, 6L, 11L, 1L, 6L, 9L, 10L,
> 5L, 3L, 11L, 10L, 2L, 8L, 9L, 7L, 8L, 8L, 7L, 1L, 9L, 1L, 11L,
> 3L, 12L, 1L, 6L, 7L, 6L, 8L, 12L, 8L, 11L, 11L, 5L, 7L, 2L, 6L,
> 9L, 9L, 11L, 6L, 11L, 5L, 5L, 3L, 10L, 6L, 7L, 8L, 9L, 2L, 3L,
> 11L, 8L, 4L, 12L, 6L, 10L, 10L, 12L, 9L, 4L, 12L, 12L, 12L, 6L,
> 6L, 11L, 1L, 5L, 6L, 2L, 4L, 7L, 10L, 12L, 4L, 5L, 8L, 7L, 2L,
> 6L, 10L, 10L, 10L, 10L, 2L, 6L, 6L, 9L, 9L), Core.CPI.Weighting = c(2L,
> 3L, 2L, 5L, 1L, 5L, 4L, 5L, 4L, 1L, 3L, 4L, 5L, 2L, 4L, 5L, 1L,
> 5L, 1L, 3L, 2L, 1L, 5L, 2L, 1L, 2L, 5L, 5L, 4L, 2L, 4L, 4L, 5L,
> 5L, 2L, 5L, 3L, 4L, 5L, 1L, 2L, 2L, 5L, 3L, 2L, 2L, 5L, 3L, 2L,
> 4L, 2L, 4L, 1L, 3L, 1L, 4L, 1L, 3L, 1L, 1L, 4L, 2L, 3L, 2L, 2L,
> 5L, 4L, 4L, 3L, 4L, 2L, 5L, 2L, 5L, 1L, 2L, 5L, 5L, 5L, 2L, 5L,
> 3L, 3L, 1L, 5L, 2L, 2L, 2L, 1L, 3L, 5L, 3L, 4L, 3L, 3L, 1L, 1L,
> 2L, 2L, 3L), CPI.Food = c(0.023474768, 0.043433814, 0.029315923,
> 0.042208873, 0.035479323, 0.024429485, 0.028537661, 0.027623773,
> 0.045546671, 0.023973579, 0.045546671, 0.038421672, 0.037161108,
> 0.023102181, 0.032765694, 0.032962625, 0.008051879, 0.028741685,
> 0.053639179, 0.025192645, 0.025077433, 0.032806764, 0.023605006,
> 0.025644434, 0.029584922, 0.031756778, 0.032450724, 0.026035343,
> 0.020656969, 0.026035343, 0.010684754, 0.029551194, 0.02442531,
> 0.012348667, 0.030959528, 0.023781539, 0.045546671, 0.008345359,
> 0.024429485, 0.031756778, 0.034731773, 0.053639179, 0.008051879,
> 0.023005118, 0.030315091, 0.04149634, 0.019373857, 0.051078725,
> 0.022406708, 0.030959528, 0.022406708, 0.044396055, 0.023304811,
> 0.025196539, 0.020831987, 0.016599861, 0.008044572, 0.049349997,
> 0.026691689, 0.01612059, 0.015903088, 0.010684754, 0.033979886,
> 0.048496522, 0.024429485, 0.023102181, 0.033084021, 0.033084021,
> 0.024429485, 0.026691689, 0.048496522, 0.054246603, 0.039956669,
> 0.054246603, 0.045546671, 0.045546671, 0.018027105, 0.053917666,
> 0.034171337, 0.025416646, 0.029331567, 0.02412957, 0.032450724,
> 0.052641267, 0.017531941, 0.048496522, 0.044396055, 0.018367312,
> 0.008044572, 0.013896245, 0.04149634, 0.032962625, 0.009905593,
> 0.032962625, 0.052641267, 0.025077433, 0.023022986, 0.023102181,
> 0.032765694, 0.00916168), PPI.Farm = c(0.009730106, 0.204892729,
> 0.138453455, 0.210017271, 0.178801715, -0.017104315, 0.168632738,
> 0.157512456, 0.208907609, -0.007879949, 0.208907609, 0.187585976,
> 0.171910952, -0.0471555, 0.144318736, 0.111713247, -0.000515726,
> 3.019e-05, 0.120566043, -0.027737238, -0.021152479, 0.168890071,
> -0.020784628, 0.231482252, 0.050380553, -0.141247793, 0.16832412,
> 0.140014634, -0.04669922, 0.140014634, 0.095775695, 0.02684028,
> 0.142349938, 0.125929795, 0.154898078, -0.043965946, 0.208907609,
> 0.033632438, -0.017104315, -0.141247793, 0.215496099, 0.120566043,
> -0.000515726, -0.041130752, 0.041959105, -0.105460885, 0.070882919,
> 0.189171764, 0.122047713, 0.154898078, 0.122047713, 0.202255379,
> -0.048781928, -0.033167528, 0.099171658, 0.041330169, 0.015714896,
> 0.204083122, -0.154114835, -0.008444152, -0.007718043, 0.095775695,
> 0.170931281, -0.067437568, -0.017104315, -0.0471555, 0.228225921,
> 0.228225921, -0.017104315, -0.154114835, -0.067437568, 0.072354298,
> 0.198873427, 0.072354298, 0.208907609, 0.208907609, -0.033883688,
> 0.030715588, 0.077744759, -0.039610801, 0.005252783, -0.040063654,
> 0.16832412, 0.154822639, -0.025165038, -0.067437568, 0.202255379,
> 0.054003537, 0.015714896, 0.158704237, -0.105460885, 0.111713247,
> 0.077687811, 0.111713247, 0.154822639, -0.021152479, 0.221790488,
> -0.0471555, 0.144318736, 0.059385754), Apple.Price = c(0.471497656,
> 0.242136143, 0.383200413, 0.213311867, 0.19191663, -0.065728883,
> 0.057338195, 0.049315633, 0.059618994, 0.493034014, 0.059618994,
> 0.214308977, 0.128389921, 0.569834371, -0.018700663, -0.070699241,
> -0.086097884, 0.366987271, -0.015999442, -0.169993743, 0.061982899,
> 0.03018664, 0.508926099, 0.220469512, 0.091490786, -0.088522479,
> 0.214605583, 0.029232469, 0.415997543, 0.029232469, 0.060085553,
> -0.342823215, 0.021628518, 0.094459129, 0.264711677, 0.397421106,
> 0.059618994, 0.036456744, -0.065728883, -0.088522479, 0.177490964,
> -0.015999442, -0.086097884, 0.565661951, -0.303093619, 0.027658663,
> 0.080537871, 0.046029687, 0.020747752, 0.264711677, 0.020747752,
> 0.140097776, 0.398741769, 0.121494593, 0.028159196, 0.102148041,
> 0.013906457, 0.096319168, -0.076928899, 0.216825824, 0.178838741,
> 0.060085553, 0.192104843, -0.082574645, -0.065728883, 0.569834371,
> 0.21221404, 0.21221404, -0.065728883, -0.076928899, -0.082574645,
> -0.033260035, 0.219937548, -0.033260035, 0.059618994, 0.059618994,
> 0.328876262, -0.042174202, 0.062637456, 0.393870002, 0.326653773,
> 0.233934148, 0.214605583, 0.007283343, 0.282652174, -0.082574645,
> 0.140097776, 0.11993679, 0.013906457, 0.114055642, 0.027658663,
> -0.070699241, 0.037565275, -0.070699241, 0.007283343, 0.061982899,
> 0.199237823, 0.569834371, -0.018700663, 0.039288557),
> Core.CPI.Food.PPI.Farm = c(0.024904916,
> 0.027307361, 0.025950523, 0.026091188, 0.027348863, 0.021461033,
> 0.01702177, 0.016107413, 0.027082059, 0.024761836, 0.027820644,
> 0.025638428, 0.021064644, 0.022617079, 0.019341399, 0.019439882,
> 0.01198474, 0.026417315, 0.033332157, 0.021643021, 0.021965605,
> 0.021677386, 0.024613858, 0.016164976, 0.023527578, 0.021546199,
> 0.025258394, 0.015801737, 0.023677409, 0.017080937, 0.010182005,
> 0.021908278, 0.015984198, 0.010100443, 0.026056165, 0.024664,
> 0.027820644, 0.011338128, 0.021461033, 0.022680707, 0.021223117,
> 0.030793779, 0.013108415, 0.023115649, 0.022438149, 0.02498414,
> 0.016860881, 0.029457635, 0.016958003, 0.025511347, 0.016958003,
> 0.026776534, 0.022253236, 0.021781775, 0.017552143, 0.018210414,
> 0.011010205, 0.029011844, 0.020677107, 0.01963456, 0.019888024,
> 0.01023228, 0.025705077, 0.027246357, 0.021832089, 0.022547779,
> 0.018714812, 0.018714812, 0.02165893, 0.019173462, 0.027246357,
> 0.026879388, 0.027452488, 0.026879388, 0.030774982, 0.02892852,
> 0.022558932, 0.02609762, 0.022395021, 0.025348179, 0.026778407,
> 0.021813883, 0.025737883, 0.033385735, 0.021936543, 0.027246357,
> 0.028538486, 0.017533072, 0.011010205, 0.01070759, 0.022625255,
> 0.020341398, 0.010418465, 0.020341398, 0.029534629, 0.022311364,
> 0.015778567, 0.022617079, 0.020683829, 0.010898218), Series =
> c("Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI", "Core.CPI",
> "Core.CPI", "Core.CPI", "Core.CPI"), Rate = c(2.5381632, 2.3275748,
> 2.4828723, 2.3404907, 2.3283633, 2.0966291, 1.4718592, 1.418802,
> 2.3389137, 2.5155965, 2.3389137, 2.308178, 1.83819, 2.2455378,
> 1.665654, 1.7186092, 1.3951171, 2.602992, 2.3178646, 2.0755616,
> 2.0928329, 1.6112696, 2.4782, 1.3005156, 2.0498906, 1.8142672,
> 2.4059673, 1.4096136, 2.4281497, 1.4096136, 1.0081455, 2.0379694,
> 1.4577346, 0.9725739, 2.4421711, 2.4811077, 2.3389137, 1.1936682,
> 2.0966291, 1.8142672, 1.6720232, 2.3178646, 1.3951171, 2.3143282,
> 1.9812501, 1.9480074, 1.6442052, 2.4052363, 1.5141768, 2.4421711,
> 1.5141768, 2.325263, 2.1727449, 2.0928083, 1.591222, 1.8532525,
> 1.2493021, 2.3927305, 1.7669816, 2.1391545, 2.0685012, 1.0081455,
> 2.3636374, 2.0162969, 2.0966291, 2.2455378, 1.584097, 1.584097,
> 2.0966291, 1.7669816, 2.0162969, 2.2318185, 2.3284427, 2.2318185,
> 2.3389137, 2.3389137, 2.3314236, 2.1460945, 2.0432301, 2.5325357,
> 2.635288, 2.1234962, 2.4059673, 2.3757969, 2.2670643, 2.0162969,
> 2.325263, 1.7254992, 1.2493021, 0.9910426, 1.9480074, 1.7186092,
> 1.052104, 1.7186092, 2.3757969, 2.0928329, 1.2156358, 2.2455378,
> 1.665654, 1.1332352), id = 1:100), .Names = c("Trials", "Year",
> "Month", "Core.CPI.Weighting", "CPI.Food", "PPI.Farm", "Apple.Price",
> "Core.CPI.Food.PPI.Farm", "Series", "Rate", "id"), row.names = c(NA,
> 100L), class = "data.frame")
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list