Possibly it was only for your example that you kept the data for each plot in separate vectors. Anyway, if the number of locations would be much bigger, you will soon have your workspace cluttered with small vectors, and you would have to call table
and barplot
many times.
It would be much easier to work with the data stored in a data frame, regardless if you plot using base
R functions, or ggplot
. Furthermore, it might be easier to compare counts for different levels of RSSI, among the different locations, if the same set of classes for each location is used in each plot, i.e. that also RSSI classes with zero counts were included. You might also use the same scale of the y axis across locations. Here is a small example with ggplot
library(ggplot2)
# create a data frame with the data in your vectors
# 'x' is the value, and 'loc' the location of each registration
df <- data.frame(x = c(rep(10, 6), rep(4, 32),
rep(9, 6), rep(10, 32), rep(11, 4),
rep(10, 6),
rep(10, 3), rep(9, 3), rep(8, 5)),
loc = c(rep("a", 6+32), rep("b", 6+32+4), rep("c", 6), rep("d", 3+3+5)))
# plot using geom_bar, which default counts the cases for each level of - no need for 'table'
ggplot(data = df, aes(x = factor(x))) +
geom_bar() +
facet_wrap(~ loc)