简体   繁体   English

如何在 R 中将 plot 日历日期作为 y 轴和年份作为 x 轴?

[英]How to plot calender dates with day of year as y axis and years as x axis in R?

I have a data set which describes the start, maximum and end of a vegetation season as day of year over 18 years like this:我有一个数据集,它将植被季节的开始、最大和结束描述为 18 年以上的一年中的一天,如下所示:

Year   Day of Green  Var Green  Day Max  Day Senesc  Var Senesc  Veg Length
2000   111           4          137      253         11          142
2001   115           5          158      252         19          137
2002   110           4          136      263         10          153
2003   112           3          143      271         16          159
2004   105           4          142      279         13          174
2005   106           5          156      278         11          172

Now I want a plot, which shows the years as xaxis and a yaxis with Day of Year(DOY).现在我想要一个 plot,它将年份显示为 xaxis,yaxis 显示年份(DOY)。 So I can pinpoint days of greening, maximum greening and browning (senescence).所以我可以确定绿化、最大绿化和褐变(衰老)的日子。

Thank you for your ideas.谢谢你的想法。

Not sure what issues you're running into but here's a way to get that plot using ggplot :不确定您遇到了什么问题,但这是一种使用ggplot获得 plot 的方法:

DATA数据

structure(list(Year = 2000:2005, Day.of.Green = c(111L, 115L, 
110L, 112L, 105L, 106L), Var.Green = c(4L, 5L, 4L, 3L, 4L, 5L
), Day.Max = c(137L, 158L, 136L, 143L, 142L, 156L), Day.Senesc = c(253L, 
252L, 263L, 271L, 279L, 278L), Var.Senesc = c(11L, 19L, 10L, 
16L, 13L, 11L), Veg.Length = c(142L, 137L, 153L, 159L, 174L, 
172L)), class = "data.frame", row.names = c(NA, -6L))

CODE代码

library(ggplot2)
ggplot(dat, aes(x = Year, y = Day.of.Green)) + geom_point() + geom_line()

OUTPUT OUTPUT 在此处输入图像描述

If you want to add multiple columns, you can do that with more geom_point or geom_line statements which add points and lines, respectively, as the name suggests:如果要添加多列,可以使用更多geom_pointgeom_line语句来分别添加点和线,顾名思义:

ggplot(dat, aes(x = Year)) + 
  geom_point(aes(y = Day.of.Green), color = 'dark red') + geom_line(aes(y = Day.of.Green), color = 'dark red') + 
  geom_point(aes(y = Day.Senesc), color = 'black') + geom_line(aes(y = Day.Senesc), color = 'black') + 
  xlab('Year') + ylab('Day of Year')

在此处输入图像描述

You can look at ggplot options to get different symbols, colors, plot types, trend-lines etc.您可以查看ggplot选项以获取不同的符号、colors、plot 类型、趋势线等。

This draws a line segment for each year and identifies the maximum point.这会为每年绘制一条线段并确定最大点。 (The question was not clear on which column is which so you may need to modify the column names in the code.) (问题不清楚哪一列是哪一列,因此您可能需要修改代码中的列名。)

library(ggplot2)

ggplot(DF, aes(Year, `Day Max`)) +
  geom_pointrange(aes(ymin = `Day of Green`, ymax = `Day Senesc`)) +
  ylab("Day")

giving给予

截屏

Note笔记

The input in reproducible form is assumed to be:假设可重现形式的输入为:

Lines <- 
"Year   \"Day of Green\"  \"Var Green\"  \"Day Max\"  \"Day Senesc\"  \"Var Senesc\"  \"Veg Length\"
2000   111           4          137      253         11          142
2001   115           5          158      252         19          137
2002   110           4          136      263         10          153
2003   112           3          143      271         16          159
2004   105           4          142      279         13          174
2005   106           5          156      278         11          172"
DF <- read.table(text = Lines, header = TRUE, check.names = FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM