使用以下資料框:
dat <- structure(list(Model = c("I.E.T", "I.E.T.S", "I.E.T.N", "I.E.T.S.N",
"F.D.E.T", "F.D.E.T.N", "F.D.E.T.S.N", "I.E.T", "I.E.T.S", "I.E.T.N",
"I.E.T.S.N", "F.D.E.T", "F.D.E.T.N", "F.D.E.T.S.N", "I.E.T",
"I.E.T.S", "I.E.T.N", "I.E.T.S.N", "F.D.E.T", "F.D.E.T.N", "F.D.E.T.S.N"),
Errors = c("NO", "NO", "NO", "YES", "NO", "NO", "YES", "NO",
"NO", "NO", "NO", "NO", "NO", "NO", "NO", "NO", "NO", "YES",
"NO", "NO", "YES"),
Type = c("CF", "CF", "CF", "CF", "CF", "CF",
"CF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "MBF", "MBF",
"MBF", "MBF", "MBF", "MBF", "MBF"),
Mean_Loadings = c(0.534, 0.591, 0.554, 0.581, 0.552, 0.569, 0.596, 0.196, 0.39, 0.306,
0.411, 0.174, 0.343, 0.429, 0.185, 0.248, 0.271, 0.292, 0.21,
0.343, 0.429)),
class = "data.frame", row.names = c(NA, -21L))
我創建了這個情節:
dat$Model <- factor(dat$Model)
dat$Errors <- factor(dat$Errors, levels = c("NO", "YES"))
dat$Type <- factor(dat$Type, levels = c("CF", "MBF", "BF"))
ggplot(dat, aes(x = interaction(Model, Errors), y = Mean_Loadings, color = Type, fill = Type))
geom_rect(data=NULL, aes(xmin=7.5, xmax=Inf, ymin=-Inf, ymax=Inf), fill="#F9EBEA", color = "gray")
geom_bar(position = position_dodge2(width = 0.9, preserve = "single")
, stat="identity")
scale_x_discrete(limits = NULL)
theme_bw()
labs(title = "",
y = "Mean Loading",
x = "Models",
color = "Model Type",
fill = "Model Type")
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title=element_text(size=15),
text = element_text(size = 14), plot.margin = margin(20, 50, 10, 20),
axis.text.x=element_text(angle = -25, hjust = 0, size = 10),
axis.text.y=element_text(size = 20), axis.text=element_text(size=20))
theme(legend.position = "top")
如何做到以下幾點?
- 從 X 軸上顯示的模型名稱中洗掉“.NO”和“.YES”。還是有更好的方法來完成整個情節
x = interaction(Model, Errors)?最終目標是分離產生錯誤的正確模型(如錯誤列中所示)。 - 在右側紅色框的頂部中心添加一些文本。例如“有錯誤”
- 在繪圖右側添加一個文本框。
uj5u.com熱心網友回復:
首先,要洗掉 no 和 yes,您可以創建一個簡單的函式來提取標簽所需的部分。因此,在 中interaction,我添加_為分隔符,然后我們可以提取繼續進行的部分,將其添加scale_x_discrete為labels = make_labels。其次,要將文本添加到紅色區域,您只需使用annotate. 第三,對于區域外,我們可以再次使用annotate,但我們想添加一條額外的線以允許使用 繪制外部coord_cartesian。
library(tidyverse)
make_labels <- function(labels) {
result <- str_split(labels, "_")
unlist(lapply(result, function(x) x[1]))
}
ggplot(dat, aes(x = interaction(Model, Errors, sep = "_"), y = Mean_Loadings, color = Type, fill = Type))
geom_rect(data=NULL, aes(xmin=7.5, xmax=Inf, ymin=-Inf, ymax=Inf), fill="#F9EBEA", color = "gray")
geom_bar(position = position_dodge2(width = 0.9, preserve = "single")
, stat="identity")
scale_x_discrete(labels = make_labels, limits = NULL)
theme_bw()
labs(title = "",
y = "Mean Loading",
x = "Models",
color = "Model Type",
fill = "Model Type")
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title=element_text(size=15),
text = element_text(size = 14), plot.margin = margin(20, 80, 10, 20),
axis.text.x=element_text(angle = -25, hjust = 0, size = 10),
axis.text.y=element_text(size = 20), axis.text=element_text(size=20))
theme(legend.position = "top")
annotate("text", x=8.5, y=0.65, label= "With Errors",
family = "", fontface = 2, size=4)
annotate("text", x = 10, y = 0.65, label = "text",
family = "", fontface = 2, size=4)
coord_cartesian(ylim = c(0, 0.7), xlim = c(0, 9), clip = "off")
輸出

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