Quantcast

ggplot 2: Histogram with bell curve?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ggplot 2: Histogram with bell curve?

wwreith
I am learning ggplot2 commands specifically qplot for the time being and I have figured out how to create histograms and normal density curves but I am not sure how to add a normal bell curve or other dist. as well on top of a histogram.

Here are the two graphs that I created.

## Histogram
t<-rnorm(500)
w<-qplot(t, main="Normal Random Sample", fill=I("blue"), colour=I("black"), geom="histogram")
w

##Density Curve
t<-rnorm(500)
r<-qplot(t, main="Normal Random Sample", colour=I("black"), geom="density", adjust=4)
r  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ggplot 2: Histogram with bell curve?

dicko ahmadou
Hi

Don't use t as var names, because t is also a function (transpose).
This code should work...
set.seed(1)
T <- rnorm(500)
qplot(T, geom = "blank") +
geom_histogram(aes(y = ..density..), colour = "black", fill = "blue") +
stat_density(geom = "line", colour = "red")

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ggplot 2: Histogram with bell curve?

William Dunlap
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of dicko ahmadou
> Sent: Tuesday, June 07, 2011 11:41 AM
> To: [hidden email]
> Subject: Re: [R] ggplot 2: Histogram with bell curve?
>
> Hi
>
> Don't use t as var names, because t is also a function (transpose).
> This code should work...
> set.seed(1)
> T <- rnorm(500)

I think that it is more dangerous to use 'T' than 't' since
'T' is a built-in data name (with value TRUE) and I've
seen it used more than 't' (probably because S and S+
regard it as a reserved word).  I don't think that reusing
built-in names as local names is as dangerous as some would make
out but reusing built-in data names is probably more dangerous
than using built-in function names for local data names
(because function name lookup is done differently than general
name lookup - the former will only look for functions).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> qplot(T, geom = "blank") +
> geom_histogram(aes(y = ..density..), colour = "black", fill =
> "blue") +
> stat_density(geom = "line", colour = "red")
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/ggplot-2-Histogram-with-bell-cur
ve-tp3580359p3580457.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ggplot 2: Histogram with bell curve?

dicko ahmadou
my bad....you are right
I always use TRUE instead of T,  so i forgot that by default T = TRUE in R.
Loading...