Oto przykład zakładając masz day.of.year() siewu i czas (w dniach) z trzech okresów dla każdej uprawy i dla każdego kraju.
#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))
Proszę podać przykład powtarzalne [] (http://stackoverflow.com/a/5963610/1412059), co próbowałeś. – Roland
odczytać jako dane.frame, podzielić według lokalizacji, utworzyć wykres dla każdego podzbioru – Dennis
Z pytaniem i połową odpowiedzi, która może być również pytaniem, trudno jest powiedzieć, co jest teraz twoje pytanie i czym jest nagroda dla. Może, jeśli odpowiedzi nie zawierają wystarczającej ilości szczegółów, przenieś swoją odpowiedź na swoje pytanie, aby ludzie mogli zobaczyć, co masz do tej pory. Następnie zadaj bardzo konkretne pytania? –