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
Hi Olga, may be you can work around with the suggestions below, while other provide the best solution for your: 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) lines(lowess(c(1,2,3,5,2,3,1),f=1), type="b", lwd=2, lty=3, col="blue") cheers milton On Sun, Mar 14, 2010 at 5:32 PM, Olga Lyashevska <olga@herenstraat.nl>wrote:> 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@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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}}
I begin to think that R needs a _mis_fortunes package... Steve E>>> Rolf Turner <r.turner at auckland.ac.nz> 03/15/10 2:21 AM >>>On 15/03/2010, at 3:07 PM, S Ellison wrote (in response to a very confused question about superimposing density curves on barplots): <SNIP>> So the bad news is that not a lot of what you're doing is right.<SNIP> Sounds like a fortune to me! :-) cheers, ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
On 15/03/2010, at 3:37 PM, S Ellison wrote:> I begin to think that R needs a _mis_fortunes package...I am tempted to nominate *that* as a fortune! :-) cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
On 03/15/2010 08:32 AM, Olga Lyashevska wrote:> 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. >Hi Olga, Part of your problem is that the barplot function places bars at positions along the abcissa that are not quite what you expect. These start at 0.6 and end at 7.2 with an increment of 1.1. The density function produces values that begin at -1.048 and end at 7.048, effectively shifting the density curve to the left. As noted by other correspondents, the density function may not be what you want anyway. If you want a smoothed line that infers what might happen if flow for intermediate values of diameter were measured, you could try: lines(spline(1:7,c(1,2,3,5,2,3,1),xmin=0,xmax=8)) The smoothed values are still a bit offset, so you might consider using: library(plotrix) barp(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",height.at=0:5) lines(spline(1:7,c(1,2,3,5,2,3,1),xmin=0,xmax=8)) As the barp function places the bars at integer values, which happen to be convenient for this case, but also are easy to adjust if you used the real diameter values. Jim
Dear All, Thanks everyone for your kind responses. Now I understand what is gong on! I guess that the question asked is wrong in the first place. As there is a categorical list of groups and their values (a range of pipe diameters and a flow for each diameter), and a barplot would be a suitable tool to use. However, it also means that I cannot plot probability density curve using this type of data because I would need to have a histogram instead. Did I get it right? Many thanks, Olga> On 03/15/2010 08:32 AM, Olga Lyashevska wrote: >> 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.