gpt4 book ai didi

r - 绘制裁剪日历

转载 作者:行者123 更新时间:2023-12-04 12:34:06 25 4
gpt4 key购买 nike

我有一个 csv 文件( crop_calendar.csv ),其中包含有关特定地区裁剪发育阶段的信息。基本上每一行都具有以下结构:

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

现在,我想把这些信息放在一个看起来像这样的图形中:
crop calendar example

知道如何在不同位置处理大量裁剪(行)吗?

最佳答案

这是一个示例,假设您有播种的 day.of.year() 以及每种裁剪和每个国家的三个时期的持续时间(以天为单位)。

The crop calendar

#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))

关于r - 绘制裁剪日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com