绘制作物日历 [英] Plotting crop calendars

查看:81
本文介绍了绘制作物日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件( crop_calendar.csv ),其中包含有关特定区域中作物生长阶段的信息。基本上,每行都具有以下结构:

I have a csv file (crop_calendar.csv) containing information on development stages of crop in a particular region. Basically each row has the following structure:

crop_name   sowing_dat    emergence_date  flowering_date  maturity_date  harvest_date

例如:

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 like to put that information in a graphic that looks like that:

有人知道如何在很多地方(不同行)种植大量作物(行)吗?

Any idea how to do it with lots of crop (rows) and at different locations?

推荐答案

下面是一个示例,假设您具有播种的day.of.year()和每个周期三个时期的持续时间(以天为单位)作物和每个国家。

Here is an example assuming you have the day.of.year() of sowing and the duration (in days) of the three periods for each crop and 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))

这篇关于绘制作物日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆