I have tried to make barplots in ggplot2 in a loop, each loop with this code
up <- data.frame(X = c("A", "B", "C"),
Y = sample(1:3))
down <- data.frame(X = c("F", "G", "H", "I"),
Y = sample(1:4))
up$type <- "up"
down$type <- "down"
gos <- rbind(down,up)
gos$X <- factor(gos$X, levels=gos$X)
# Diverging Barcharts
g <- ggplot(gos, aes(x=X, y=Y , label=Y)) +
geom_bar(stat='identity', aes(fill=type), width=.5,position="dodge") +
scale_fill_manual(name="Expression",
labels = c("Down regulated", "Up regulated"),
values = c("down"="#00ba38", "up"="#f8766d")) +
labs(title= "Biological process") +
coord_flip()
g
Because of the number of observers changes in the loop, I don't have a fixed number of bar per barplot. For example, in this case, I got 3 bars in the up category, 4 bars in the down category.
However, for better visual, I want to make the structure of the plot fixed, and the empty observer will be left empty in the barplot.
For example: I want all of them has this structure, 5 bars in each category, the empty bar will still be kept (without anydata)
How can I do this using ggplot2 in R? Thank you very much