1

I need to decompose my time series using the function decompose() but I have to change some of the aesthetics of the graphic in order to fit in my work. I need the variables (trend, seasonal, random,...) to be on an opposite side of the scales for example. Is there any function that I could use? Thank you!

ts <- ts(daily2018$UI, frequency = 365.25, start = c(2012,1,1))
d<-decompose(ts)
plot(d)

My graphic looks like this: enter image description here

and some of my daily2018 is:(I could not post the entire dataset)

structure(list( Ano = c(2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012), UI = c(18.0647497177124, 51.0355095863342, 47.5402774810791, 
340.893737792969, 247.539836883545, 399.510726928711, 122.39359664917, 
98.836296081543, 35.0952081680298, 50.6278982162476, 87.2319049835205, 
77.6166572570801, 112.672420501709, 2.67568874359131, -392.484573364258, 
618.681500434875, 692.416625976562, 236.788291931152, 51.255313873291, 
71.8802337646484, 345.232437133789, 236.85432434082, -0.00471636652946472, 
-27.9389114379883, 146.920881986618, 341.540222167969, 1208.46826171875, 
454.171142578125, 94.2786865234375, -23.250904083252, 6.60693836212158, 
11.3990550041199, 461.585052490234, 110.249738693237, 282.145706176758, 
30.0083067417145, 922.615142822266, 665.348541259766, 280.90966796875, 
129.148958206177, 97.8483505249023, 151.280666351318, 41.1154479980469, 
462.513458251953, 223.375587463379, 243.899364471436, 91.236083984375, 
-33.4530096054077, 419.289459228516, 216.38060760498, 223.88525390625, 
42.9771499633789, 63.462251663208, 135.543815612793, 103.11067199707, 
-2.97302103042603, 50.1539402008057, 31.7654695510864, 4.74521112442017, 
116.475090026855, -31.0308532714844, 95.1957511901855, -74.0424919128418, 
74.0312874317169, 370.580841064453, 439.345306396484, 288.222801208496, 
245.310737609863, 163.978561401367, -43.6043725013733, 90.422176361084, 
13.5707854032516, 35.8152360916138, -251.285369873047, -396.40592956543, 
27.4398317337036, 119.581299781799, 374.314788818359, 895.551239013672, 
583.116119384766, 83.0863494873047, -162.666477203369, -326.430221557617, 
-219.034694671631, 246.566764831543, 142.276351928711, 167.783622741699, 
116.547183990479, -962.846618652344, -199.737373352051, 581.776489257812, 
127.202213287354, 243.02904510498, 546.869003295898, 322.698364257812, 
416.055419921875, -35.2042512893677, 4.76407432556152, 59.2325496673584, 
109.642734527588, 39.2587299346924, 70.3620586395264, 3161.94677734375, 
1006.70434570312, 2140.01275634766, 2786.77355957031, 1705.16961669922, 
944.830413818359, 655.952301025391, 338.197479248047, 63.7034378051758, 
519.655364990234, 658.758819580078, 443.135345458984, 365.920120239258, 
-533.251229286194, 514.586601257324, 67.87255859375, -295.31233215332, 
175.399646759033, 27.4736328125, -3460.8505859375, -2083.35198974609, 
-1276.77111816406, -203.34831237793, 121.706581115723, -1469.41851806641, 
-224.111042022705, 191.504776000977, 172.109447479248, 26.7028465270996, 
113.872447013855, 115.648100852966, 365.883389472961, 2044.16656494141, 
-32.7207946777344, 24.2001395225525, 1416.10055541992, -49.864501953125, 
953.696166992188, 1353.91192626953, 331.314163208008, 755.130172729492, 
504.707885742188, 1100.87829589844, 410.127822875977, 1059.75042724609, 
1814.80114746094, 732.802597045898, 2145.48461914062, 2912.95367431641, 
210.142471313477, 39.4968557357788, 380.054672241211, 278.151397705078, 
1334.32025146484, 681.097854614258, -69.2400665283203, 567.077270507812, 
624.232055664062, 391.584182739258, 586.699615478516, 1279.32067871094, 
3932.2900390625, 789.148895263672, 474.529556274414, 316.368942260742, 
589.92162322998, 107.363967418671, 3477.34375, 241.996397972107, 
350.299514770508, 3326.77624511719, 2569.17126464844, 4582.50708007812, 
1923.22076416016, -61.1435556411743, -85.2687129974365, 217.463203430176, 
517.184478759766, 172.391616821289, 1056.78869628906, 2622.95007324219, 
2552.87609863281, 1118.66555786133, 1658.28143310547, 1830.66302490234, 
1645.97857666016, 874.851470947266, 1195.69207763672, 771.39143371582, 
1265.49255371094, 1729.88415527344, 4629.59558105469, 3249.35119628906, 
2862.19885253906, 2368.9833984375, 380.631881713867, 276.73908996582, 
5071.85693359375), Date = structure(c(1325376000, 1325462400, 
1325548800, 1325635200, 1325721600, 1325808000, 1325894400, 1325980800, 
1326067200, 1326153600, 1326240000, 1326326400, 1326412800, 1326499200, 
1326585600, 1326672000, 1326758400, 1326844800, 1326931200, 1327017600, 
1327104000, 1327190400, 1327276800, 1327363200, 1327449600, 1327536000, 
1327622400, 1327708800, 1327795200, 1327881600, 1327968000, 1328054400, 
1328140800, 1328227200, 1328313600, 1328400000, 1328486400, 1328572800, 
1328659200, 1328745600, 1328832000, 1328918400, 1329004800, 1329091200, 
1329177600, 1329264000, 1329350400, 1329436800, 1329523200, 1329609600, 
1329696000, 1329782400, 1329868800, 1329955200, 1330041600, 1330128000, 
1330214400, 1330300800, 1330387200, 1330473600, 1330560000, 1330646400, 
1330732800, 1330819200, 1330905600, 1330992000, 1331078400, 1331164800, 
1331251200, 1331337600, 1331424000, 1331510400, 1331596800, 1331683200, 
1331769600, 1331856000, 1331942400, 1332028800, 1332115200, 1332201600, 
1332288000, 1332374400, 1332460800, 1332547200, 1332633600, 1332720000, 
1332806400, 1332892800, 1332979200, 1333065600, 1333152000, 1333238400, 
1333324800, 1333411200, 1333497600, 1333584000, 1333670400, 1333756800, 
1333843200, 1333929600, 1334016000, 1334102400, 1334188800, 1334275200, 
1334361600, 1334448000, 1334534400, 1334620800, 1334707200, 1334793600, 
1334880000, 1334966400, 1335052800, 1335139200, 1335225600, 1335312000, 
1335398400, 1335484800, 1335571200, 1335657600, 1335744000, 1335830400, 
1335916800, 1336003200, 1336089600, 1336176000, 1336262400, 1336348800, 
1336435200, 1336521600, 1336608000, 1336694400, 1336780800, 1336867200, 
1336953600, 1337040000, 1337126400, 1337212800, 1337299200, 1337385600, 
1337472000, 1337558400, 1337644800, 1337731200, 1337817600, 1337904000, 
1337990400, 1338076800, 1338163200, 1338249600, 1338336000, 1338422400, 
1338508800, 1338595200, 1338681600, 1338768000, 1338854400, 1338940800, 
1339027200, 1339113600, 1339200000, 1339286400, 1339372800, 1339459200, 
1339545600, 1339632000, 1339718400, 1339804800, 1339891200, 1339977600, 
1340064000, 1340150400, 1340236800, 1340323200, 1340409600, 1340496000, 
1340582400, 1340668800, 1340755200, 1340841600, 1340928000, 1341014400, 
1341100800, 1341187200, 1341273600, 1341360000, 1341446400, 1341532800, 
1341619200, 1341705600, 1341792000, 1341878400, 1341964800, 1342051200, 
1342137600, 1342224000, 1342310400, 1342396800, 1342483200, 1342569600
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
200L), class = "data.frame")
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
macs2021
  • 85
  • 1
  • 7

1 Answers1

0

Your example isn't reproducible with the data/code you posted, specifically Error in decompose(ts) : time series has no or less than 2 periods. Here is something that might be useful to you though. I'm not sure if this is what you meant by "I need the variables (trend, seasonal, random,...) to be on an opposite side of the scales"...

EDIT: In response to your comment, you can convert a decomposed.ts object to a dataframe using the dts2 function from the ggplottimeseries package. I implemented some of the code from that function below.

co2_decomp <- decompose(co2)

#-------------- from the ggplottimeseries package ----------------------#
df <- as.data.frame(cbind(observation = as.numeric(co2_decomp$x),
                         trend = as.numeric(co2_decomp$trend),
                         seasonal = as.numeric(co2_decomp$seasonal),
                         random = as.numeric(co2_decomp$random)))
df2 <- reshape2::melt(df)
df2$date <- rep(lubridate::date_decimal(as.numeric(time(co2))), ncol(df))
#------------------------------------------------------------------------#

library(ggplot2)
ggplot(df2, aes(x = date, y = value)) + 
    geom_line() + 
    facet_wrap(~variable, ncol = 1, scales = 'free_y', strip.position = 'right') + 
    ggthemes::theme_base() + # optional
    theme(plot.background = element_blank()) # optional

enter image description here

Snipeskies
  • 258
  • 1
  • 5
  • Sorry for the poor explanation, but yes, this graphic is exactly how I need it to look! However I can't use ggplot because my data won't be a dataframe. It is a decomposed.ts object. – macs2021 Apr 27 '21 at 09:10
  • @macs2021 you could convert your data to a data.frame, see edit – Snipeskies Apr 27 '21 at 16:33