0

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. Model

Yet with my set of data and Rstudio I can only get something like this:.

MyResult

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()
OTStats
  • 1,820
  • 1
  • 13
  • 22
Tyab
  • 5
  • 1
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Put sample data in the question itself. Linking to external sites is not helpful because links can disappear or contain dangerous content. – MrFlick Jun 26 '19 at 15:35
  • @MrFlick The two files I put in external links are my reproducible example. Sadly my minimal sample input necessary to reproduce the error is too long to put in here. I can still put part of it just for you to have an idea of how it looks, as well as the code , but in order to reproduce the whole thing you'll need everything ... – Tyab Jun 27 '19 at 06:46

1 Answers1

0

Two things you can try:

  • Click on the zoom button above the plot in rstudio and see if it looks better then. You should be able to export it this way too.
  • Otherwise do the plot in a Rmd and set the size of the figure in the R chunk options (e.g. r fig.width = 10, fig.height = 10)
Overlytic
  • 143
  • 7
  • I tried to set the size of the figure accordingly but it doesn't seem to be a problem of size. I wish there was a way to set a minimal space between bars. So that between the smallest interval of depth/Time there is no overlapping just a minimal spacing. And as soon as the interval get bigger, the spacing between bars would increase following my continuous scale... – Tyab Jun 27 '19 at 13:29
  • Maybe try the expand parameter, with the expand_scale function. See: https://ggplot2.tidyverse.org/reference/expand_scale.html. It ought to work, if the graph is not too crowded. I've tried on your graph, but haven't got it working yet. Worst case you could define your own custom expand scale. Alternatively you could define a custom scale function. But hopefully that won't be necessary. Sorry haven't played with these things much, so hope this helps. – Overlytic Jun 27 '19 at 15:13