這裏是假設你有播種的每個作物和每個國家的三個時期的day.of.year()和持續時間(天)的例子。
#making random numbers reproducible
set.seed(12345)
rawdata <- expand.grid(
Crop = paste("Crop", LETTERS[1:8]),
Country = paste("Country", letters[10:13])
)
#day.of.year of sowing
rawdata$Sowing <- runif(nrow(rawdata), min = 0, max = 365)
#number of days until mid season
rawdata$Midseason <- runif(nrow(rawdata), min = 10, max = 30)
#number of days until harvest
rawdata$Harvest <- runif(nrow(rawdata), min = 20, max = 150)
#number of days until end of harvest
rawdata$Harvest.end <- runif(nrow(rawdata), min = 10, max = 40)
dataset <- data.frame(Crop = character(0), Country = character(0), Period = character(0), Duration = numeric(0))
#sowing around new year
last.day <- rowSums(rawdata[, c("Sowing", "Midseason")])
if(any(last.day >= 365)){
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Sowing",
Duration = last.day[last.day >= 365] - 365
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Mid-season",
Duration = rawdata$Harvest[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Harvest",
Duration = rawdata$Harvest.end[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = NA,
Duration = 365 - rowSums(rawdata[last.day >= 365, c("Midseason", "Harvest", "Harvest.end")])
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Sowing",
Duration = 365 - rawdata$Sowing[last.day >= 365]
)
)
rawdata <- rawdata[last.day < 365, ]
}
#mid-season around new year
last.day <- rowSums(rawdata[, c("Sowing", "Midseason", "Harvest")])
if(any(last.day >= 365)){
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Mid-season",
Duration = last.day[last.day >= 365] - 365
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Harvest",
Duration = rawdata$Harvest.end[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = NA,
Duration = 365 - rowSums(rawdata[last.day >= 365, c("Midseason", "Harvest", "Harvest.end")])
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Sowing",
Duration = rawdata$Midseason[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Mid-season",
Duration = 365 - rowSums(rawdata[last.day >= 365, c("Sowing", "Midseason")])
)
)
rawdata <- rawdata[last.day < 365, ]
}
#harvest around new year
last.day <- rowSums(rawdata[, c("Sowing", "Midseason", "Harvest", "Harvest.end")])
if(any(last.day >= 365)){
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Harvest",
Duration = last.day[last.day >= 365] - 365
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = NA,
Duration = 365 - rowSums(rawdata[last.day >= 365, c("Midseason", "Harvest", "Harvest.end")])
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Sowing",
Duration = rawdata$Midseason[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Mid-season",
Duration = rawdata$Harvest[last.day >= 365]
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[last.day >= 365, c("Crop", "Country")],
Period = "Harvest",
Duration = 365 - rowSums(rawdata[last.day >= 365, c("Sowing", "Midseason", "Harvest")])
)
)
rawdata <- rawdata[last.day < 365, ]
}
#no crop around new year
dataset <- rbind(
dataset,
cbind(
rawdata[, c("Crop", "Country")],
Period = NA,
Duration = rawdata$Sowing
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[, c("Crop", "Country")],
Period = "Sowing",
Duration = rawdata$Midseason
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[, c("Crop", "Country")],
Period = "Mid-season",
Duration = rawdata$Harvest
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[, c("Crop", "Country")],
Period = "Harvest",
Duration = rawdata$Harvest.end
)
)
dataset <- rbind(
dataset,
cbind(
rawdata[, c("Crop", "Country")],
Period = NA,
Duration = 365 - rowSums(rawdata[, c("Sowing", "Midseason", "Harvest")])
)
)
Labels <- c("", "Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Okt.", "Nov.", "Dec.")
Breaks <- cumsum(c(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31))
ggplot(dataset, aes(x = Crop, y = Duration, colour = Period, fill = Period)) + geom_bar(stat = "identity") + facet_wrap(~Country) + coord_flip() + scale_fill_manual(values = c("Sowing" = "darkgreen", "Mid-season" = "grey", "Harvest" = "yellow")) + scale_colour_manual(values = c("Sowing" = "black", "Mid-season" = "black", "Harvest" = "black"), guide = "none") + scale_y_continuous("", breaks = Breaks, labels = Labels, limits = c(0, 365)) + theme_bw() + theme(axis.text.x = element_text(hjust = 1))
請提供您嘗試過什麼是[重複的例子(http://stackoverflow.com/a/5963610/1412059)。 – Roland
將其作爲data.frame讀取,按位置分割,爲每個子集創建圖形 – Dennis
有了一個問題和半個答案,這也可能是一個問題,很難分辨您的問題現在是什麼以及這個賞金是什麼對於。也許如果答案沒有包含足夠的細節,請將自己的答案移到問題中,以便人們可以看到目前爲止所擁有的答案。然後問非常具體的問題? –