-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistograms.R
More file actions
29 lines (23 loc) · 1.01 KB
/
Histograms.R
File metadata and controls
29 lines (23 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(ggplot2)
#load data, in this case using example data from R
cars<-chickwts
# look at data structure
str(cars)
# Basic histograms using Rbase
hist(cars$weight)
hist(cars$weight,breaks=100)
# Basic histograms using ggplot
ggplot(cars, aes(x=weight)) + geom_histogram()
# Change the width of bins
ggplot(cars, aes(x=weight)) + geom_histogram(binwidth=1)
# Change colors
ggplot(cars, aes(x=weight)) + geom_histogram(color="black", fill="white")
#add a verticle line at mean
ggplot(cars, aes(x=weight)) + geom_histogram(color="black", fill="white")+
geom_vline(aes(xintercept=mean(cars$weight, na.rm = T)),color="blue", linetype="dashed", size=1)
# Overlaid groups histogram
ggplot(cars, aes(x=weight, color=feed ,y = ..ncount..)) +
geom_histogram(fill="white", alpha=0.5, position="identity")
# facetwrapped, color-coded groups histograms
ggplot(cars, aes(x=weight, color=feed)) + geom_histogram(fill="white")+facet_grid(feed~.)
ggplot(cars, aes(x=weight, color=feed)) + geom_histogram( fill="white")+facet_grid(.~feed)