[R] histogram-like plot with two variables

Duncan Murdoch murdoch at stats.uwo.ca
Mon Sep 29 12:51:19 CEST 2008


On 28/09/2008 10:02 PM, Jörg Groß wrote:
> Hi,
> 
> I want to plot a binomial propability distribution.
> 
> I know how to generate the data;
> 
>    x <- seq(from=0, to=14, by=1)
>    y <- dbinom(x, 14, 0.7, log = FALSE)
> 
> but what I don't know is how to plot this within a histogram like plot.
> Because the histogram function only accepts one variable.
> 
> Is there a way to get the look of "hist()" with two variables?
> 
> 
> 
> I tried:
> plot(x,y, type="h")
>   but the bars are very thin -is there a way to define the width?

You could use barplot():

barplot(y,names=x)

You could also do it with plot.histogram, but it's trickier, because 
it's designed for continuous data.  For example,

dat <- hist(x, plot=FALSE, breaks=c(-1,x)+0.5)
dat$density <- y
plot(dat, freq=FALSE)

Duncan Murdoch



More information about the R-help mailing list