我在 Excel 中有一個圖表,如果可能的話,我想在 R 中復制它。我是 R 的新手,所以任何指導將不勝感激。所以我的資料看起來像這樣

如果有人需要,我可以包含該檔案。
然后我有這張圖:

我想繪制與 Excel 檔案中相同的圖形,但使用 R。那么,有沒有辦法為屬于主值的 x 軸值創建一種子集?
我查看了 ggplot 檔案和
ggplot(df, aes(x = rowname, y = value, fill = name))
geom_bar(position = "dodge", stat = "identity")

uj5u.com熱心網友回復:
這可能會有所幫助
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(ACI:SB) %>%
mutate(across(where(is.character), as.factor)) %>%
ggplot(aes(x = R, y = value, fill=name))
geom_bar(stat="identity", position = "dodge", width=0.75)
facet_wrap(~A, nrow=1, strip.position="bottom")
theme(legend.position = "bottom")
labs(fill="", y="", x="")
產生:

如果你想“用 Excel 口音說 R”并將這個漂亮的圖轉換成默認的 excel 圖,那么你可以在圖的末尾添加theme_excel_new()ggtheme 包
library(ggthemes)
...
theme_excel_new()
它會給出以下情節

樣本資料:
structure(list(A = c(25, 25, 25, 50, 50, 50, 100, 100, 100, 250,
250, 250), R = c("R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2",
"R3", "R1", "R2", "R3"), ACI = c(2.94, 1.91, 8.86, 5.03, 8.77,
1.89, 7.58, 7.24, 9.44, 5.48, 7.12, 3.89), PB = c(1.01, 9.27,
2.83, 5.91, 1.1, 8.41, 3.18, 7.83, 2.68, 2.19, 5.17, 2.69), NB = c(1.81,
5.19, 5.63, 1.29, 2.56, 7.18, 9.61, 1, 7.63, 9.48, 8.19, 3.08
), Bca = c(6.5, 9.53, 9.54, 3.4, 2.62, 1.65, 3.22, 5.1, 9.24,
5.11, 2.58, 0.46), SB = c(4.18, 8.54, 3.47, 1.31, 3.74, 6.31,
3.9, 6.9, 6.89, 5.55, 4.3, 4.53), `round(2)` = c(2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2)), class = "data.frame", row.names = c(NA,
-12L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536949.html
標籤:r图表2
上一篇:如何按質量編號對變數進行分組?
