0

I am attempting to make a bar graph figure in ggplot2 with all bars having an equal width regardless if there is data present for a particular variable combination. This question is quite similar to the one found here "Consistent width for geom_bar in the event of missing data"; however, I do not have a variable mapped to the y-axis, but rather I am doing a count of the number of times a specific combination of variables occurs.

Does anyone know of a way to work around this? Also, in my original "actual" dataset, I have two variables to account for in facet_wrap, rather than one. Would answers differ when including this extra variable?

The code I have worked out currently is giving me uneven bar widths:

library(ggplot2) ggplot(dat, aes(Subjective_Assessment, fill=pts))+geom_bar(position="dodge")+ facet_wrap(~background)+ labs(y="Count")

Example Bar graph resulting in uneven bar widths

  species background      pts Subjective_Assessment
Species_1    State_1 Factor_1                  Good
Species_2    State_1 Factor_1                  Good
Species_3    State_1 Factor_1                  Good
Species_4    State_1 Factor_1                  Good
Species_1    State_1 Factor_2                  Poor
Species_2    State_1 Factor_2                  Poor
Species_3    State_1 Factor_2              Moderate
Species_4    State_1 Factor_2                  Poor
Species_1    State_1 Factor_3              Moderate
Species_2    State_1 Factor_3              Moderate
Species_3    State_1 Factor_3              Moderate
Species_4    State_1 Factor_3                  Poor
Species_1    State_2 Factor_1                  Good
Species_2    State_2 Factor_1                  Good
Species_3    State_2 Factor_1                  Good
Species_4    State_2 Factor_1                  Good
Species_1    State_2 Factor_2              Moderate
Species_2    State_2 Factor_2              Moderate
Species_3    State_2 Factor_2              Moderate
Species_4    State_2 Factor_2              Moderate

Thanks in advance for your help.

Community
  • 1
  • 1
mycelial
  • 11
  • 2
  • Transform your data so you have a variable mapped to the y-axis, then use the method in your linked duplicate. – Gregor Thomas Feb 12 '16 at 15:58
  • @Gregor Perhaps I'm not understanding. I added a "count" variable like you suggested and tried the linked approach, but I'm receiving a rbind error "numbers of columns of arguments do not match. dat.all<-rbind(dat,cbind(expand.grid(Subjective_Assessment=levels(dat$Subjective_Assessment), pts=levels(dat$pts), background=levels(dat$background), count=levels(dat$count)))) – mycelial Feb 12 '16 at 16:17
  • See also (1) [Consistent width for geom_bar in the event of missing data](http://stackoverflow.com/q/11020437/903061), (2) [A Way to Always Dodge a Histogram](http://stackoverflow.com/q/10149571/903061), and (3) [Control column widths in a ggplot2 graph with a series and inconsistent data](http://stackoverflow.com/q/28890227/903061), which are, I believe, exact duplicates. – Gregor Thomas Feb 12 '16 at 16:19
  • You probably don't want `count` in your expand.grid... you should use `dplyr` or `data.table` or `aggregate` to summarize your data in terms of the unique factor levels. – Gregor Thomas Feb 12 '16 at 16:21
  • @Gregor, I still can't get this to work out. When I remove "count" from my expand.grid, I receive the argument "error in rbind(deparse.level,...):numbers of columns of arguments do not match. I'm not familiar with summarizing data with dplyr or data.table or aggregate. Could you provide a clue as to how you might summarize the data this way? Thanks – mycelial Feb 17 '16 at 16:18

0 Answers0