I am trying to plot a stacked bar plot in ggplot2 of multiple samples; however, when I graph my data, the PDF is riddled with white lines on each barplot.
Here is some example code so you can see what my data looks like:
Expt <- as.data.frame(round(replicate(10, runif(10, 0, 5)), 3))
colnames(Expt) <- c("S_3", "S_5", "S_12", "S_22", "S_25", "S_27", "S_33", "S_52", "S_100", "S_101")
Expt$Phyla <- c("Proteobacteria", "Firmicutes", "Actinobacteria", "Bacteroidetes", "Verrucomicrobia", "Planctomycetes", "Cyanobacteria", "Chlamydiae", "Chloroflexi", "Deinococcus")
colors <- c("navajowhite4", "dodgerblue", "cyan", "orange", "lawngreen", "burlywood1", "black", "pink", "slateblue2", "hotpink")
library(reshape2)
Expt_melt <- melt(Expt, id.vars = "Phyla")
colnames(Expt_melt)[2:3] <- c("Sample", "Abund")
library(ggplot2)
ggplot() + geom_bar(aes(x = Sample, y = Abund, fill = Phyla), data = Expt_melt, stat = 'identity') + scale_fill_manual(values = colors)
This code produces a nice stacked barplot; however, my data has well over 6000 rows and many more samples and the actual image I get in return looks different dep. on the medium I use.
Any ideas as to why this is occurring? I need to be able to use the graph at PDF Zoomed at 150% when I insert it into a a normal powerpoint slide w/out it looking riddled with internal white lines. Not to mention, eventually, these graphs will get published and I cannot turn in a PDF that looks right when only zoomed in at 150%.
Looking into the the white line issue, I found this link to get rid of the internal bar lines: Getting Rid of Internal Bar Lines in a Bar Plot and following their code, I graphed my data using this code:
ggplot(Expt_melt, aes(x= Sample, y= Abund, fill = Phylum)) + stat_summary(fun.y = sum, geom = "bar") + scale_fill_manual(values = colors)
This produced the following graph:
As you can see, this completely deleted all of my top bars - which were my Phyla (Expt_melt$Phyla) with lower Expt_melt$Abund values. Not to mention, this graph has to be zoomed in at 200% to be seen 'normal'.
Any suggestions on what to do? Any ideas as to why these graphs are exhibiting this behavior?
Thank you!
The default width of bars in ggplot is 0.9 (on a categorical axis where each level has a width of 1), so by default there are gaps between the bars. You may want to try setting width = 1
:
ggplot() +
geom_bar(aes(x = Sample, y = Abund, fill = Phyla),
data = Expt_melt, stat = 'identity',
width = 1) +
scale_fill_manual(values = colors)