这是一个示例,假设您有播种的 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))