Home > other >  Grouped and Stacked Barplot in R
Grouped and Stacked Barplot in R

Time:01-01

I currently have two distinct data frames:

dput(df1)
structure(list(n = c("0", "0.01", "0.02", "0.03", "0.04", "0.05", 
"0.06", "0.07", "0.08", "0.09", "0.1", "0", "0.01", "0.02", "0.03", 
"0.04", "0.05", "0.06", "0.07", "0.08", "0.09", "0.1"), Condition = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Correct", 
"Correct", "Correct", "Correct", "Correct", "Incorrect", "Incorrect", 
"Incorrect", "Incorrect", "Incorrect", "Incorrect", "Incorrect", 
"Incorrect", "Incorrect", "Incorrect", "Incorrect"), Value = c(100, 
100, 98, 84, 77, 36, 1, 0, 0, 0, 0, 0, 0, 2, 16, 23, 64, 99, 
100, 100, 100, 100), Method = c("Method1", "Method1", "Method1", 
"Method1", "Method1", "Method1", "Method1", "Method1", "Method1", 
"Method1", "Method1", "Method1", "Method1", "Method1", "Method1", 
"Method1", "Method1", "Method1", "Method1", "Method1", "Method1", 
"Method1")), row.names = c(NA, -22L), class = "data.frame")


dput(df2)
structure(list(n = c("0", "0.01", "0.02", "0.03", "0.04", "0.05", 
"0.06", "0.07", "0.08", "0.09", "0.1", "0", "0.01", "0.02", "0.03", 
"0.04", "0.05", "0.06", "0.07", "0.08", "0.09", "0.1"), Condition = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Correct", 
"Correct", "Correct", "Correct", "Correct", "Incorrect", "Incorrect", 
"Incorrect", "Incorrect", "Incorrect", "Incorrect", "Incorrect", 
"Incorrect", "Incorrect", "Incorrect", "Incorrect"), Value = c(100, 
100, 92, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 66, 100, 100, 100, 
100, 100, 100, 100), Method = c("Method2", "Method2", "Method2", 
"Method2", "Method2", "Method2", "Method2", "Method2", "Method2", 
"Method2", "Method2", "Method2", "Method2", "Method2", "Method2", 
"Method2", "Method2", "Method2", "Method2", "Method2", "Method2", 
"Method2")), row.names = c(NA, -22L), class = "data.frame")

I am trying to develop a grouped, stacked barplot where n is on the x-axis, with stacked bars for Condition, grouped by Method, and having Value on the y-axis. So far, I have been able to develop a DataFrame only having Condition == 'Correct', but am trying to fill the bars completely to 100.

I asked this question previously but don't think I explained it well enough, but am still struggling to develop the result for the problem.

Thanks in advance.

CodePudding user response:

To stack the bars by Condition filling to 100% use position_fill(). And facet_wrap to group by Method.

df3 <- rbind(df1, df2)

ggplot(df3, aes(n, Value, fill = Condition))  
  geom_col(position = position_fill())  
  facet_wrap( ~ Method) 

enter image description here


Edit

Inspired in enter image description here

CodePudding user response:

You could rbind the data, reshape, and use barplot.

rbind(df1, df2) |>
  transform(g=paste0(Condition, Method)) |>
  (\(.) .[c('n', 'Value', 'g')])() |> 
  reshape(direction="wide", idvar="n", timevar="g") |>
  (\(.) as.matrix(.[-1]))() |> 
  barplot(col=hcl.colors(11), legend.text=head(df1$n, 11), cex.names=.9,
          args.legend=list(x='topleft', ncol=2, cex=.8, title='n'), 
          main='My Plot') 

Note: R >= 4.1 used.

enter image description here

  • Related