1

Here is an example of my data. I have several different sensor data (Var1) broken up by sensor(Var2). Here's a reproducible data set.

var.sens1 <- structure(list(Var1 = structure(c(9L, 9L, 9L, 9L, 9L, 1L, 2L, 
                                           9L, 9L), 
                                         .Label = c("bathymetry", "current", "deep scattering layer", 
                                                    "frontal data", "imagery", "ocean productivity", 
                                                    "salinity",  "sea surface height", "sea surface temperature",
                                                    "vessel", "wind"), 
                                         class = "factor"), 
                        Var2 = structure(c(1L, 2L, 3L, 4L, 7L, 8L, 8L, 8L, 10L), 
                                         .Label = c("AMSR-E", "AVHRR", "CZCS", "Imager", 
                                                    "IRS-P4 OCM", "MERIS", "MODIS", "not specified", 
                                                    "OCI", "SeaWiFS", 
                                                    "SeaWinds"), class = "factor"), 
                        Freq = c(4L, 37L, 1L, 3L, 20L, 4L, 20L, 26L, 2L)), 
                   row.names = c(9L, 20L, 31L, 42L, 75L, 78L,79L, 86L, 108L), 
                   class = "data.frame")

I then use this code to facet wrap (rather than stacked barplot) my data.

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_bar(stat="identity")

vp + facet_wrap( ~ Var1, ncol=3, scales="free_y") +
  coord_flip() +
  theme_light()+
  theme(legend.position="none") +
  xlab("Sensor") + ylab("Frequency")

I use scales_y=free to remove any unwanted space in the graph. And get the following plot.

enter image description here

However, the barwidths are different sizes. Does anyone know how to fix the bar width while facet wrapping? Fixing barwidth in geom_bar doesn't seem to work. Neither does using geom_col with position_dodge2 and preserve as recommended here. See code and image below.

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_col(position = "dodge")

vp + 
facet_grid(~Var1, scales = "free_y")+
coord_flip() +
theme_light()+
theme(legend.position="none") +
xlab("Sensor") + ylab("Frequency")

I'd prefer to use facet_wrap over facet grid so I don't have one long series of plots ( I have multiple variables that need plotting in my data set not just the three shown).

enter image description here

mikejwilliamson
  • 405
  • 1
  • 7
  • 17
  • 1
    Possible duplicate of [The same width of the bars in geom\_bar(position = "dodge")](https://stackoverflow.com/questions/38101512/the-same-width-of-the-bars-in-geom-barposition-dodge) – pogibas Oct 15 '18 at 08:33
  • I tried position dodge and it didn't seem to work. – mikejwilliamson Oct 15 '18 at 13:13
  • Why? What's the error message you're getting? – pogibas Oct 15 '18 at 13:14
  • no error message. just doesn't change the widths so they match. It also uses facet_grid, and I have multiple variables so facet_wrap is more applicable. – mikejwilliamson Oct 15 '18 at 13:22
  • no error message.It changes the widths but then doesn't remove the the empty sensors. See update to question. – mikejwilliamson Oct 15 '18 at 13:31
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Oct 16 '18 at 01:51
  • Thanks. I have added the code for a reproducible sample of my data. – mikejwilliamson Oct 17 '18 at 07:33

1 Answers1

2

use geom_col (position = position_dodge2(preserve = "single"))

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_col(position = position_dodge2(preserve = "single"))

vp + 
  facet_grid(~Var1, scales = "free_y")+
  coord_flip() +
  theme_light()+
  theme(legend.position="none") +
  xlab("Sensor") + ylab("Frequency")

enter image description here

M.Pinilla
  • 70
  • 6