I'm using R
to create my graphics for the Walker alias tables I am using in my thesis. I have managed to produce every graph using ggplot2
, except for the last one where the alias values are allocated so the probability in each column equals 1.
The graph with the probabilities scaled prior to creating the aliases is:
foo <- data.frame(Buscount=c(1,2,3,4,5), Rescaled.busfreq= c(5/9, 10/9, 15/9, 10/9, 5/9))
ggplot(foo, aes(x=factor(Buscount),y=Rescaled.busfreq, fill=factor(Buscount))) +
geom_bar(stat="identity", width=1) +
scale_fill_manual(values=c("cyan","magenta2","gold","gray","darkolivegreen3", "black")) +
scale_x_discrete(labels=c("a-2", "a-1", "a", "a+1", "a+2"), expand=c(0,0), name="Real count") +
scale_y_continuous(breaks=seq(0,15/9, by=3/9),labels=c("0", "3/9","6/9","9/9", "12/9", "15/9"), expand=c(0,0),
name="Adjusted probability of count") +
geom_rect(data=NULL, aes(xmin = 0.5, xmax = 5.5, ymin = 0, ymax = 9/9), color="black", fill=NA, size=1.5) +
geom_vline(xintercept=c(1.5, 2.5, 3.5, 4.5), color="gray") +
theme(panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_line(color="gray"),
panel.background=element_blank(), legend.position="none",
axis.line = element_line(color="gray", size = 1))
This produces the desired output:
I thought a stacked bar graph in ggplot2
would be the most convenient method of fitting the values into the 1 x 5 plane, but I can't get the stacked bar graph to work. This is the code I have ended up with after a number of attempts, and I have constructed a new data.frame as the lengths exceed those in the original data.frame. In order not to repeat the Columns data in the Values data, the Values data has substituted A
for a-2
, B
for a-1
and so forth. The 0's are there as fillers so that exactly five probabilities contribute to each Columns value.
Final.Buscount.Alias <- data.frame(Values=rep(c("A","B", "C", "D", "E"), times=5))
Final.Buscount.Alias$Probabilities <- c(5/9,4/9,0,0,0, 0, 6/9, 0, 3/9,0, 0,0,9/9,0,0, 0,0,2/9,7/9,0, 0,0,4/9,0,5/9)
Final.Buscount.Alias$Columns <- rep(c("a-2","a-1", "a", "a+1", "a+2"), each=5)
ggplot(Final.Buscount.Alias, aes(x=factor(Columns),y=Probabilities, fill=factor(Values))) +
geom_bar(stat="identity", width=1) +
scale_fill_manual(values=c("cyan","magenta2","gold","gray","darkolivegreen3", "black")) +
scale_x_discrete(labels=c("a-2", "a-1", "a", "a+1", "a+2"), expand=c(0,0), name="Real count") +
scale_y_continuous(breaks=seq(0,15/9, by=3/9),labels=c("0", "3/9","6/9","9/9", "12/9", "15/9"), expand=c(0,0),
name="Probabilities including alias") +
geom_rect(data=NULL, aes(xmin = 0.5, xmax = 5.5, ymin = 0, ymax = 9/9), color="black", fill=NA, size=1.5) +
geom_vline(xintercept=c(1.5, 2.5, 3.5, 4.5), color="gray") +
theme(panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_line(color="gray"),
panel.background=element_blank(), legend.position="none",
axis.line = element_line(color="gray", size = 1))
But the colours appear to be correct, but there are some problems. The bar for a-1
is the only correct one. The bar at a-2
should be at a
, the bar at a
should be at a-2
. a+1
and a+2
are almost correct, although - strictly speaking - the order of the bars within the columns should be reversed. The graph I am trying to create is one I produced manually in Excel:
There seems to be an ordering inside ggplot2
that I don't understand.
I've read some solutions to stacked bar graphs here, here, here, here, and here, but I can't work out what I am doing wrong.