[R] Help with continuous color plot

matt at considine.net matt at considine.net
Wed Sep 24 17:26:10 CEST 2014


Hi,
I have a matrix of data, with the rows representing observations and the 
columns representing various values that the observation can take on.  
In other words, each row can be thought of as a sampling of the density 
function/histogram associated with the range of values for that 
observation.

I'd like to graph these with a shaded color, rather than as lines.  So a 
given observation would have the darkest shade at the mean and the 
shading would lighten for values that approached the tails.  In a sense 
this is like a ribbon chart, but where there are many confidence bands.

I think the example near the bottom of this page
   http://bconnelly.net/2013/10/creating-colorblind-friendly-figures/
starts to get at what I want.  But when I tried to get a ribbon, I get 
an error message saying that "Error: Aesthetics can not vary with a 
ribbon"

Can anyone point me to an example that accomplishes my task, or give me 
some ideas as to how to code this?

Below is a reproducible dataset and the code I ran that generated the 
above error.  And apologies in advance if I have overlooked some obvious 
source - I'm not exactly sure what keywords to search for.

Regards,
Matt

testdataset <- structure(c(0.703482475602795, 0.708141442616021, 
0.696373713631662,
                            0.670284015871304, 0.675183812793659, 
0.690440437259122, 0.717483375152826,
                            0.775328205198994, 0.848374059782512, 
0.869939471712489, 0.86329313061477,
                            0.842138830353923, 0.819853961383293, 
0.808038546509378, 0.826626282345039,
                            0.855428819162732, 0.873943618483253, 
0.906412218904192, 0.95345525957727,
                            0.941481792397259, 0.923791753474186, 
0.909206164221341, 0.847283523824235,
                            0.774333551860785, 0.723440114819687, 
0.653247411286407, 0.585889004137383,
                            0.516531935718585, 0.458855598305008, 
0.422596378188962, 0.385800210249005,
                            0.363663809831211, 0.703482475602795, 
0.708055808109959, 0.696379276680681,
                            0.686643131558789, 0.702628930558265, 
0.736010723583024, 0.790795207667811,
                            0.843997296035071, 0.872447231982615, 
0.876357159885425, 0.852095141662599,
                            0.815122741092172, 0.759163100114952, 
0.737079598996168, 0.755626127703219,
                            0.76375495269533, 0.757290640161052, 
0.754301244147121, 0.738872719902144,
                            0.712590028244082, 0.707690675037336, 
0.707234385372842, 0.708720518303698,
                            0.723271948541464, 0.738173079905318, 
0.772161522113349, 0.776237486574842,
                            0.775666977944939, 0.764229462885737, 
0.758916671383124, 0.742887393474484,
                            0.741362343479079, 0.703482475602795, 
0.70722192044612, 0.694934601341247,
                            0.675623005679584, 0.67355293987199, 
0.67514195581405, 0.701338223542176,
                            0.770084545123592, 0.826615555391194, 
0.815331595124185, 0.801265437257298,
                            0.768736104243487, 0.698903427959817, 
0.654393072393584, 0.646507677289504,
                            0.606308031283892, 0.574521529688064, 
0.550931914275617, 0.518538683619987,
                            0.495773346159491, 0.482784058725618, 
0.473031502762785, 0.462940836756943,
                            0.455472910452526, 0.457374752189383, 
0.468449683385787, 0.469177346159405,
                            0.47981744053419, 0.500517935694715, 
0.521161553352487, 0.538278248678118,
                            0.545834896270532, 0.703482475602795, 
0.707475643569319, 0.695699528962731,
                            0.695460540915422, 0.705063229294573, 
0.694190083263775, 0.676451221936696,
                            0.661139999162065, 0.627150885842318, 
0.592467979293877, 0.556197511727567,
                            0.524883713023224, 0.484571801496662, 
0.427784904562, 0.370137413134906,
                            0.331233866457343, 0.292181528642806, 
0.265504971226103, 0.239129968439056,
                            0.21258454640671, 0.184521419432522, 
0.160633576032345, 0.135729972994914,
                            0.115111431576686, 0.0933784744252792, 
0.0672765522562478, 0.0397992726679255,
                            0.0118179662548541, NA, NA, NA, NA, 
0.703482475602795, 0.70791132542366,
                            0.696508162877812, 0.672357035115831, 
0.679831378223931, 0.702075998432084,
                            0.736057349706643, 0.759252979404642, 
0.739391321260192, 0.706608353324493,
                            0.653481111693474, 0.607986236497692, 
0.600942686427268, 0.602450590412635,
                            0.594096281507138, 0.598414292518021, 
0.570859444977738, 0.50462737404968,
                            0.441225469913529, 0.37010584373766, 
0.299554326292306, 0.250957120974181,
                            0.231147047662909, 0.218081437060998, 
0.209354124252359, 0.212236940966109,
                            0.213898409405384, 0.200693009702681, 
0.189880443626695, 0.175663436717225,
                            0.160910269517771, 0.14423774751828, 
0.703482475602795, 0.707648742091919,
                            0.696092540716741, 0.656540853310246, 
0.6051367218461, 0.591695064299013,
                            0.596015810648035, 0.603800534715597, 
0.630728546677123, 0.658732672149451,
                            0.660216960664134, 0.675188962779905, 
0.680940355728037, 0.677371529164165,
                            0.678862966955137, 0.706307948099043, 
0.72103331978575, 0.71201796002067,
                            0.695266699409018, 0.685297231486624, 
0.661576951062559, 0.643301885602357,
                            0.619830526453808, 0.608129873046412, 
0.594283830084397, 0.562317115717112,
                            0.530350595536459, 0.506782526041746, 
0.485311767855001, 0.473335687828819,
                            0.47767850215973, 0.47780651839627, 
0.703482475602795, 0.708970958906805,
                            0.697551150784402, 0.661080016538957, 
0.61232038566617, 0.587787379274781,
                            0.593431699246192, 0.585207322444761, 
0.568559116457046, 0.548048255621954,
                            0.530114701694855, 0.534118004338362, 
0.551070523651917, 0.575923824458594,
                            0.601811119913484, 0.599679943614892, 
0.571040665848032, 0.537593347467274,
                            0.517448133546794, 0.509401266935977, 
0.506839184709813, 0.513795796376072,
                            0.538416419444161, 0.54431580354862, 
0.533416582151419, 0.536830327067308,
                            0.536462655629334, 0.513629165635279, 
0.478995218355945, 0.438690505845544,
                            0.382567436102273, 0.34023179757446, 
0.703482475602795, 0.708469004078862,
                            0.697978508894162, 0.676172179276398, 
0.653999136795877, 0.621901773418305,
                            0.611086726778221, 0.593857278888487, 
0.59772372401201, 0.615523307201519,
                            0.639972290824288, 0.642201950188424, 
0.640462288885887, 0.599634050862654,
                            0.556922658075191, 0.51984992524725, 
0.500551878868105, 0.481116358620079,
                            0.46280476578578, 0.439079916934238, 
0.426481043460678, 0.4046073777228,
                            0.384894885963074, 0.387418227322783, 
0.397735327855843, 0.382421337902373,
                            0.364609477937235, 0.351141834403433, 
0.326023299419572, 0.298998310511409,
                            0.279388702747555, 0.265729633371279, 
0.703482475602795, 0.708084110634261,
                            0.695308105608089, 0.680512620916581, 
0.674409687648891, 0.64479397663385,
                            0.611185461407328, 0.574633943767944, 
0.531967082066137, 0.509238294683539,
                            0.539380037981121, 0.605934156503129, 
0.67139250700667, 0.692111261722321,
                            0.681033453581129, 0.649259774238939, 
0.61340010828123, 0.601868728090964,
                            0.626623248125788, 0.630474601620122, 
0.632877115180945, 0.622655315896617,
                            0.602457206226614, 0.577755838513823, 
0.575613491077747, 0.56693811567409,
                            0.538199408312755, 0.508731518312269, 
0.488361667141151, 0.462965384850233,
                            0.438776128537772, 0.433205440295598, 
0.703482475602795, 0.708955081210283,
                            0.696337276771488, 0.675657944024044, 
0.682199731001736, 0.713302322144747,
                            0.780735831443758, 0.836330924875064, 
0.920042864888473, 0.993749005071184,
                            1.09383447176433, 1.13919188883409, 
1.17120759930688, 1.20149479327377,
                            1.21627978424244, 1.16585978639785, 
1.11758399725948, 1.04238685207932,
                            0.937208207066276, 0.862384770704829, 
0.774099878399983, 0.701556273005073,
                            0.670583202198987, 0.686968300073611, 
0.732378201300416, 0.820191766252091,
                            0.855392362283012, 0.845365464014818, 
0.803317719342587, 0.757849795095728,
                            0.711624608771727, 0.674398052370244), .Dim = 
c(32L, 10L))
matplot(testdataset, type = 'l', las = 1, xlab = 'x values',
         ylab = 'y values', main = 'title - testdataset')
library(ggplot2,RColorBrewer,reshape2)
testdataset[ is.na(testdataset) ] <- 0
testdataset2 <- melt(testdataset)
ggplot(testdataset2, aes(x=Var1,y=factor(value))) +
   stat_density(aes(fill=..density..), position="identity") +
   scale_fill_gradientn(colours=brewer.pal(n=8, name="PuBuGn"))



More information about the R-help mailing list