The original UCLA ATS like is: http://www.ats.ucla.edu/stat/r/gbe/histogram.htm.
In ggplot2, geom_histogram is used to draw histogram.
The data is:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(121343) | |
x=rnorm(100) | |
u <- as.data.frame(x) |
The first one is histogram with black fill:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x), binwidth=.5, colour="black", fill="white") |
ggplot2 has more choice that you can fill in the color by the counts in each bin. like
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x, , fill=..count..), binwidth=.5, colour="black") |
Next is to change the binwidth. In ggplot2 this can be done by binwidth or by breaks. Here is shown by binwidth:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x), binwidth=.2) |
Next is to change from frequency to density(percentage in fact). This can be done by assigning y to be ..density.. in ggplot2:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x, y=..density..), binwidth=1) |
Then is to add normal density and empirical curve in the plot.
First is the empirical curve:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x, y=..density..), binwidth=.2)+geom_density(aes(x=u$x), colour="red") |
Next is the Normal density with mean and std are from the generated data:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(u)+geom_histogram(aes(x=u$x, y=..density..), binwidth=.2)+stat_function(fun=dnorm, args=list(mean=mean(x), sd=sd(x)), colour="red")+labs(title="Histogram with Density", y="Percentile") |
The last one is to add the counts on top of each bin. I did not figure out how to do it. It should be easy by adding text. But unitl now my process has not reached there.
No comments:
Post a Comment