2016-09-21 58 views
4

我正在研究在循環中生成多面直方圖。當我在循環中調用facet_wrap時會出現問題。我嘗試不同的選擇,但它們都失敗,出現以下消息:facet_wrap for循環中的直方圖:「至少有一個圖層必須包含所有變量」

錯誤layout_base(數據,乏,降=滴):至少有一個層必須含有用於磨製

所有變量以下是一個可重複的例子。

library(ggplot2) 
library(scales) #date_format 

## Reproducible example 

datatest <- data.frame(
    column1 = sample(c("yes", "no"), 50, replace = TRUE), 
    column2 = sample(c("yes", "no"), 50, replace = TRUE), 
    column3 = sample(c("yes", "no"), 50, replace = TRUE), 
    column4 = sample(c("yes", "no"), 50, replace = TRUE) 
) 

#This function will generate a uniform sample of dates from 
#within a designated start and end date:  
rand.date=function(start.day,end.day,data){ 
    size=dim(data)[1]  
    days=seq.Date(as.Date(start.day),as.Date(end.day),by="day") 
    pick.day=runif(size,1,length(days)) 
    date=days[pick.day] 
} 

#This will create a new column within your data frame called date: 
datatest$date=rand.date("2016-01-01","2016-09-21",datatest) 

## Simple frequency plot that works well 
histotest <- ggplot(datatest, aes(x = date)) + 
    geom_histogram(binwidth = 7, fill="#2a87c8", colour="white") + 
    scale_x_date(limits = c(Sys.Date() - 250, NA), labels = date_format("%b %Y")) + 
    labs(x = "Period", y = "Count") + 
    facet_wrap(~ column1 , ncol=1) + 
    theme(plot.title=element_text(face="bold", size=9), 
     panel.grid.major = element_line(colour = "white"), 
     panel.grid.minor = element_blank()) 
ggsave("out/column1_histo.png", plot=histotest, width=12, height=6,units="in", dpi=300) 

這裏談到的問題:

## Same plot generated through a loop 
## The pb is with the facet_wrap 
for (i in 1:4) { 
    rm(variablename) 
    variablename <- names(datatest)[i] 
    ## histogramme to display event occurence over time 
    histoloop <- ggplot(datatest, aes(x = date)) + 
    geom_histogram(binwidth = 7, fill="#2a87c8", colour="white") + 
    scale_x_date(limits = c(Sys.Date() - 250, NA), labels = date_format("%b %Y")) + 
    labs(x = "Period", y = "Count") + 

    ## I tried different options but none of them is working 
    ## If I comment the facet_wrap everything's fine... 
    facet_wrap(~ variablename , ncol=1) + 
    #facet_wrap(~ names(datatest)[i] , ncol=1) + 
    #facet_wrap(~ aes_string(names(datatest)[i]) , ncol=1) + 
    theme(plot.title=element_text(face="bold", size=9), 
      panel.grid.major = element_line(colour = "white"), 
      panel.grid.minor = element_blank()) 
    ggsave(filename=paste("out/",variablename,"_histo.png",sep=""), plot=histoloop, width=12, height=6,units="in", dpi=300) 
} 

回答

1

變化如下(因爲VARIABLENAME是代碼一個字符串):

facet_wrap(as.formula(paste("~", variablename)) , ncol=1) 
0

密謀格式化數據,然後用分離的數據繪製在一個循環:

library(tidyr) #gather 

# prepare the data, wide to long 
plotDat <- gather(datatest, key = "Column", value = "Value", -c(date)) 

# then use loop, I prefer lapply 
lapply(split(plotDat, plotDat$Column), function(i){ 
    ggplot(i, aes(x = date)) + 
    geom_histogram(binwidth = 7, fill="#2a87c8", colour="white") + 
    scale_x_date(limits = c(Sys.Date() - 250, NA), labels = date_format("%b %Y")) + 
    labs(x = "Period", y = "Count") + 
    facet_wrap(~ Value , ncol = 1) + 
    theme(plot.title=element_text(face="bold", size=9), 
      panel.grid.major = element_line(colour = "white"), 
      panel.grid.minor = element_blank()) 
}) 
相關問題