XTS对象的ggplot重复条形图

时间:2019-07-19 21:36:44

标签: r ggplot2 bar-chart xts

这真让我发疯。我有一个xts对象:

> 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

我想要一个barplot,所以我这样做了:

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

这给了我这个:

enter image description here

我想使用ggplot2重现此图(100%堆叠),但是我尝试了所有可以想到的包括

我已经尝试并调整了ggplot了很多次(下面的示例),但是仍然遇到相同的错误:

> 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

我是否需要将数据类型从xts重新缩放或更改为其他类型?
如果没有,那怎么办?

谢谢。

更新

以下是head(秒)的dput和str:

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

输出内容太长,无法完全上传到该论坛。

1 个答案:

答案 0 :(得分:0)

不确定如何在没有static string GetPath<T>(Expression<Func<T>> prop) { var body = prop.Body as System.Linq.Expressions.MemberExpression; if (body is null) { return null; } string res = body.Member.Name; MemberExpression expr = body; while ((expr = (expr.Expression as MemberExpression)) != null) { if (expr.Member is FieldInfo fi) { res = fi.FieldType.Name + "." + res; } else if (expr.Member is PropertyInfo pi) { res = pi.PropertyType.Name + "." + res; } } return res; } 摘录(或类似结构的摘录)的情况下转换对象,因此下面是使用所提供对象的打印版本的示例:

dput

enter image description here

数据:

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