r - Stacked bar chart with multiple facets -
i trying plot stacked bar chart multiple facets using code below:
dat <- read.csv(file="./fig1.csv", header=true) dat2 <- melt(dat, id.var = "id") ggplot(dat2, aes(x=id, y=value, fill = variable)) + geom_bar(stat="identity") + facet_grid(. ~ col1) + geom_col(position = position_stack(reverse = true))
and here minimized example of how data looks like:
id col1 col2 col3 col4 col5 1 1 0.2 0.1 0.1 0.1 2 1 0.2 0.1 0.2 0.1 3 1 0.2 0.2 0.2 0.1 4 2 0.1 0.1 0.2 0.1 5 2 0.1 0.1 0.1 0.2
however, keep getting error below. think problem coming facet_grid(. ~ col1)
, more using col1
.
error in combine_vars(data, params$plot_env, cols, drop = params$drop) : @ least 1 layer must contain variables used facetting
does have idea how can fix that?
the col1
not included variable in melt
function, melted rest of columns. include col1
variable in melt
function.
dat2 <- melt(dat, id.var=c("id", "col1"))
wiki
Comments
Post a Comment