[英]Day-Month versus year plot in R
I have a csv file containing the following lines: 我有一个包含以下行的csv文件:
V1 V2
1979 05/11/1979
1980 06/14/1980
1981 06/22/1981
1982 06/02/1982
1983 05/21/1983
I need to plot this as Day-Month in the Y-axis and Year in the X-axis 我需要将其绘制为Y轴中的Day-Month和X轴中的Year
I tried to do the following: 我尝试执行以下操作:
dat <-read.csv("test.csv",header=FALSE,sep=",")
dat$V3 <-as.Date(dat$V2,format="%m/%d/%y")
plot(dat$V1,dat$V3)
The output is Year vs Year which is not what I want. 输出是Year vs Year,这不是我想要的。 Can anyone provide any tips on how to plot something like this?
谁能提供有关如何绘制此类内容的提示?
The output is like this: 输出是这样的:
What I want should look like this: 我想要的应该是这样的:
I'll appreciate any help. 我将不胜感激。
Many thanks in advance 提前谢谢了
Not the most elegant way of dealing with this, but should work: 不是解决这个问题的最优雅的方法,但是应该可以:
df$date <- as.Date(df$V2, format="%m/%d/%Y")
df$mm <- format(df$date, "%m") %>% as.numeric
df$dd <- format(df$date, "%d") %>% as.numeric
df$V3 <- df$mm + df$dd/30
plot(df$V1, df$V3, type="l", yaxt="n", ylim=c(5, 7))
points(df$V1, df$V3, pch=16)
axis(2, at=5:7, labels=c("May", "June", "July"))
Data used: 使用的数据:
df <- structure(list(V1 = 1979:1983, V2 = structure(c(1L, 4L, 5L, 3L,
2L), .Label = c("05/11/1979", "05/21/1983", "06/02/1982", "06/14/1980",
"06/22/1981"), class = "factor"), date = structure(c(3417, 3817,
4190, 4535, 4888), class = "Date"), dm = c(5.11, 6.14, 6.22,
6.02, 5.21), mm = c(5, 6, 6, 6, 5), dd = c(11, 14, 22, 2, 21),
V3 = c(5.36666666666667, 6.46666666666667, 6.73333333333333,
6.06666666666667, 5.7)), .Names = c("V1", "V2", "date", "dm",
"mm", "dd", "V3"), row.names = c(NA, -5L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.