You can use ggplot2
package using stat = identity
and facet_wrap
. Additionally you can use melt
function from reshape2
package to transform data convinient for use in ggplot2
- from wide to narrow dataset format. Please see the code below:
library(ggplot2)
library(reshape2)
data <- structure(list(sex = structure(c(2L, 1L, 1L), .Label = c("female",
"male"), class = "factor"), ethnicity = structure(c(1L, 1L, 2L
), .Label = c("dutch", "german"), class = "factor"), x2015 = c(112L,
114L, 102L), x2016 = c(117L, 118L, 101L), x2017 = c(116L, 120L,
99L)), class = "data.frame", row.names = c(NA, -3L))
df <- melt(data)
ggplot(df, aes(x = ethnicity, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap(~ sex)
Output:
