ループ内のggplotの問題が発生しています。個別にうまく機能します。
以下のように:
*plotgg<-
ggplot(renewalplot, aes(x = Month,y=Rate)) +
stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
labs(x = "Month") +
ggtitle("Rate Change Distribution")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
facet_grid(cols = vars(Year))
print(plotgg)*
それらをループに入れると、エラーが発生します。
vars <- colnames(detailinfo_renewal_1)
varslist1 = vars[c(13)]
for (i in varslist1) {
renewalplot <- detailinfo_renewal_1 %>%
filter(Product=="FI")
plotgg<-
ggplot(renewalplot, aes(x = renewalplot[, i],y=Rate)) +
stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
labs(x = i) +
ggtitle("Mean of Rate Change Distribution")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
facet_grid(cols = vars(Year))
print(plotgg)
}
とても感謝しています!LC
あなたは関数を書くことができます:
library(dplyr)
library(ggplot2)
plot_fn <- function(col) {
renewalplot <- detailinfo_renewal_1 %>% filter(Product=="FI")
ggplot(renewalplot, aes(x = .data[[col]],y=Rate)) +
stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
labs(x = col) +
ggtitle("Mean of Rate Change Distribution")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
facet_grid(cols = vars(Year))
}
を使用lapply
して、のすべての値に適用しますvarslist1
。
list_plot <- lapply(varslist1, plot_fn)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加