0

This is driving me nuts. I have an xts object:

> head(sec)
                          y1       y2       y3       y4       y5       y6       y7       y8       y9      y10      y11
2018-06-18 17:00:00 57.24548 49.45735 72.30936 26.78655 71.70350 70.40394 49.59040 30.39259 48.57300 83.48015 110.2542
2018-06-19 17:00:00 57.05952 50.07112 72.62863 26.71799 71.75246 70.55178 49.63896 30.72064 48.61171 83.65722 110.7769
2018-06-20 17:00:00 56.45272 49.76424 71.28383 26.63964 70.85164 70.00969 49.73608 30.90396 48.77624 83.17519 109.9878
2018-06-21 17:00:00 57.27484 49.98203 72.70603 26.51232 71.09642 69.78300 50.14399 31.17412 49.11496 83.54901 109.8004
2018-06-24 17:00:00 56.38420 48.95250 71.24513 26.22830 70.19559 68.33411 50.39651 31.09693 49.92790 82.78169 107.4135
2018-06-25 17:00:00 56.59952 49.03367 72.14489 26.14015 70.45997 68.61008 50.18284 31.26096 49.98597 82.52592 108.1828

I wanted a barplot so I did this:

barplot(sec, border = NA, col = brewer.pal(11, name = "Set3"))

which gave me this:

enter image description here

I want to reproduce this plot (100% stacked) using ggplot2, but I've tried everything I could think of including

I have tried and adjusted ggplot so many times (example below), but keep getting the same error:

> ggplot(sec, aes(x = index(sec), y = coredata(sec))) + geom_bar()
Error: Aesthetics must be either length 1 or the same as the data (6): y

Do I need to rescale or change data types from xts to something else?
If not, then how?

Thanks.

UPDATE

Here's the dput and str for head(sec):

> str(sec)
An ‘xts’ object on 2018-06-18 17:00:00/2019-07-16 17:00:00 containing:
  Data: num [1:271, 1:11] 57.2 57.1 56.5 57.3 56.4 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:11] "y1" "y2" "y3" "y4" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
List of 1
 $ na.action: 'omit' int [1:2885] 1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "index")= num [1:2885] 1.17e+09 1.17e+09 1.17e+09 1.17e+09 1.17e+09 ...

> dput(head(sec))
    structure(c(57.24548, 57.059517, 56.452717, 57.274837, 56.384201, 
    56.599518, 49.457355, 50.071121, 49.764236, 49.982029, 48.952496, 
    49.033669, 72.309357, 72.628632, 71.283829, 72.706032, 71.245132, 
    72.14489, 26.786551, 26.717995, 26.639641, 26.51232, 26.228296, 
    26.14015, 71.703499, 71.752457, 70.851639, 71.09642, 70.195595, 
    70.459969, 70.403938, 70.551781, 70.009689, 69.782997, 68.334106, 
    68.610085, 49.590397, 49.638962, 49.73608, 50.143993, 50.396507, 
    50.182838, 30.392593, 30.720638, 30.903957, 31.17412, 31.09693, 
    31.260956, 48.573002, 48.611713, 48.776241, 49.11496, 49.927898, 
    49.985966, 83.480148, 83.657219, 83.175194, 83.549011, 82.781693, 
    82.525917, 110.254166, 110.776917, 109.987839, 109.800438, 107.413475, 
    108.182823), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
    "POSIXt"), tclass = c("POSIXct", "POSIXt"), tzone = "", na.action = structure(1:2885, class = "omit", index = c(1167782400, 
    1167868800, 1167955200, 1168214400, 1168300800, 1168387200, 1168473600, 
    1168560000, 1168905600, 1168992000, 1169078400, 1169164800, 1169424000, 
    1169510400, 1169596800, 1169683200, 1169769600, 1170028800, 1170115200, 
    1170201600, 1170288000, 1170374400, 1170633600, 1170720000, 1170806400, 
    1170892800, 1170979200, 1171238400, 1171324800, 1171411200, 1171497600, 
    ...
    1528848000, 1528934400, 1529020800, 1529280000)), index = c(1529366400, 
    1529452800, 1529539200, 1529625600, 1529884800, 1529971200), .Dim = c(6L, 
    11L), .Dimnames = list(NULL, c("y1", "y2", "y3", "y4", "y5", 
    "y6", "y7", "y8", "y9", "y10", "y11")))

The output is too long for me to completely upload to this forum.

Cinji18
  • 619
  • 8
  • 22
  • 2
    "I've tried everything" – please give us exact code that you tried to use. Optimally, create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – January Jul 19 '19 at 21:54
  • 2
    Could you include usable copy of your data with dput() so that we can try to graph these data? – adts Jul 19 '19 at 22:08
  • What's dput and why is it necessary?` – Cinji18 Jul 19 '19 at 23:20
  • R objects can vary in the way they store data in ways that you can't see by sharing the output of printing the object (for example, data types character vs. factor vs. numeric). If you run `dput(head(sec))`, R will output code that reproduces your data in exactly the form you have it. Sharing this in your question can make it easier for people to help you. – Jon Spring Jul 20 '19 at 05:47
  • See here for similar questions that might help: https://stackoverflow.com/questions/52789537/plotting-wide-format-data-using-r-ggplot https://stackoverflow.com/questions/11918080/how-to-create-a-stacked-bar-chart-from-summarized-data-in-ggplot2 https://stackoverflow.com/questions/6693257/making-a-stacked-bar-plot-for-multiple-variables-ggplot2-in-r – Jon Spring Jul 20 '19 at 05:51

1 Answers1

0

Not sure how to convert your object without a dput excerpt of it (or something structurally similar), so here's an example using the printed version of the object as provided:

library(ggplot2)
# First I reshape into long form, so the series is denoted in a single
#   column. This way we can map the series to the "fill" aesthetic,
#   and get a legend. (Harder to do across multiple variables.)
df <- tidyr::gather(df, col, val, -date)

ggplot(df, aes(date, val, fill = col)) + geom_col()

enter image description here

Data:

df <- read.table(
  header = T, 
  stringsAsFactors = F,
  text = "date   y1       y2       y3       y4       y5       y6       y7       y8       y9      y10      y11
'2018-06-18 17:00:00' 57.24548 49.45735 72.30936 26.78655 71.70350 70.40394 49.59040 30.39259 48.57300 83.48015 110.2542
'2018-06-19 17:00:00' 57.05952 50.07112 72.62863 26.71799 71.75246 70.55178 49.63896 30.72064 48.61171 83.65722 110.7769
'2018-06-20 17:00:00' 56.45272 49.76424 71.28383 26.63964 70.85164 70.00969 49.73608 30.90396 48.77624 83.17519 109.9878
'2018-06-21 17:00:00' 57.27484 49.98203 72.70603 26.51232 71.09642 69.78300 50.14399 31.17412 49.11496 83.54901 109.8004
'2018-06-24 17:00:00' 56.38420 48.95250 71.24513 26.22830 70.19559 68.33411 50.39651 31.09693 49.92790 82.78169 107.4135
'2018-06-25 17:00:00' 56.59952 49.03367 72.14489 26.14015 70.45997 68.61008 50.18284 31.26096 49.98597 82.52592 108.1828")
df$date = as.Date(df$date, format = "%Y-%m-%d %H:%M:%S")
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • What's dput and why is it necessary?` – Cinji18 Jul 20 '19 at 05:12
  • I managed to make some modifications and it worked. Not sure why though. I don't see geom_bar, but is it still possible if I want to change it to an area plot with something like geom_area? – Cinji18 Jul 20 '19 at 06:59
  • `geom_bar()` maps counts to height. `geom_col()` maps the y value to height, and is equivalent to `geom_bar(stat = "identity")` https://ggplot2.tidyverse.org/reference/geom_bar.html See here for more on area plots, should work the same: https://ggplot2.tidyverse.org/reference/geom_ribbon.html – Jon Spring Jul 20 '19 at 07:19