如何修复错误:找不到函数“plot_grid”?

问题描述 投票:0回答:3

我试着做 Kaggle 项目:组织中的流失 ||工人为什么辞职?

当我运行一部分代码时,它使用 R ggplot 函数“cowplot”绘制多个图形,

library(ggplot2)

options(repr.plot.width=8, repr.plot.height=6)
options(warn=-1)


df <- read.csv("C:/Users/Abdo Taha/Documents/WA_Fn-UseC_-HR-Employee-Attrition.csv")

head(df)

original_df <- df


attritions_number <-
df %>% group_by(Attrition) %>% summarise(Count = n()) %>%
ggplot(aes(x = Attrition, y = Count)) + geom_bar(stat = "identity", fill =
                                                  "orange", color = "grey40") + theme_bw() + coord_flip() +
geom_text(
 aes(x = Attrition, y = 0.01, label = Count),
 hjust = -0.8,
 vjust = -1,
 size = 3,
 colour = "black",
 fontface = "bold",
 angle = 360
) + labs(title = "Employee Attrition (Amount)", x = "Employee Attrition", y =
          "Amount") + theme(plot.title = element_text(hjust = 0.5))

attrition_percentage <-
df %>% group_by(Attrition) %>% summarise(Count = n()) %>%
mutate(pct = round(prop.table(Count), 2) * 100) %>%
ggplot(aes(x = Attrition, y = pct)) + geom_bar(stat = "identity", fill = "dodgerblue", color =
                                                "grey40") +
geom_text(
 aes(
   x = Attrition,
   y = 0.01,
   label = sprintf("%.2f%%", pct)
 ),
 hjust = 0.5,
 vjust = -3,
 size = 4,
 colour = "black",
 fontface = "bold"
) + theme_bw() + labs(x = "Employee Attrition", y = "Percentage") +
labs(title = "Employee Attrition (%)") + theme(plot.title = element_text(hjust =
                                                                          0.5))

plot_grid(plot.attritions_number,
       plot.attrition_percentage,
       align = "h",
       ncol = 2)

我得到错误:

> plot_grid(plot.attritions_number,
+           plot.attrition_percentage,
+           align = "h",
+           ncol = 2)
Error in plot_grid(plot.attritions_number, plot.attrition_percentage,  : 
  could not find function "plot_grid"

我用谷歌搜索错误但没有找到解决方案。

我要的是那个图表:

你们中有人可以帮忙吗?

r ggplot2 cowplot
3个回答
1
投票

运行你的代码,我能够毫无问题地重现你的两个图(使用相同的数据集)。也尝试修改 plot_grid 中的对象,因为在任何一种情况下都不需要使用

plot.

plot_grid(attritions_number,
   attrition_percentage,
   align = "h",
   ncol = 2)

还要仔细检查以确保 cowplot 已成功安装。


1
投票

尝试安装“cowplot”并使用 cowplot 库。 这对我有用。

install.packages('cowplot')
library('cowplot')

这对我有用。


0
投票

如果你确定你安装了并且还使用了,那么使用如下:

cowplot::plot_grid()

© www.soinside.com 2019 - 2024. All rights reserved.