简体   繁体   English

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

[英]How to fix the error: could not find function "plot_grid"?

I'm try to do the Kaggle Project: Attrition in an Organization ||我正在尝试做Kaggle 项目:组织中的流失 || Why Workers Quit? 工人为什么辞职?

when I run a part of the code, which plot multiple graphs using R ggplot function "cowplot",当我运行一部分代码时,其中 plot 多个图形使用 R ggplot function “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)

I get the error:我收到错误:

> 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"

I googled the error but didn't find solution.我用谷歌搜索错误但没有找到解决方案。

what I'm after is that graph:我想要的是那个图表:

may any of you help in that?你们中有人可以帮忙吗?

在此处输入图像描述

Try installing "cowplot" and use the cowplot library.尝试安装“cowplot”并使用 cowplot 库。 This is what worked for me.这对我有用。

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

This is what worked for me.这对我有用。

Running your code, I was able to reproduce both of your plots without any problem (using the same dataset). 运行您的代码,我能够毫无问题地复制两个图(使用相同的数据集)。 Try modifying the objects within plot_grid as well since you shouldn't need to use plot. 尝试修改plot_grid中的对象,因为您不需要使用plot. in either case 在任一情况下

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

Also double check to make sure that cowplot has been installed successfully. 还要仔细检查以确保Cowplot已成功安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM