Code :
library(plyr)
library(datasets)
data("iris")
iris$Sepal.Length
size <- c()
for (s in iris$Sepal.Length){
if (s < 5.8){
size <- c(size, "SMALL")
} else if(s >= 5.8){
size <- c(size, "LARGE")
}
}
iris$Size <- size
plot(table(iris$Species, iris$Size))
plot :

I'm wondering how to plot this kind of thing in ggplot.
I have this (which is dependent on the previous code):
ggplot(as.data.frame(table(iris$Species, iris$Size)),
aes(x=Var1, y=Freq, fill=Var2)) +
geom_bar(stat="identity", position="fill") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(size=15),
text = element_text(size=15)) +
scale_x_discrete(labels=c("S1", "S2", "S3")) +
labs(y = "Percentage") +
labs(x = "") +
theme(axis.title = element_text()) +
ggtitle("Something about iris stuff") +
scale_fill_discrete(name = "Size")

Which communicates similar information, but it's not the same.
So - how can I make the a table in ggplot like that of plot(table(a, b)). I don't want it to be stylistically exactly the same ( or I'd just use base ), but I like the way that the proportions are displayed in that table more than the bars that I have with gg
The ability to be able to pass a table object is useful here as I'm generating the plots with base within a for loop
Edit - code which plots within a loop
I shall edit this post when it's finished so that it's cleaner, I didn't want to delete stuff that people might currently be looking at referencing
Here's some code that plots within a loop, by plotting a table object. I'm not sure how I would go about doing this in ggplot.
rm(list=ls())
library(plyr)
library(datasets)
data("iris")
set.seed(1234)
iris$Sep.Size <- c("SMALL", "LARGE")[(iris$Sepal.Length >= 5.8) + 1]
# create an additional categorical variable, purely for the sake of plotting it
iris$Data.2 <- cut(
rnorm(150, 10, 2),
c(-Inf, 8, 10, 11, Inf),
labels = c('a', 'b', 'c', 'd'),
include.lowest = TRUE)
iris.2 <- data.frame(data = iris$Data.2, sepsize = iris$Sep.Size, species = iris$Species)
# plotting tables using a loop - one of them will be nonsense, but the others are usable.
for ( i in 1:dim(iris.2)[2]){
t = table(iris.2$species, iris.2[,i])
plot(t)
}
There are 3 plots created as a result of this
size <- c("SMALL", "LARGE")[(iris$Sepal.Length >= 5.8) + 1L]. A one-liner. No loops at all. R is a vectorized language.findIntervalorcut. In the latter case, you would set thelabelsto the character values you want. In the former, use the return value offindIntervalto index a vector of values forsize. See the edit to the answer.