I made a simple example to see if I could follow what you are doing. Whether or not x or y is a zero is not provided in the table itself but rather the dimension names of the tables (the function table outputs an array). So you need to select only the dimensions that are not zero. Below is my code:
df <- data.frame(x = floor(runif(1000) * 3), y = floor(runif(1000) * 4))
graph1 <- table(df$x, df$y)
barplot(unlist(graph1))
graph1
keepRow <- dimnames(graph1)[[1]] != 0
keepCol <- dimnames(graph1)[[2]] != 0
graph2 <- graph1[keepRow, keepCol]
graph2
barplot(unlist(graph2))
To do the comparable in ggplot, I find it more straightforward to work with the df directly but to create factor variables from the numeric ones:
df$xx <- as.factor(df$x)
df$yy <- as.factor(df$y)
ggplot(df, aes(x = yy)) +
geom_bar() +
geom_bar(aes(fill = xx))
nrow(df)
df2 <- df[as.logical(((df$x != 0) * (df$y != 0))), ]
nrow(df2)
head(cbind(df, ((df$x != 0) * (df$y != 0))))
ggplot(df2, aes(x = yy)) +
geom_bar() +
geom_bar(aes(fill = xx))