循環多個ggplots我有這個對於R中
Product <- c("X","Y","Z")
Var1 <- runif(3)
Var2 <- rnorm(3)
Var3 <- rnorm(3)
df <- data.frame(Product,Var1,Var3,Var2)
bar.plot <- function(dat,k,p,this) {
mytitle <- paste("Topic:",as.character(this))
ggplot(dat, aes_string(x = substitute(k), y = substitute(p))) +
geom_bar(position = "dodge",stat = "identity",fill="lightblue", colour="black") +
theme(plot.title=element_text(size=25,face="bold"), axis.text.y = element_text(size=20),axis.title.y = element_blank(),axis.text.x = element_text(size = 10,angle = 30, hjust = 1)) +
labs(title=mytitle)
}
我想以下在df
返回我的每個列的三個地塊。它並沒有。
col <- c(Var1,Var2,Var3)
for(i in col){
bar.plot(df,Product,i,"Data")
}
任何想法,爲什麼這可能是?謝謝。
檢查「col」變量的內容。您應該將「Product」,「Var1」等作爲字符傳遞給ggplot函數調用。 – DrDom
你期望將他們歸還給誰?你似乎沒有試圖將它們存儲在任何地方。 'for'循環不返回值。 – MrFlick