[R] Howto Superimpose Multiple Density Curves Into One Plot

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Sep 2 15:26:49 CEST 2009


I'm using the latest version: 0.8.3 on R 2.9.2 


----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: Gundala Viswanath [mailto:gundalav at gmail.com] 
Verzonden: woensdag 2 september 2009 15:21
Aan: ONKELINX, Thierry
CC: r-help at stat.math.ethz.ch
Onderwerp: Re: [R] Howto Superimpose Multiple Density Curves Into One Plot

Which version of ggplot2 you use?
Mine is:
Version:       0.7
Date:          2008-10-03

Somehow it can't recognize facet_wrap()

> ggplot(subset(dat, V2 <= 2), aes(x = V1)) + geom_density() + 
> facet_wrap(~V2)
Error: could not find function "facet_wrap"
>

- G.V.

On Wed, Sep 2, 2009 at 10:01 PM, ONKELINX, Thierry<Thierry.ONKELINX at inbo.be> wrote:
> This code work without errors for me.
>
> library(ggplot2)
> dat <- read.table("http://dpaste.com/88561/plain/")
> ggplot(dat, aes(x = V1, colour = factor(V2))) + geom_density() 
> ggplot(dat, aes(x = V1, fill = factor(V2))) + geom_density(alpha = 
> 0.5)
>
> Have a look at table(dat$V2). Some classes have only 1 or 2 values, 
> which in not enough to estimate the density. Therefore you get an 
> error with
>
> ggplot(dat, aes(x = V1)) + geom_density() + facet_wrap(~V2)
>
> Subsetting the data solves the problem
>
> ggplot(subset(dat, V2 <= 2), aes(x = V1)) + geom_density() + 
> facet_wrap(~V2)
>
> HTH,
>
> Thierry
>
>
> ----------------------------------------------------------------------
> ------
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature 
> and Forest Cel biometrie, methodologie en kwaliteitszorg / Section 
> biometrics, methodology and quality assurance Gaverstraat 4 9500 
> Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be 
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> -----Oorspronkelijk bericht-----
> Van: Gundala Viswanath [mailto:gundalav at gmail.com]
> Verzonden: woensdag 2 september 2009 14:54
> Aan: ONKELINX, Thierry
> CC: r-help at stat.math.ethz.ch
> Onderwerp: Re: [R] Howto Superimpose Multiple Density Curves Into One 
> Plot
>
>> str(dat)
> 'data.frame':   200 obs. of  2 variables:
>  $ V1: num  0.98 0.19 1.09 0.21 0.26 0.98 0.31 0.88 0.23 0.2 ...
>  $ V2: int  1 0 1 0 0 1 0 1 0 0 ...
>> summary(dat)
>       V1               V2
>  Min.   :0.0000   Min.   :0.000
>  1st Qu.:0.1600   1st Qu.:0.000
>  Median :0.2950   Median :0.000
>  Mean   :0.7108   Mean   :0.635
>  3rd Qu.:1.0600   3rd Qu.:1.000
>  Max.   :5.4400   Max.   :5.000
>
>
> - G.V.
>
> On Wed, Sep 2, 2009 at 9:50 PM, ONKELINX, Thierry<Thierry.ONKELINX at inbo.be> wrote:
>> My orginal code should work with those colnames. Note that you must not use "variable.name" but just variable.name.
>>
>> I guess that somethings wrong with your dataframe. What does str(dat) and summary(dat) gives?
>>
>> Thierry
>>
>>
>> ---------------------------------------------------------------------
>> -
>> ------
>> ir. Thierry Onkelinx
>> Instituut voor natuur- en bosonderzoek / Research Institute for 
>> Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / 
>> Section biometrics, methodology and quality assurance Gaverstraat 4 
>> 9500 Geraardsbergen Belgium tel. + 32 54/436 185 
>> Thierry.Onkelinx at inbo.be www.inbo.be
>>
>> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
>> ~ Sir Ronald Aylmer Fisher
>>
>> The plural of anecdote is not data.
>> ~ Roger Brinner
>>
>> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
>> ~ John Tukey
>>
>> -----Oorspronkelijk bericht-----
>> Van: Gundala Viswanath [mailto:gundalav at gmail.com]
>> Verzonden: woensdag 2 september 2009 14:47
>> Aan: ONKELINX, Thierry
>> CC: r-help at stat.math.ethz.ch
>> Onderwerp: Re: [R] Howto Superimpose Multiple Density Curves Into One 
>> Plot
>>
>> Hi Thierry,
>>
>> I am sorry for coming back to you.
>> Maybe I misunderstood you, but I got this:
>>
>>> colnames(dat)
>> [1] "V1" "V2"
>>> ggplot(dat, aes(x = "V1", colour = factor("V2"))) + geom_density()
>> Error in data.frame(..., check.names = FALSE) :
>>  arguments imply differing number of rows: 1, 200
>>
>>> ggplot(dat, aes(x = V1, colour = factor(V2))) + geom_density()
>> Error in density.default(data$x, adjust = adjust, kernel = kernel, weight = data$weight,  :
>>  need at least 2 points to select a bandwidth automatically
>>
>>
>> - G.V
>>
>> On Wed, Sep 2, 2009 at 9:35 PM, ONKELINX, Thierry<Thierry.ONKELINX at inbo.be> wrote:
>>> It looks like the data has other columnnames than the samplecode you provided.
>>>
>>> Use colnames(dat) to get the columnnames. Replace V1 with the columnname of the values and V2 with the column name of the ID's.
>>>
>>> HTH,
>>>
>>> Thierry
>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> -
>>> ------
>>> ir. Thierry Onkelinx
>>> Instituut voor natuur- en bosonderzoek / Research Institute for 
>>> Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / 
>>> Section biometrics, methodology and quality assurance Gaverstraat 4 
>>> 9500 Geraardsbergen Belgium tel. + 32 54/436 185 
>>> Thierry.Onkelinx at inbo.be www.inbo.be
>>>
>>> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
>>> ~ Sir Ronald Aylmer Fisher
>>>
>>> The plural of anecdote is not data.
>>> ~ Roger Brinner
>>>
>>> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
>>> ~ John Tukey
>>>
>>> -----Oorspronkelijk bericht-----
>>> Van: Gundala Viswanath [mailto:gundalav at gmail.com]
>>> Verzonden: woensdag 2 september 2009 14:33
>>> Aan: ONKELINX, Thierry
>>> CC: r-help at stat.math.ethz.ch
>>> Onderwerp: Re: [R] Howto Superimpose Multiple Density Curves Into 
>>> One Plot
>>>
>>> Hi Thierry,
>>>
>>> Thanks for the reply. I tried this:
>>>> dat <-   read.table("http://dpaste.com/88561/plain/")
>>>
>>> But I got such error:
>>>
>>>> ggplot(dat, aes(x = V1, colour = factor(V2))) + geom_density()
>>> Error in density.default(data$x, adjust = adjust, kernel = kernel, weight = data$weight,  :
>>>  need at least 2 points to select a bandwidth automatically
>>>> ggplot(dat, aes(x = V1, fill = factor(V2))) + geom_density(alpha =
>>>> 0.2)
>>> Error in density.default(data$x, adjust = adjust, kernel = kernel, weight = data$weight,  :
>>>  need at least 2 points to select a bandwidth automatically
>>>> ggplot(dat, aes(x = V1)) + geom_density() + facet_wrap(~V2)
>>> Error: could not find function "facet_wrap"
>>>>
>>>> ggplot(dat, aes(x = V1, fill = factor(V2))) + geom_density(alpha =
>>>> 0.2)
>>> Error in density.default(data$x, adjust = adjust, kernel = kernel, weight = data$weight,  :
>>>  need at least 2 points to select a bandwidth automatically
>>>> ggplot(dat, aes(x = V1)) + geom_density() + facet_wrap(~V2)
>>> Error: could not find function "facet_wrap"
>>>
>>> Maybe I miss something? What could possibly go wrong?
>>>
>>> - Edward
>>>
>>>
>>> On Wed, Sep 2, 2009 at 8:30 PM, ONKELINX, Thierry<Thierry.ONKELINX at inbo.be> wrote:
>>>> Have a look at the ggplot2 package.
>>>>
>>>> library(ggplot2)
>>>> dat <- read.table("mydat.txt")
>>>> ggplot(dat, aes(x = V1, colour = factor(V2))) + geom_density() #or 
>>>> a few alternatives ggplot(dat, aes(x = V1, fill = factor(V2))) + 
>>>> geom_density(alpha = 0.2) ggplot(dat, aes(x = V1)) + geom_density()
>>>> +
>>>> facet_wrap(~V2)
>>>>
>>>> HTH,
>>>>
>>>> Thierry
>>>>
>>>>
>>>> -------------------------------------------------------------------
>>>> -
>>>> -
>>>> -
>>>> --
>>>> ----
>>>> ir. Thierry Onkelinx
>>>> Instituut voor natuur- en bosonderzoek / Research Institute for 
>>>> Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / 
>>>> Section biometrics, methodology and quality assurance Gaverstraat 4 
>>>> 9500 Geraardsbergen Belgium tel. + 32 54/436 185 
>>>> Thierry.Onkelinx at inbo.be www.inbo.be
>>>>
>>>> To call in the statistician after the experiment is done may be no 
>>>> more than asking him to perform a post-mortem examination: he may 
>>>> be able to say what the experiment died of.
>>>> ~ Sir Ronald Aylmer Fisher
>>>>
>>>> The plural of anecdote is not data.
>>>> ~ Roger Brinner
>>>>
>>>> The combination of some data and an aching desire for an answer 
>>>> does not ensure that a reasonable answer can be extracted from a 
>>>> given body of data.
>>>> ~ John Tukey
>>>>
>>>> -----Oorspronkelijk bericht-----
>>>> Van: r-help-bounces at r-project.org
>>>> [mailto:r-help-bounces at r-project.org]
>>>> Namens Gundala Viswanath
>>>> Verzonden: woensdag 2 september 2009 12:10
>>>> Aan: r-help at stat.math.ethz.ch
>>>> Onderwerp: [R] Howto Superimpose Multiple Density Curves Into One 
>>>> Plot
>>>>
>>>> I have a data that looks like this:
>>>> http://dpaste.com/88561/plain/
>>>>
>>>> And I intend to create multiple density curve into one plot, where 
>>>> each curve correspond to the unique ID.
>>>>
>>>> I tried to use "sm" package, with this code, but without success.
>>>>
>>>> __BEGIN__
>>>> library(sm)
>>>> dat <- read.table("mydat.txt");
>>>> plotfn <- ("~/Desktop/flowgram_superimposed.pdf");
>>>> pdf(plotfn);
>>>>
>>>> sm.density.compare(dat$V1,dat$V2, xlab = "Flow Signal") colfill <- 
>>>> c(2:10); legend(locator(1), levels(dat$V2), fill=colfill)
>>>>
>>>> dev.off();
>>>> __END__
>>>>
>>>> Please advice what's the right way to do it or if  there is 
>>>> alternative way to do it?
>>>> I am trying to get this kind of figure:
>>>> http://img524.imageshack.us/img524/2736/testl.png
>>>>
>>>>
>>>> - G.V.
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>> Druk dit bericht a.u.b. niet onnodig af.
>>>> Please do not print this message unnecessarily.
>>>>
>>>> Dit bericht en eventuele bijlagen geven enkel de visie van de 
>>>> schrijver weer en binden het INBO onder geen enkel beding, zolang 
>>>> dit bericht niet bevestigd is door een geldig ondertekend document.
>>>> The views expressed in  this message and any annex are purely those 
>>>> of the writer and may not be regarded as stating an official 
>>>> position of INBO, as long as the message is not confirmed by a duly signed document.
>>>>
>>>
>>> Druk dit bericht a.u.b. niet onnodig af.
>>> Please do not print this message unnecessarily.
>>>
>>> Dit bericht en eventuele bijlagen geven enkel de visie van de 
>>> schrijver weer en binden het INBO onder geen enkel beding, zolang 
>>> dit bericht niet bevestigd is door een geldig ondertekend document. 
>>> The views expressed in  this message and any annex are purely those 
>>> of the writer and may not be regarded as stating an official 
>>> position of INBO, as long as the message is not confirmed by a duly signed document.
>>>
>>
>> Druk dit bericht a.u.b. niet onnodig af.
>> Please do not print this message unnecessarily.
>>
>> Dit bericht en eventuele bijlagen geven enkel de visie van de 
>> schrijver weer en binden het INBO onder geen enkel beding, zolang dit 
>> bericht niet bevestigd is door een geldig ondertekend document. The 
>> views expressed in  this message and any annex are purely those of 
>> the writer and may not be regarded as stating an official position of 
>> INBO, as long as the message is not confirmed by a duly signed document.
>>
>
> Druk dit bericht a.u.b. niet onnodig af.
> Please do not print this message unnecessarily.
>
> Dit bericht en eventuele bijlagen geven enkel de visie van de 
> schrijver weer en binden het INBO onder geen enkel beding, zolang dit 
> bericht niet bevestigd is door een geldig ondertekend document. The 
> views expressed in  this message and any annex are purely those of the 
> writer and may not be regarded as stating an official position of 
> INBO, as long as the message is not confirmed by a duly signed document.
>

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




More information about the R-help mailing list