1

This is the data frame i am working with:

Month   HSI GSI K
Dec-12  1.703   0.516   0.315
Jan-13  1.841   0.441   0.316
Feb-13  NA  NA  NA
Mar-13  NA  NA  NA
Apr-13  NA  NA  NA
May-13  3.365   0.627   0.324
Jun-13  NA  NA  NA
Jul-13  NA  NA  NA
Aug-13  4.097   0.456   0.317
Sep-13  NA  NA  NA
Oct-13  2.582   0.977   0.336
Nov-13  3.728   1.178   0.352
Dec-13  2.211   3.937   0.352
Jan-14  1.617   1.163   0.336

I am trying to plot HSI, GSI and K on the same graph. I have no problem with that. However, my problem is that my curves are discontinuous due to the NA fields, and i am only getting the year on the x axis. This is what u an using:

library(zoo)     #to plot multiple lines in the graph
library(timeDate)     #to create a time series by month
tS = timeSequence(from = "2012-12-01", to = "2014-01-01", by = "month")
plot(tS,HSI, type="l",ann="False",ylim=c(1,5), pch=22, lty=1,lwd=2, col="red")
lines(tS,GSI,type="l",pch=22,lty=1,lwd=2, col="green")     #to add other lines
lines(tS,K, type="l",pch=22,lty=1,lwd=2, col="blue")

Any help please? Thank you

Henrik
  • 65,555
  • 14
  • 143
  • 159
Myr
  • 67
  • 1
  • 1
  • 11
  • Myr, welcome to Stackoverflow! Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for suggestions on how you could make your question easier to answer. – Andy Clifton Apr 04 '14 at 15:31
  • 1
    Why the downvote? First post on SO, with a small example data set, and the code that has been tried. _Far_ better than maaaany other posts on SO I would say. +1! – Henrik Apr 04 '14 at 16:00

2 Answers2

1

You may convert your data frame to zoo object and use the plot.zoo facilities:

library(zoo)
z <- zoo(df[ , c("HSI", "GSI", "K")], order.by = as.yearmon(df$Month, "%b-%y"))

plot(na.omit(z), plot.type = "single", col = c("red", "green", "blue"))

plot(na.omit(z), plot.type = "single", col = c("red", "green", "blue"), xaxt = "n")
axis(side = 1, at = index(z), labels = format(index(z), "%b-%y"))

enter image description here

You may also try ggplot alternatives:

library(scales)
library(ggplot2)

z2 <- fortify(z, melt = TRUE)

ggplot(data = na.omit(z2), aes(x = Index, y = Value, colour = Series)) +
  geom_line() +
  scale_x_yearmon(breaks = z2$Index, format = "%b %Y") +
  theme(axis.text.x  = element_text(angle = 45, vjust = 1, hjust = 1))

enter image description here

Another ggplot possibility:

# convert year-month dates to as.POSIXct
df$Month <- as.POSIXct(as.Date(paste0(df$Month, "-01"), "%b-%y-%d"))

# reshape data from wide to long
library(reshape2)
df2 <- melt(df, id.var = "Month")

ggplot(data = na.omit(df2), aes(x = Month, y = value, colour = variable)) +
  geom_line()

enter image description here

When you have dates in as.POSIXct format you can easily format labels using scale_x_datetime: breaks = date_breaks and labels = date_format, e.g.

ggplot(data = na.omit(df2), aes(x = Month, y = value, colour = variable)) +
  geom_line() +
  scale_x_datetime(breaks = date_breaks("2 months"),
                   labels = date_format("%Y-%m"))

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • GREAT! i am sorry i cannot vote for your answers as my rep is still below 15:) – Myr Apr 04 '14 at 17:12
  • @Myr, I have updated my answer with some `ggplot` alternatives. – Henrik Apr 04 '14 at 22:41
  • I need some help. It is required from me to plot a right axis with a smaller scale to show the "K" of the same data. I cannot modify the script to do so. Any idea? – Myr Jul 21 '14 at 13:52
  • If I understand you correctly, you wish a plot with two separate y scales. In `ggplot2`, this is not possible by design. See e.g. the answer [**here**](http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-plot-with-2-y-axes-one-y-axis-on-the-left-and-another) by Hadley Wickham, the author of `ggplot2`. However, there are [**work-arounds**](http://rpubs.com/kohske/dual_axis_in_ggplot2). Good luck! – Henrik Jul 21 '14 at 13:59
  • No not by ggplot, but by using the first method you proposed using zoo. – Myr Jul 21 '14 at 14:00
  • Sorry, I don't know. But I have seen many of Q&As on SO with `base` R solutions. – Henrik Jul 21 '14 at 14:08
  • Thank you @Henrik. I will check them out – Myr Jul 21 '14 at 14:09
  • 1
    You may also check `twoord.plot` in the `plotrix` package. But see the **Note** section - similar arguments as in the SO link I posted above... – Henrik Jul 21 '14 at 14:13
0

You can use complete.cases as follows. Let t be your data.frame:

x <- complete.cases(t)
plot(tS[x],t$HSI[x], type="l",ann="False",ylim=c(0,5), pch=22, lty=1,lwd=2, col="red")
lines(tS[x],t$GSI[x],type="l",pch=22,lty=1,lwd=2, col="green")     #to add other lines
lines(tS[x],t$K[x], type="l",pch=22,lty=1,lwd=2, col="blue")

Note that I changed the ylim in the call to plot because the blue line was outside the plot limits.

enter image description here

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • This is great. Thanks a lotttt! but any idea on how i can add the months to the x axis instead of the year! – Myr Apr 04 '14 at 16:25