0

I have a csv file containing the following lines:

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

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. Can anyone provide any tips on how to plot something like this?

The output is like this:

Output image

What I want should look like this:

Desired image

I'll appreciate any help.

Many thanks in advance

Lyndz
  • 347
  • 1
  • 13
  • 30

1 Answers1

0

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

enter image description here

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")
Adam Quek
  • 6,973
  • 1
  • 17
  • 23