0

I have seen the graphs that the function decompose produces in R. They have the same values on the Y axis and different values in the graphs in the X axis. I want to create the same kind of graphs.

I have a dataframe screenshot for it in the R studio that has multiple columns of data, say for rainfall. SO there is one column for Year, the others are for Jan-Dec and there are other columns(Like Annual, JF, MAM, JJAS, OND) that are basically the sum of the some particular columns. Ex: Annual is the sum of all months, JF is the sum of January February, etc.

I need to make a plot that looks like this Plot where the first graph gives an annual vs time graph, the second graph gives a jf vs time, third: mam vs time, fourth: jjas vs time, fifth: ond vs time.

Thanks in advance :)

EDIT: The reproducible code for the data set is:

structure(list(YEAR = 1901:1904, JAN = c(28.1, 9.9, 22.8, 10.6
), FEB = c(22.1, 11.9, 26.4, 54.9), MAR = c(31.1, 108, 101.9, 
57.6), APR = c(230.2, 341.6, 151.7, 450.2), MAY = c(186.5, 236.9, 
200.6, 358), JUN = c(460, 511.6, 498.1, 367.1), JUL = c(375.9, 
430.8, 317.8, 453.8), AUG = c(455.2, 487.6, 498.7, 410), SEP = c(256.7, 
419.3, 307.8, 232.6), OCT = c(117.4, 93, 145.3, 109.7), NOV = c(97.8, 
8.2, 42.8, 47.3), DEC = c(1.4, 1.4, 1.5, 2.5), ANNUAL = c(2262.4, 
2660.4, 2315.5, 2554.4), JF = c(50.2, 21.8, 49.2, 65.6), MAM = c(447.8, 
686.5, 454.3, 865.7), JJAS = c(1547.8, 1849.4, 1622.4, 1463.6
), OND = c(216.6, 102.7, 189.6, 159.5)), .Names = c("YEAR", "JAN", 
"FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", 
"NOV", "DEC", "ANNUAL", "JF", "MAM", "JJAS", "OND"), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

The code that I currently use to produce the graph is:

year <- ggplot(rainfall, aes(x = YEAR, y = ANNUAL)) + geom_line(size = 1.0, colour = "blue") + labs(x = "Year", y = "Rainfall in mm") + geom_smooth(method = "lm", se = FALSE, color = "red") + ggtitle("Collective rainfall for the Year")
jf <- ggplot(rainfall, aes(x = YEAR, y = JF)) + geom_line(size = 1.0, colour = "first line") + labs(x = "Year", y = "Rainfall in mm") + geom_smooth(method = "lm", se = FALSE, color = "red") + ggtitle("Collective rainfall for January February")
mam <- ggplot(rainfall, aes(x = YEAR, y = MAM)) + geom_line(size = 1.0, colour = "blue") + labs(x = "Year", y = "Rainfall in mm") + geom_smooth(method = "lm", se = FALSE, color = "red") + ggtitle("Collective rainfall for March April May")
jjas <- ggplot(rainfall, aes(x = YEAR, y = JJAS)) + geom_line(size = 1.0, colour = "blue") + labs(x = "Year", y = "Rainfall in mm") + geom_smooth(method = "lm", se = FALSE, color = "red") + ggtitle("Collective rainfall for June July August September")
ond <- ggplot(rainfall, aes(x = YEAR, y = OND)) + geom_line(size = 1.0, colour = "blue") + labs(x = "Year", y = "Rainfall in mm") + geom_smooth(method = "lm", se = FALSE, color = "red") + ggtitle("Collective rainfall for October November December")

Then I used the code for the multiplot function from the R cookbookMultiplot function code and used this code to produce the graph I currently have:

multiplot(year, jf, mam, jjas, ond, cols = 1)

But the issue is that I get separate graphs in the same plot, It doesn't look like the decompose graph. How do I get one that looks like the decompose graph. Finally, also how to get the slope for the regression line written in the graphs?

Did I ask it right? I'm trying. :)

  • 1
    you mean mechanically what's going on? – MichaelChirico Jul 02 '17 at 20:00
  • I want to know how to create a graph like the one I described @MichaelChirico – Kaushal Gautam Jul 02 '17 at 20:11
  • 2
    it seems you were already able to produce such a graph? – MichaelChirico Jul 02 '17 at 20:49
  • If you want help with code, [you should make a reproducible example](https://stackoverflow.com/q/5963269/903061), sharing data with `dput` or via simulation. Images of data are useless. Also please clarify your question: if you want a plot like `decompose` makes, why not use `decompose`? – Gregor Thomas Jul 03 '17 at 02:47
  • I was able to produce a decompose graph, but I don't want a decompose graph. I want a graph that looks like a decompose graph. Hold on, I will provide more details by updating the question. @MichaelChirico – Kaushal Gautam Jul 03 '17 at 07:41

0 Answers0