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.
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).
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")