[R] barplot with a probability density curve

S Ellison S.Ellison at lgc.co.uk
Mon Mar 15 03:07:50 CET 2010


You have three problems:
i) Barplot's plot locations are not at 100-700 in your picture.
ii) density applied to your bar hights will produce the density as if
those are x locations, not heights. 
iii) density's height is scaled to unit area and not the height of your
bars.

And a statistical issue: barplot typically plots heights for a
categorical list of separate groups, while density assumes that x and y
are pretty much continuous interval scale quantities. Combining the two
would not generally have any useful meaning.

So the bad news is that not a lot of what you're doing is right. 

That isn't very helpful, so I'm going to guess at what you might be
trying to do. A thing which _would_ have some useful meaning is a
combination of a histogram (special case of bar plot) and a density
plot. 

Let's say you had data like this:

x<-rep(c(100,200,300,400,500,600,700), c(1,2,3,5,2,3,1))
x

(I've guessed at your actual values just for illustration; usually x
would not be simple integer multiples of 100, but it shows the
principle) 

Then you could do
hist(x, breaks=seq(50,750,100), freq=FALSE)
lines(density(x))

The freq=FALSE tells the histogram to scale for unit area so the density
line can be compared with the histogram heights.

Does that help?

Steve E

>>> Olga Lyashevska <olga at herenstraat.nl> 03/14/10 9:32 PM >>>
Dear all, 

I am making a barplot as following:

barplot(c(1,2,3,5,2,3,1),names.arg=c("100","200","300","400","500","600","700"),xlab="diameter",ylab="flow",main="some
title",space=0.1)

I am also trying to add a probability density curve, however using
lines(density(c(1,2,3,5,2,3,1))) does not give a desired result.

Any tips?
Thanks

Olga

______________________________________________
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.


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list