I am working on some data and what I'd like to do is something like the plot below. With bars following a continuous scale (time period), a great bar_width (easy to read), no overlapping issue.
Yet with my set of data and Rstudio I can only get something like this:.
You see, there are lots of bars overlapping ( overlapping labels are not a problem for me, just the actual bars) . I tried to adjust parameters like bar_width , and also width and height when I save it from Rstudio but there is always something wrong. When there is no overlap it is too small and bars are too thin etc.
Here you can find my script, as well as the data necessary to obtain the figure: MyData
I hope I followed all the instructions of stackoverflow, please excuse my poor English, and most of all thanks for your help!
Edit: A sample of my data (but to reproduce the error you'll need the whole thing)
color_by label_by Sample Abundance
1 Ar Chl 2 0.0050024660
2 Ar Rho 2 0.0014796026
3 Ar Str 2 0.0031001198
4 Ar Unknown 2 0.0000000000
5 Al Api 2 0.0076798422
6 Al Cil 2 0.0073980131
7 Al Din 2 0.0228281547
8 Al Unknown 2 0.0002113718
9 Op Cho 2 0.0000000000
10 Op Fun 2 0.0007045727
And here is a sample of the code I use to obtain the "MyResult" plot:
counts_long<-read.table("data.csv", header=TRUE,sep= ",")
counts_long$X<-NULL
clr_pal<-c("#B8143C", "#E6194B" ,"#EE6587", "#F6B2C3", "#30903C" ,"#3CB44B", "#7DCD87" ,"#BEE6C3",
"#CCB414" ,"#FFE119", "#FFE74C" ,"#FFEE7F", "#FFF5B2" ,"#4363d8", "#8197E5", "#f58231",
"#F8AB75", "#911eb4", "#B569CD", "#DAB4E6", "#42d4f4", "#81E2F7", "#C028B8", "#F032E6",
"#F576EE", "#FABAF6", "#bfef45", "#000000")
bar_width=3
p <- ggplot2::ggplot(counts_long, aes(x = Sample, y = Abundance, fill = label_by)) +
ggplot2::geom_bar(position = "stack", stat = "identity", width = bar_width) +
ggplot2::guides(fill=guide_legend(title = "legend", ncol = 1)) +
ggplot2::scale_fill_manual(values = clr_pal) +
xlab("Depth(cm)")+
scale_x_reverse(breaks=counts_long$Sample)+
ggplot2::scale_y_continuous(expand = c(0,0)) +
ggplot2::theme(axis.line.x = element_line(colour = 'grey'),
axis.line.y = element_line(colour = 'grey'),
axis.ticks = element_line(colour = 'grey'),
axis.text.x = element_text(angle = 90, family = "Helvetica",
size = 6, hjust = 1, vjust = 0.5),
legend.background = element_rect(fill = 'transparent', colour = NA),
legend.key = element_rect(fill = "transparent"),
legend.key.size = unit(0.4, "cm"),
panel.background = element_rect(fill = 'transparent', colour = NA),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = adjustcolor('grey', 0.2)),
panel.grid.minor = element_line(colour = NA),
plot.background = element_rect(fill = 'transparent', colour = NA),
plot.title = element_text(hjust = 0.5),
strip.background = element_blank(),
strip.text = element_text(family = "Helvetica", size = 8, face = "bold"),
text = element_text(family = "Helvetica", size = 8))
p+coord_flip()