r - Plotting crop calendars -
i have csv file (crop_calendar.csv
) containing information on development stages of crop in particular region. each row has following structure:
crop_name sowing_dat emergence_date flowering_date maturity_date harvest_date
which gives example:
winter_wheat 18.08 28.08 24.06 30.07 3.08 winter_rye 18.08 28.08 15.06 23.07 29.07 spring_wheat 27.04 10.05 1.07 4.08 7.08 spring_barley 27.04 12.05 27.06 1.08 5.08
now, i'd put information in graphic looks that:
any idea how lots of crop (rows) , @ different locations?
here example assuming have day.of.year() of sowing , duration (in days) of 3 periods each crop , each country.
#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))
Comments
Post a Comment