Can density be >10?

17 views
Skip to first unread message

Bogaso

unread,
Dec 25, 2009, 1:03:24 AM12/25/09
to ggplot2
Hi,

Here I have following dataset :

> dat111
[1] -0.0700560096 0.0292167911 -0.0293505649 0.0030089374
-0.0366910215 0.0502314541 -0.1009953708
[8] 0.0939554230 0.0231295909 0.0461035095 -0.0026240427
0.1164344115 0.0849477570 0.1047018371
[15] 0.1245621214 -0.0410198887 0.0791613984 -0.0076546187
0.0358229088 0.0427989718 0.0418144824
[22] -0.0485601222 -0.0229328444 -0.0582568554 0.0323019358
-0.1242318110 -0.0070094101 0.0453381849
[29] -0.0140461848 0.0884171012 -0.0912742791 0.0424582758
0.1058372524 0.0285815776 0.0008203119
[36] -0.0117625040 0.0975290842 0.0209006651 0.0383024471
-0.0408462456 -0.0845307010 0.0598814874
[43] -0.0348634543 0.0149196477 -0.0724593403 0.1677959017
-0.0721297761 -0.0496290380 0.1605989702
[50] -0.0069297266


Now I wanted to plot a histogram where y axis represents "Density"
instead "Count". Here is my code :

ggplot(data.frame(dat111), aes(x=dat111)) +
geom_histogram(aes(y=..density.., fill=..count..))

However I am surprised to see the range of y-axis. Shouldn't it be
within the range [0, 1]? Am I missing something?

Mike Lawrence

unread,
Dec 26, 2009, 11:10:48 AM12/26/09
to Bogaso, ggplot2
This is a common question over at R-help. From the help on hist:

density: values f^(x[i]), as estimated density values. If
all(diff(breaks) == 1), they are the relative frequencies counts/n and
in general satisfy sum[i; f^(x[i]) (b[i+1]-b[i])] = 1, where b[i] =
breaks[i].

So, only if the breaks are of size 1 apart will all density values be
less than or equal to 1. Alternatively, sum(density*bin_width) will
always equal 1, so if you want to plot a histogram such that its scale
matches up with some theoretical probability density function, then
plot density*bin_width.

Mike

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
>
>

--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

Bogaso

unread,
Dec 26, 2009, 12:38:42 PM12/26/09
to ggplot2
Thanks Mike for your reply. Here perhaps I do not want to a
"theoretical pdf". I actually want to get a histogram_like plot
wherein y-axis represents the proportion of observations within user-
defined range. Say for example :

> intervals <- seq(range(dat111)[1], range(dat111)[2], length.out=50); intervals
[1] -0.124231811 -0.118272062 -0.112312313 -0.106352563 -0.100392814
-0.094433065 -0.088473316 -0.082513566
[9] -0.076553817 -0.070594068 -0.064634319 -0.058674569 -0.052714820
-0.046755071 -0.040795322 -0.034835572
[17] -0.028875823 -0.022916074 -0.016956325 -0.010996575 -0.005036826
0.000922923 0.006882672 0.012842421
[25] 0.018802171 0.024761920 0.030721669 0.036681418 0.042641168
0.048600917 0.054560666 0.060520415
[33] 0.066480165 0.072439914 0.078399663 0.084359412 0.090319162
0.096278911 0.102238660 0.108198409
[41] 0.114158159 0.120117908 0.126077657 0.132037406 0.137997156
0.143956905 0.149916654 0.155876403
[49] 0.161836152 0.167795902

I need a histogram-like-plot representing number of observations
within range (intervals[1], intervals[2]), (intervals[2], intervals
[3]).................

How I can get those proportion of numbers and how to pass them into
geom_histogram() ??

Is there any direct way to do that?

Thanks,

Bogaso

unread,
Dec 26, 2009, 1:05:24 PM12/26/09
to ggplot2
I am tried following code :

ggplot() + stat_bin(aes(x=dat111, fill=..count..), binwidth=0.01)

But again I stuck on how to tell y-axis to display the "Proportion"
NOT "count". Any suggestion please ?

Thanks,

hadley wickham

unread,
Dec 26, 2009, 1:09:21 PM12/26/09
to Bogaso, ggplot2
Try this:

prop <- function(x) x / sum(x)
ggplot() + stat_bin(aes(x=dat111, fill= prop(..count..)), binwidth=0.01)

Hadley

--
http://had.co.nz/

Gary Collins

unread,
Dec 26, 2009, 1:13:53 PM12/26/09
to Bogaso, ggplot2
I think this is what you want. To get a proportion you need to divide
the count by the total count, i.e. sum(..count..)

dat111 <- rnorm(1000)
p <- ggplot(data.frame(dat111), aes(x=dat111))
p + stat_bin(aes(y=..count../sum(..count..)), binwidth=0.5)

HTH

Gary

Reply all
Reply to author
Forward
0 new messages