我目前有兩個不同的資料框:
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")
我試圖建立一個分組,堆疊barplot哪里n是在x-axis,與堆疊條形Condition,通過分組Method,并具有Value在y-axis。到目前為止,我已經能夠開發一個資料幀只能有Condition == 'Correct',但我想完全填滿酒吧100。
我之前問過這個問題,但認為我解釋得不夠好,但仍在努力為該問題開發結果。
提前致謝。
uj5u.com熱心網友回復:
要通過Condition填充到 100%來堆疊條形,請使用position_fill(). 而facet_wrap到GROUP BY Method。
df3 <- rbind(df1, df2)
ggplot(df3, aes(n, Value, fill = Condition))
geom_col(position = position_fill())
facet_wrap( ~ Method)

編輯
受此
uj5u.com熱心網友回復:
您可以rbind將資料reshape、 和使用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')
注:使用R >= 4.1。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400190.html
