【发布时间】:2021-05-09 12:37:34
【问题描述】:
当我在 R 中创建下面的条形图时,没有问题,但是当我将这些代码转换为闪亮的函数时,出现了问题,你能帮忙吗?
普通 R 代码中的条形图:
path <- "WA_Fn-UseC_-HR-Employee-Attrition.csv (job attrition)"
data <-fread(path)
# bar chart function function
Categorical_vs_categorical_plot_2 <- function(data,group_col,fill_col){
data %>%
ggplot(aes_(x = fill_col, group = group_col)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = 2) +
labs(y = "Percentage", fill= "Education") +
facet_grid(~Attrition) +
theme_minimal()+
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition")
}
Categorical_vs_categorical_plot_2(data,~Attrition,~BusinessTravel)
条形图转换为闪亮代码[发生错误,不像上面的普通条形图那样显示 facet_grid 和文本值]:
output$cat_vs_cat_chart2 <- renderPlot({
data() %>%
#ggplot(aes_(x = input$cat_compare, group = ~Attrition)) +
ggplot(aes_(x = 'BusinessTravel', group = ~Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = 2) +
#labs(y = "Percentage", fill= "Education") +
facet_grid(~Attrition) +
theme_minimal()+
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition")
})
【问题讨论】:
-
请将您自己的解决方案作为答案发布(并标记为已接受),不要将其添加到问题中。
-
谢谢,我的答案如下!