0

I have tried finding the questions with similar questions but was not fruitful. If this question has already been requested, please guide me there.


How to add space within each bar of the stacked barplots?

df <- data.frame(Type =sample(LETTERS[1:5],14,replace = T), 
                  ET =sample(c('A1','A2','B3','B6','C5'),14,replace = T))
ggplot(df, aes(x=Type, fill = as.factor(ET))) + geom_bar()

enter image description here

For example, space between the two types (colours) within the A bar.

There is a option of doing it for two variables using the below command but not sure for a single variable plot

ggplot(data, aes(x=xvar,y=yvar,fill=zvar)) + geom_bar(stat="identity")

Tried this command, but did not work:

df %>% group_by(Type) %>% count(ET) %>% data.frame() %>%  ggplot(aes(x=Type, y=n, fill = ET)) + geom_bar(stat="identity")

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84
  • 1
    What exactly do you mean by "spacing"? They're stacked, inherently there should be no spacing between the different colors. Do you mean changing how the colors are ordered? e.g. purple on top of red? – bouncyball Aug 21 '17 at 14:57
  • Yes, I agree with you on the concept of stacked bar plots. In my case, when there are more than 10s of subtypes within each bar, it is getting difficult to distinguish them. So. Was looking for space which clearly separates the types. – Prradep Aug 21 '17 at 15:04
  • maybe use a dodged bar plot instead of stacked? `ggplot(df, aes(x=Type, fill = as.factor(ET))) + geom_bar(position = 'dodge')` – bouncyball Aug 21 '17 at 15:06
  • 1
    @Prradep If you are so concerned about the space to distinguish each type, then why not add a `width` parameter. Like `ggplot(df, aes(x=Type, fill = as.factor(ET), width=0.5))+ geom_bar(colour="black", position="stack")` ? – mnm Aug 21 '17 at 15:07
  • 1
    Perhaps we're overthinking this. If you simply want the stacked bars visually distinct from one another, how about setting `col = "black"` (or white) in the `geom_bar()` call? – Z.Lin Aug 21 '17 at 17:18
  • @Ashish Thanks for the idea! I would use it as an alternative if I do not hear any other solution for my question. – Prradep Aug 22 '17 at 07:35
  • @bouncyball It looks as a good alternative for this example case but it will be a bit messy to distinguish types when I have 50+ types with 500+ subtypes in total. – Prradep Aug 22 '17 at 07:36
  • Thanks @Z.Lin It is a very good alternative(or actually the answer, I'm looking for). Are there possibilities of increasing the width of the `white` or `black` colors? – Prradep Aug 22 '17 at 07:39
  • 1
    @Prradep Glad to be of assistance. You can control the line thickness via `size`, e.g. `geom_bar(col = "black", size = 2)` :) – Z.Lin Aug 22 '17 at 07:56
  • Please write it as the solution below. I could accept it until there is another best way to do this(Which I would not expect after the searches I have done so far). – Prradep Aug 22 '17 at 11:07
  • @Z.Lin if you see my comment, I have already specified both `width` in the `ggplot()` call as well as `colour` in the `geom_bar()` call to make a distinction between the types. – mnm Aug 22 '17 at 12:04
  • @Prradep are you sure you will accept @Z.Lin answer, when actually I had `kind of`answered it first. Its just that I did not highlight the `colour`. As my focus was on `width`. I'm sorry if this sounds like `claiming the crown` but I'm forced to put forth my `claim` because @Z.Lin inadvertently sought `resolution claim` without providing a reference to `my previous comment`. – mnm Aug 22 '17 at 12:11
  • @Prradep Also, Z.Lin `proposed solution` is exactly the same as the one I had previously commented. The only difference is in the spelling of colour. I wrote `colour=black` and she wrote `col=black`! Fascinating! Are you sure, you know what you want? – mnm Aug 22 '17 at 12:26
  • @Ashish As per your comment (`then why not add a width parameter`), my focus has shifted only to width parameter and as per @Z.Lin's comment (`how about setting col = "black" (or white) in the geom_bar() call?`) my focus was only on `col` parameter. As per my above understanding, I have requested OP to write it as a solution. Please be informed that I have no special favour for OP or against you. I would really appreciate you if you do not consider it any other (wrong) way and act accordingly. Thanks for your cooperation. Sorry if it has hurt anyof you. Thanks.. Happy SO'ing. – Prradep Aug 23 '17 at 09:18
  • @Prradep, if your question has been answered to your satisfaction, I suggest you (or anyone else) can post and it can be marked as an answer. Thanks.. – mnm Aug 23 '17 at 09:28

1 Answers1

0

If any one is looking for the answer I was looking for, please have a look at the snippet below; which provides a similar output

ggplot(df, aes(x = Type, fill = as.factor(ET))) + 
  geom_bar(colour = "white", size = 2)

enter image description here

As the above answer is based on the comments given for the question, I am not accepting this as an answer.

Prradep
  • 5,506
  • 5
  • 43
  • 84