5

crop_calendar.csv特定の地域の作物の発育段階に関する情報を含む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

ここで、その情報を次のようなグラフィックに入れたいと思います。 作物カレンダーの例

たくさんの作物 (行) と異なる場所でそれを行う方法はありますか?

4

4 に答える 4

6

これは、各作物と各国について、播種の day.of.year() と 3 つの期間の期間 (日数) があると仮定した例です。

収穫カレンダー

#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))
于 2013-08-12T12:44:51.467 に答える
1

あなたが何をしたいのかを推測するのは少し難しいです。3 つの日付だけでは、表示するグラフを再現することはできません (作物ごとに 4 つの日付が必要です)。また、数字が何を表しているかも明確ではありません (おそらく数週間?)。プロットに関する単なる質問であれば、これで始められます。それ以外の場合は、質問を明確にしてください。

df <- read.table(text="crop_name   emergence_date  maturity_date  harvest_date
                 wheat        13.04           25.05          30.06
                 corn         12.02           21.30          23.11", header=TRUE)
require(ggplot2)
ggplot(df, aes(x=crop_name)) +
  geom_linerange(aes(ymin=emergence_date, ymax=maturity_date), color="green3", size=5) +
  geom_linerange(aes(ymin=maturity_date, ymax=harvest_date), color="yellow", size=5) +
  coord_flip() + ylim(0, 52)
于 2013-07-23T10:31:26.130 に答える