Reputation:
I have two vectors of different lengths, each containing numbers between 0 and 50. Some numbers are not containted in the vectors, others may appear multiple times.
a <- c(22, 11, 9, 15, 19, 14, 13, 17, 24, 21, 21, 19, 20, 23, 18, 20, 21, 15, 25, 11, 21, 19, 27, 20, 30, 11, 31, 28, 21, 24, 29, 13, 19, 13, 18, 18, 11, 29, 7, 21, 36, 21, 31, 24, 28, 36, 12, 21, 18, 27, 6, 23, 22, 25, 17, 15, 27, 9, 33, 14, 4, 15, 31, 27, 22, 25, 31, 23, 8, 23, 27, 21, 19, 17, 5, 29, 15, 26, 25, 30, 29, 5, 19, 12, 23, 8, 21, 20, 23, 19, 18, 40, 33, 17, 15, 25, 15, 15, 10, 9, 19, 11, 41, 14, 33, 5, 28, 15, 27, 16, 9, 5, 21, 19, 19, 29, 30, 8, 15, 20, 26, 17, 35, 18, 18, 26, 35, 33, 30, 11, 26, 21, 14, 20, 20, 23, 15, 21, 23, 15, 12, 8, 33, 13, 15, 5, 19, 12, 23, 18, 19, 15, 18, 16, 7, 19, 21, 23, 8, 10, 6, 5, 20, 19, 18, 13, 32, 14, 11, 14, 26, 28, 20, 9, 31, 19, 9, 23, 29, 12, 37, 17, 15, 13, 18, 23, 18, 10, 13, 18, 28, 8, 17, 18, 14, 14, 19, 23, 16, 30, 16, 16, 19, 13, 15, 25, 22, 36, 8, 26, 5, 2, 26)
b <- c(7, 2, 3, 11, 16, 1, 3, 9, 15, 27, 2, 5, 11, 13, 24, 29, 11, 6, 1, 2, 5, 4, 2, 1, 7, 3, 0, 26, 13, 2, 15, 14, 11, 12, 15, 10, 4, 24, 21, 3, 43, 12, 19, 5, 2, 30, 9, 3, 5, 8, 25, 5, 24, 16, 15, 7, 2, 28, 8, 1, 15, 11, 3, 19, 28, 7, 3, 16, 7, 19, 5, 7, 1, 21, 21, 4, 8, 11, 16, 27, 13, 9, 2, 5, 14, 10, 3, 4, 20, 10, 7, 1, 10, 13, 11, 12, 10, 9, 24, 4, 26, 7, 11, 14, 3, 2, 9, 5, 1, 6, 9, 8, 16, 23, 3, 5, 5, 23, 25, 14, 3, 7, 16, 1, 11, 4, 2, 8, 6, 2, 2, 2, 2, 16, 8, 5, 15, 14, 10, 7, 9, 13, 5, 10, 18, 1, 24, 1, 8, 14, 3, 16, 18, 13, 0, 0, 10, 3, 21, 10, 8, 4, 2, 2, 4, 1, 10, 10, 8, 5, 17, 19, 2, 6, 5, 5, 17, 13, 0, 1, 19, 2, 14, 24, 7, 4, 8, 9, 7, 9, 6, 15, 8, 3, 7, 9, 13, 20, 13, 1, 7, 6, 28, 2, 11, 7, 0, 14)
I want to plot lines that show how often each number is contained in each vector, i.e. the frequency of the numbers.
I can draw histograms that show the frequency, if I set the breaks to between each possible number:
hist(a, breaks=seq(-0.5, 50.5, 1), xlim = c(0, 50), col = rgb(0,1,0,0.5))
par(new=TRUE)
hist(b, breaks=seq(-0.5, 50.5, 1), xlim = c(0, 50), col = rgb(1,0,1,0.5))
I know that there is an empirical cumulative distribution function (ecdf()
), which results in an S-like shape; but what I want is a non-cumulative empirical distribution fuction that will result in something like a stepped bell-curve, similar to the outline of the histogram.
The closest I can get is plotting the density:
plot(density(a), xlim = c(-10, 50))
par(new=TRUE)
plot(density(b), xlim = c(-10, 50))
But that is not what I want. What I want is a frequency "curve". It should look something like this (drawn freehand in Photohshop):
So how can I achieve a frequency "curve"?
Upvotes: 0
Views: 568
Reputation: 24535
Try following with ggplot:
dual = data.frame(grp='a', value=a)
dual2 = data.frame(grp='b', value=b)
dual3 = rbind(dual, dual2)
dd = data.frame(with(dual3, table(grp, value)))
> head(dd)
grp value Freq
1 a 0 0
2 b 0 5
3 a 1 0
4 b 1 13
5 a 2 1
6 b 2 19
ggplot(dd, aes(x=value, y=Freq, group=grp, fill=grp))+geom_bar(stat='identity', position='dodge')
ggplot(dd, aes(x=value, y=Freq, group=grp, color=grp))+geom_line()
ggplot(dd, aes(x=value, y=Freq, group=grp, color=grp))+stat_smooth()
Upvotes: 0
Reputation: 14413
Something like this?
ah <- hist(a, seq(-0.5, 50.5, 1), plot=FALSE)
bh <- hist(b, seq(-0.5, 50.5, 1), plot=FALSE)
plot(ah$breaks[-1], ah$counts, type="s", ylim=c(0, 100), xaxt="n")
lines(bh$breaks[-1], bh$counts, type="s", col="red")
axis(1, at=ah$breaks)
Upvotes: 2
Reputation: 132576
Using ggplot2:
DF <- data.frame(x=c(a, b), g=c(rep("a", length(a)), rep("b", length(b))))
library(ggplot2)
ggplot(DF, aes(x=x, colour=g)) +
stat_bin(position="identity", breaks=seq(-0.5, 50.5, 1),
geom="errorbarh", aes(xmin=..x..-0.5, xmax=..x..+0.5), height=0, size=2) +
theme_bw() +
theme(legend.position="none")
Upvotes: 1