图异常堆积面积图ggplot2
Graph Anomalies Stacked Area Chart ggplot2
我正在尝试创建负值和正值的堆积面积图,显示投资组合中行业的多头和空头权重的构成。这些值可以在负值和正值之间变化,所以理想情况下我想要这样的值:
但是,同一行业可以在一个日期具有正权重而在另一个日期具有负权重。不幸的是,ggplot2 试图连接这些值,因此如果某个扇区在某一天为正,然后在一段时间内为负,然后再次为正,gglpot2 将在两个正值之间绘制一条连接线。这看起来像:
有什么方法可以摆脱这些连接线吗?这是我的数据和代码示例。
library(reshape)
library(ggplot2)
sectorMelt <- melt(exampleData, id = 'date')
longSector <- sectorMelt[which(sectorMelt$value > 0),]
shortSector <- sectorMelt[which(sectorMelt$value < 0),]
ggplot() +
geom_area(data = longSector, aes(x = date, y = value, fill = variable), stat = 'identity') +
geom_area(data = shortSector, aes(x = date, y = value, fill = variable), stat = 'identity')
< dput(exampleData)
structure(list(date = structure(c(13880, 13881, 13882, 13885,
13886, 13887, 13888, 13889, 13892, 13893, 13894, 13895, 13896,
13900, 13901, 13902, 13903, 13906, 13907, 13908, 13909, 13910,
13913, 13914, 13915, 13916, 13917, 13920, 13921, 13922, 13923,
13924, 13928, 13929, 13930, 13931, 13934, 13935, 13936, 13937,
13938, 13941, 13942, 13943, 13944, 13945, 13948, 13949, 13950,
13951, 13952, 13955, 13956, 13957, 13958, 13962, 13963, 13964,
13965, 13966, 13969, 13970, 13971, 13972, 13973, 13976, 13977,
13978, 13979, 13980, 13983, 13984, 13985, 13986, 13987, 13990,
13991, 13992, 13993, 13994, 13997, 13998, 13999, 14000, 14001,
14004, 14005, 14006, 14007, 14008, 14011, 14012, 14013, 14014,
14015, 14018, 14019, 14020, 14021, 14022, 14026, 14027, 14028,
14029, 14032, 14033, 14034, 14035, 14036, 14039, 14040, 14041,
14042, 14043, 14046, 14047, 14048, 14049, 14050, 14053, 14054,
14055, 14056, 14057, 14060), class = "Date"), V1 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004016064, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004016064, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), Basic.Materials = c(0.016064256, 0.016064256, 0.024096384,
0.016064256, 0.016129032, 0.016064256, 0.016064256, 0.016064256,
0.011870058, 0.008032128, 0.008032128, 0.016064256, 0.016064256,
0, 0, 0.016064256, 0.016064256, 0.008032128, 0.008032128, -0.008032128,
-0.016064256, -0.024096384, -0.016064256, -0.016064256, -0.016064256,
-0.016064256, -0.024096384, -0.024096384, -0.024096384, -0.024096384,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.032, -0.032, -0.024096384, -0.024, -0.032128512, -0.024096384,
-0.024096384, -0.032128512, -0.032128512, -0.024096384, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.024096384, -0.02008032,
-0.028112448, -0.024096384, -0.028112448, -0.032128512, -0.032128512,
-0.032128512, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.024096384, -0.048192768, -0.04016064, -0.056224896, -0.04016064,
-0.024096384, -0.024096384, 0, -0.008032128, 0, 0, 0, 0, 0, 0.008032128,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.008032128,
-0.008032128, -0.008032128, -0.008032128, -0.008032128, -0.008032128,
-0.008032128, -0.008032128, -0.008032128, -0.008032128, -0.008032128,
-0.008032128, 0, 0, 0, 0, 0, 0, 0, 0, 0), Communications = c(-0.056224896,
-0.048192768, -0.056224896, -0.056224896, -0.052419354, -0.064257024,
-0.056224896, -0.056224896, -0.06472665, -0.064257024, -0.056224896,
-0.056224896, -0.048192768, -0.04016064, -0.036144576, -0.04016064,
-0.032128512, -0.024096384, -0.016064256, -0.016064256, -0.016064256,
-0.008032128, -0.008032128, 0.008032128, 0.008032128, 0.008032128,
0.016064256, 0.016064256, 0.024096384, 0.024096384, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.008, 0.024,
0.024096384, 0.024, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.008032128, 0, 0.016064256, 0.008032128, 0, 0,
0.008032128, 0.008032128, 0.008032128, 0.016064256, 0.008032128,
0.008032128, 0.008032128, 0.016064256, 0.008032128, 0.008032128,
0.008032128, 0.008032128, 0.024096384, 0.016064256, 0.016064256,
0.008032128, 0, 0, 0.004016064, 0.016064256, 0, 0.008032128,
-0.008032128, -0.036144576, -0.032128512, -0.016064256, -0.024096384,
-0.032128512, -0.048192768, -0.032, -0.048, -0.048, -0.048, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.044176704, -0.04016064, -0.032128512,
-0.032128512, -0.04016064, -0.032128512, -0.04016064, -0.024096384,
-0.02008032, -0.032128512, -0.024096384, -0.024096384, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.02008032, -0.024096384, -0.032128512, -0.028112448, -0.028112448,
-0.032128512), Consumer..Cyclical = c(-0.008032128, -0.008032128,
-0.008032128, -0.008032128, 0, 0.008032128, 0.008032128, 0.016064256,
0.02359437, 0.024096384, 0.012048192, 0.02008032, 0.008032128,
0, -0.008032128, -0.008032128, -0.012048192, 0, 0.004016064,
0.004016064, -0.004016064, -0.008032128, -0.004016064, 0.004016064,
0.004016064, -0.004016064, -0.004016064, -0.012048192, -0.012048192,
-0.02008032, -0.004016064, 0.004016064, 0.012048192, 0.012048192,
0.012048192, 0, -0.004016064, -0.004016064, -0.004016064, 0.012048192,
0.02, 0.02, 0.008032128, 0.012, 0.024096384, 0.028112448, 0.028112448,
0.02008032, 0.028112448, 0.02008032, 0.012048192, 0.02008032,
0.012048192, 0.016064256, 0.024096384, 0.04016064, 0.048192768,
0.048192768, 0.052208832, 0.056224896, 0.06024096, 0.044176704,
0.052208832, 0.052208832, 0.052208832, 0.068273088, 0.068273088,
0.068273088, 0.068273088, 0.076305216, 0.076305216, 0.068273088,
0.068273088, 0.068273088, 0.06024096, 0.044176704, 0.044176704,
0.044176704, 0.032128512, 0.04016064, 0.032128512, 0.04016064,
0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.032128512,
0.032128512, 0.032128512, 0.032, 0.032, 0.016, 0.016, 0.008032128,
-0.008032128, 0, -0.008032128, -0.008032128, -0.024096384, -0.024096384,
-0.024096384, -0.016064256, -0.04016064, -0.04016064, -0.04016064,
-0.032128512, -0.036144576, -0.032128512, -0.032128512, -0.04016064,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.032128512,
-0.04016064, -0.024096384, -0.024096384, -0.024096384, -0.024096384,
-0.024096384, -0.024096384, -0.024096384, -0.024096384, -0.024096384
), Consumer..Non.cyclical = c(-0.06024096, -0.064257024, -0.064257024,
-0.06024096, -0.096774192, -0.076305216, -0.092369472, -0.1004016,
-0.077195886, -0.112449792, -0.108433728, -0.116465856, -0.108433728,
-0.1004016, -0.076305216, -0.06024096, -0.06024096, -0.06024096,
-0.056224896, -0.044176704, -0.052208832, -0.028112448, -0.036144576,
-0.076305216, -0.076305216, -0.068273088, -0.06024096, -0.052208832,
-0.072289152, -0.06024096, -0.064257024, -0.076305216, -0.076305216,
-0.072289152, -0.064257024, -0.052208832, -0.06024096, -0.072289152,
-0.068273088, -0.068273088, -0.068, -0.06, -0.084337344, -0.076,
-0.068273088, -0.076305216, -0.068273088, -0.06024096, -0.068273088,
-0.06024096, -0.06024096, -0.06024096, -0.052208832, -0.06024096,
-0.072289152, -0.072289152, -0.072289152, -0.072289152, -0.08032128,
-0.084337344, -0.084337344, -0.068273088, -0.068273088, -0.068273088,
-0.068273088, -0.076305216, -0.068273088, -0.076305216, -0.068273088,
-0.068273088, -0.068273088, -0.084337344, -0.076305216, -0.076305216,
-0.032128512, -0.032128512, -0.04016064, -0.032128512, -0.032128512,
-0.032128512, -0.036144576, -0.048192768, -0.052208832, -0.048192768,
-0.032128512, -0.04016064, -0.028112448, -0.044176704, -0.048192768,
-0.056, -0.056, -0.04, -0.048, -0.036144576, -0.024096384, -0.028112448,
-0.028112448, -0.04016064, -0.04016064, -0.04016064, -0.044176704,
-0.04016064, -0.048192768, -0.04016064, -0.04016064, -0.044176704,
-0.052208832, -0.052208832, -0.052208832, -0.052208832, -0.048192768,
-0.056224896, -0.056224896, -0.044176704, -0.04016064, -0.024096384,
-0.04016064, -0.04016064, -0.04016064, -0.032128512, -0.044176704,
-0.048192768, -0.056224896, -0.056224896, -0.048192768), Diversified = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.004,
0.004, 0.004016064, 0.004, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004, -0.004, -0.004, -0.004,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Energy = c(0, 0, -0.008032128,
-0.008032128, 0, -0.008032128, 0.004016064, -0.008032128, -0.00833981400000001,
0, 0.016064256, 0.024096384, 0.024096384, 0.032128512, 0.032128512,
0.024096384, 0.032128512, 0.032128512, 0.036144576, 0.036144576,
0.044176704, 0.036144576, 0.036144576, 0.044176704, 0.056224896,
0.052208832, 0.036144576, 0.02008032, 0.036144576, 0.028112448,
0.028112448, 0.036144576, 0.028112448, 0.028112448, 0.02008032,
0.032128512, 0.028112448, 0.036144576, 0.036144576, 0.036144576,
0.036, 0.028, 0.036144576, 0.028, 0.028112448, 0.036144576, 0.036144576,
0.036144576, 0.036144576, 0.036144576, 0.036144576, 0.036144576,
0.036144576, 0.044176704, 0.052208832, 0.044176704, 0.044176704,
0.036144576, 0.036144576, 0.036144576, 0.028112448, 0.044176704,
0.036144576, 0.036144576, 0.028112448, 0.028112448, 0.028112448,
0.02008032, 0.02008032, 0.012048192, 0.012048192, 0.012048192,
0.012048192, 0.012048192, 0.012048192, 0.012048192, 0.004016064,
-0.004016064, 0.02008032, 0.012048192, 0.028112448, 0.028112448,
0.028112448, 0.044176704, 0.044176704, 0.04016064, 0.044176704,
0.044176704, 0.044176704, 0.028, 0.044, 0.052, 0.052, 0.052208832,
0.036144576, 0.036144576, 0.02008032, 0.02008032, 0.028112448,
0.028112448, 0.036144576, 0.028112448, 0.044176704, 0.044176704,
0.044176704, 0.052208832, 0.052208832, 0.036144576, 0.024096384,
0.024096384, 0.024096384, 0.016064256, 0.024096384, 0.024096384,
0.024096384, 0.024096384, 0.008032128, 0.02008032, 0.016064256,
-0.008032128, 0, 0.008032128, -0.008032128, -0.008032128, -0.008032128
), Financial = c(0.08032128, 0.08032128, 0.08032128, 0.08032128,
0.08064516, 0.08032128, 0.08032128, 0.068273088, 0.071706168,
0.08032128, 0.064257024, 0.056224896, 0.04016064, 0.024096384,
0.008032128, 0, -0.024096384, -0.032128512, -0.048192768, -0.048192768,
-0.056224896, -0.064257024, -0.072289152, -0.056224896, -0.064257024,
-0.08032128, -0.072289152, -0.064257024, -0.056224896, -0.056224896,
-0.056224896, -0.056224896, -0.064257024, -0.064257024, -0.056224896,
-0.052208832, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.04, -0.048, -0.048192768, -0.048, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.04016064, -0.048192768, -0.048192768,
-0.036144576, -0.048192768, -0.04016064, -0.064257024, -0.064257024,
-0.064257024, -0.036144576, -0.044176704, -0.036144576, -0.04016064,
-0.056224896, -0.048192768, -0.048192768, -0.04016064, -0.048192768,
-0.048192768, -0.04016064, -0.04016064, -0.032128512, -0.032128512,
-0.032128512, -0.008032128, 0.024096384, 0.02008032, 0.028112448,
0.036144576, 0.044176704, 0.036144576, 0.032128512, 0.036144576,
0.032128512, 0.044176704, 0.036144576, 0.036144576, 0.028112448,
0.02008032, 0.036144576, 0.036144576, 0.044, 0.044, 0.052, 0.06,
0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096,
0.068273088, 0.068273088, 0.068273088, 0.068273088, 0.064257024,
0.064257024, 0.064257024, 0.072289152, 0.072289152, 0.072289152,
0.072289152, 0.072289152, 0.076305216, 0.076305216, 0.076305216,
0.068273088, 0.06024096, 0.06024096, 0.06024096, 0.056224896,
0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.064257024
), Industrial = c(0.048192768, 0.044176704, 0.052208832, 0.048192768,
0.06048387, 0.064257024, 0.052208832, 0.068273088, 0.059852304,
0.06024096, 0.06024096, 0.052208832, 0.076305216, 0.076305216,
0.06024096, 0.068273088, 0.068273088, 0.076305216, 0.076305216,
0.092369472, 0.08032128, 0.1004016, 0.096385536, 0.088353408,
0.084337344, 0.092369472, 0.096385536, 0.112449792, 0.108433728,
0.1004016, 0.108433728, 0.104417664, 0.1004016, 0.1004016, 0.1004016,
0.088353408, 0.092369472, 0.096385536, 0.084337344, 0.076305216,
0.084, 0.076, 0.084337344, 0.084, 0.076305216, 0.084337344, 0.084337344,
0.088353408, 0.092369472, 0.092369472, 0.1004016, 0.084337344,
0.084337344, 0.076305216, 0.088353408, 0.084337344, 0.068273088,
0.06024096, 0.068273088, 0.06024096, 0.068273088, 0.06024096,
0.052208832, 0.044176704, 0.052208832, 0.052208832, 0.06024096,
0.06024096, 0.06024096, 0.052208832, 0.052208832, 0.06024096,
0.044176704, 0.028112448, 0.02008032, 0.028112448, 0.028112448,
0.028112448, 0.02008032, 0.012048192, -0.004016064, -0.012048192,
-0.004016064, -0.004016064, -0.016064256, -0.02008032, -0.028112448,
-0.028112448, -0.02008032, -0.012, -0.02, -0.02, -0.02, -0.02008032,
0, -0.012048192, 0.012048192, 0.024096384, 0.04016064, 0.032128512,
0.028112448, 0.02008032, 0.024096384, 0.02008032, 0.02008032,
0.024096384, 0.028112448, 0.04016064, 0.032128512, 0.032128512,
0.04016064, 0.044176704, 0.028112448, 0.024096384, 0.028112448,
0.028112448, 0.028112448, 0.024096384, 0.032128512, 0.024096384,
0.04016064, 0.028112448, 0.048192768, 0.048192768, 0.048192768
), Technology = c(-0.068273088, -0.068273088, -0.06024096, -0.052208832,
-0.048387096, -0.06024096, -0.052208832, -0.044176704, -0.056759298,
-0.036144576, -0.036144576, -0.036144576, -0.048192768, -0.036144576,
-0.02008032, -0.048192768, -0.036144576, -0.036144576, -0.028112448,
-0.024096384, 0.012048192, -0.012048192, 0.004016064, 0.012048192,
0.012048192, 0.024096384, 0.028112448, 0.028112448, 0.028112448,
0.032128512, 0.036144576, 0.044176704, 0.06024096, 0.052208832,
0.036144576, 0.044176704, 0.048192768, 0.048192768, 0.056224896,
0.056224896, 0.044, 0.036, 0.048192768, 0.044, 0.044176704, 0.024096384,
0.016064256, 0.028112448, 0.016064256, 0.016064256, 0.024096384,
0.02008032, 0.032128512, 0.028112448, 0.028112448, 0.028112448,
0.028112448, 0.02008032, 0.036144576, 0.04016064, 0.04016064,
0.04016064, 0.048192768, 0.048192768, 0.048192768, 0.048192768,
0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.04016064,
0.032128512, 0.016064256, 0.008032128, 0, 0, -0.008032128, -0.032128512,
-0.024096384, -0.024096384, -0.024096384, -0.016064256, -0.008032128,
-0.016064256, -0.016064256, -0.016064256, -0.016064256, -0.02008032,
-0.024, -0.016, -0.032, -0.032, -0.04016064, -0.04016064, -0.032128512,
-0.032128512, -0.032128512, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.048192768, -0.04016064, -0.048192768,
-0.056224896, -0.048192768, -0.044176704, -0.04016064, -0.04016064,
-0.032128512, -0.032128512, -0.032128512, -0.04016064, -0.04016064,
-0.032128512, -0.04016064, -0.04016064, -0.032128512, -0.04016064,
-0.024096384, -0.024096384, -0.024096384, -0.024096384), Utilities = c(0.048192768,
0.048192768, 0.04016064, 0.04016064, 0.04032258, 0.04016064,
0.04016064, 0.04016064, 0.0399987, 0.04016064, 0.04016064, 0.04016064,
0.04016064, 0.044176704, 0.04016064, 0.048192768, 0.048192768,
0.04016064, 0.024096384, 0.008032128, 0.008032128, 0.008032128,
0, -0.008032128, -0.008032128, -0.008032128, -0.016064256, -0.024096384,
-0.032128512, -0.024096384, -0.032128512, -0.04016064, -0.044176704,
-0.04016064, -0.032128512, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.056, -0.048, -0.048192768, -0.048,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.032128512, -0.024096384, -0.032128512, -0.024096384,
-0.024096384, -0.024096384, 0, 0.008032128, 0.024096384, 0.024,
0.024, 0.024, 0.024, 0.024096384, 0.024096384, 0.024096384, 0.024096384,
0.024096384, 0.024096384, 0.024096384, 0.024096384, 0.024096384,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.024096384,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.024096384), NA. = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("date",
"V1", "Basic.Materials", "Communications", "Consumer..Cyclical",
"Consumer..Non.cyclical", "Diversified", "Energy", "Financial",
"Industrial", "Technology", "Utilities", "NA."), row.names = 755:879, class = "data.frame")
我通过分别创建长图和短图解决了这个问题,然后将长数据集中的所有负值设置为 0,反之亦然。所以,我只是用这个修改了代码。
sectorMelt <- melt(exampleData, id = 'date')
longSector <- sectorMelt
shortSector <- sectorMelt
longSector$value[which(longSector$value < 0)] <- rep(0, length(which(longSector$value < 0)))
shortSector$value[which(shortSector$value > 0)] <- rep(0, length(which(shortSector$value > 0)))
ggplot() +
geom_area(data = longSector, aes(x = date, y = value, fill = variable), stat = 'identity') +
geom_area(data = shortSector, aes(x = date, y = value, fill = variable), stat = 'identity')
所以现在看起来像这样:
我正在尝试创建负值和正值的堆积面积图,显示投资组合中行业的多头和空头权重的构成。这些值可以在负值和正值之间变化,所以理想情况下我想要这样的值:
但是,同一行业可以在一个日期具有正权重而在另一个日期具有负权重。不幸的是,ggplot2 试图连接这些值,因此如果某个扇区在某一天为正,然后在一段时间内为负,然后再次为正,gglpot2 将在两个正值之间绘制一条连接线。这看起来像:
有什么方法可以摆脱这些连接线吗?这是我的数据和代码示例。
library(reshape)
library(ggplot2)
sectorMelt <- melt(exampleData, id = 'date')
longSector <- sectorMelt[which(sectorMelt$value > 0),]
shortSector <- sectorMelt[which(sectorMelt$value < 0),]
ggplot() +
geom_area(data = longSector, aes(x = date, y = value, fill = variable), stat = 'identity') +
geom_area(data = shortSector, aes(x = date, y = value, fill = variable), stat = 'identity')
< dput(exampleData)
structure(list(date = structure(c(13880, 13881, 13882, 13885,
13886, 13887, 13888, 13889, 13892, 13893, 13894, 13895, 13896,
13900, 13901, 13902, 13903, 13906, 13907, 13908, 13909, 13910,
13913, 13914, 13915, 13916, 13917, 13920, 13921, 13922, 13923,
13924, 13928, 13929, 13930, 13931, 13934, 13935, 13936, 13937,
13938, 13941, 13942, 13943, 13944, 13945, 13948, 13949, 13950,
13951, 13952, 13955, 13956, 13957, 13958, 13962, 13963, 13964,
13965, 13966, 13969, 13970, 13971, 13972, 13973, 13976, 13977,
13978, 13979, 13980, 13983, 13984, 13985, 13986, 13987, 13990,
13991, 13992, 13993, 13994, 13997, 13998, 13999, 14000, 14001,
14004, 14005, 14006, 14007, 14008, 14011, 14012, 14013, 14014,
14015, 14018, 14019, 14020, 14021, 14022, 14026, 14027, 14028,
14029, 14032, 14033, 14034, 14035, 14036, 14039, 14040, 14041,
14042, 14043, 14046, 14047, 14048, 14049, 14050, 14053, 14054,
14055, 14056, 14057, 14060), class = "Date"), V1 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004016064, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004016064, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), Basic.Materials = c(0.016064256, 0.016064256, 0.024096384,
0.016064256, 0.016129032, 0.016064256, 0.016064256, 0.016064256,
0.011870058, 0.008032128, 0.008032128, 0.016064256, 0.016064256,
0, 0, 0.016064256, 0.016064256, 0.008032128, 0.008032128, -0.008032128,
-0.016064256, -0.024096384, -0.016064256, -0.016064256, -0.016064256,
-0.016064256, -0.024096384, -0.024096384, -0.024096384, -0.024096384,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.032, -0.032, -0.024096384, -0.024, -0.032128512, -0.024096384,
-0.024096384, -0.032128512, -0.032128512, -0.024096384, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.024096384, -0.02008032,
-0.028112448, -0.024096384, -0.028112448, -0.032128512, -0.032128512,
-0.032128512, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.024096384, -0.048192768, -0.04016064, -0.056224896, -0.04016064,
-0.024096384, -0.024096384, 0, -0.008032128, 0, 0, 0, 0, 0, 0.008032128,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.008032128,
-0.008032128, -0.008032128, -0.008032128, -0.008032128, -0.008032128,
-0.008032128, -0.008032128, -0.008032128, -0.008032128, -0.008032128,
-0.008032128, 0, 0, 0, 0, 0, 0, 0, 0, 0), Communications = c(-0.056224896,
-0.048192768, -0.056224896, -0.056224896, -0.052419354, -0.064257024,
-0.056224896, -0.056224896, -0.06472665, -0.064257024, -0.056224896,
-0.056224896, -0.048192768, -0.04016064, -0.036144576, -0.04016064,
-0.032128512, -0.024096384, -0.016064256, -0.016064256, -0.016064256,
-0.008032128, -0.008032128, 0.008032128, 0.008032128, 0.008032128,
0.016064256, 0.016064256, 0.024096384, 0.024096384, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.008, 0.024,
0.024096384, 0.024, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.016064256, 0.016064256, 0.016064256, 0.016064256,
0.016064256, 0.008032128, 0, 0.016064256, 0.008032128, 0, 0,
0.008032128, 0.008032128, 0.008032128, 0.016064256, 0.008032128,
0.008032128, 0.008032128, 0.016064256, 0.008032128, 0.008032128,
0.008032128, 0.008032128, 0.024096384, 0.016064256, 0.016064256,
0.008032128, 0, 0, 0.004016064, 0.016064256, 0, 0.008032128,
-0.008032128, -0.036144576, -0.032128512, -0.016064256, -0.024096384,
-0.032128512, -0.048192768, -0.032, -0.048, -0.048, -0.048, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.044176704, -0.04016064, -0.032128512,
-0.032128512, -0.04016064, -0.032128512, -0.04016064, -0.024096384,
-0.02008032, -0.032128512, -0.024096384, -0.024096384, -0.032128512,
-0.032128512, -0.032128512, -0.032128512, -0.032128512, -0.032128512,
-0.02008032, -0.024096384, -0.032128512, -0.028112448, -0.028112448,
-0.032128512), Consumer..Cyclical = c(-0.008032128, -0.008032128,
-0.008032128, -0.008032128, 0, 0.008032128, 0.008032128, 0.016064256,
0.02359437, 0.024096384, 0.012048192, 0.02008032, 0.008032128,
0, -0.008032128, -0.008032128, -0.012048192, 0, 0.004016064,
0.004016064, -0.004016064, -0.008032128, -0.004016064, 0.004016064,
0.004016064, -0.004016064, -0.004016064, -0.012048192, -0.012048192,
-0.02008032, -0.004016064, 0.004016064, 0.012048192, 0.012048192,
0.012048192, 0, -0.004016064, -0.004016064, -0.004016064, 0.012048192,
0.02, 0.02, 0.008032128, 0.012, 0.024096384, 0.028112448, 0.028112448,
0.02008032, 0.028112448, 0.02008032, 0.012048192, 0.02008032,
0.012048192, 0.016064256, 0.024096384, 0.04016064, 0.048192768,
0.048192768, 0.052208832, 0.056224896, 0.06024096, 0.044176704,
0.052208832, 0.052208832, 0.052208832, 0.068273088, 0.068273088,
0.068273088, 0.068273088, 0.076305216, 0.076305216, 0.068273088,
0.068273088, 0.068273088, 0.06024096, 0.044176704, 0.044176704,
0.044176704, 0.032128512, 0.04016064, 0.032128512, 0.04016064,
0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.032128512,
0.032128512, 0.032128512, 0.032, 0.032, 0.016, 0.016, 0.008032128,
-0.008032128, 0, -0.008032128, -0.008032128, -0.024096384, -0.024096384,
-0.024096384, -0.016064256, -0.04016064, -0.04016064, -0.04016064,
-0.032128512, -0.036144576, -0.032128512, -0.032128512, -0.04016064,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.032128512,
-0.04016064, -0.024096384, -0.024096384, -0.024096384, -0.024096384,
-0.024096384, -0.024096384, -0.024096384, -0.024096384, -0.024096384
), Consumer..Non.cyclical = c(-0.06024096, -0.064257024, -0.064257024,
-0.06024096, -0.096774192, -0.076305216, -0.092369472, -0.1004016,
-0.077195886, -0.112449792, -0.108433728, -0.116465856, -0.108433728,
-0.1004016, -0.076305216, -0.06024096, -0.06024096, -0.06024096,
-0.056224896, -0.044176704, -0.052208832, -0.028112448, -0.036144576,
-0.076305216, -0.076305216, -0.068273088, -0.06024096, -0.052208832,
-0.072289152, -0.06024096, -0.064257024, -0.076305216, -0.076305216,
-0.072289152, -0.064257024, -0.052208832, -0.06024096, -0.072289152,
-0.068273088, -0.068273088, -0.068, -0.06, -0.084337344, -0.076,
-0.068273088, -0.076305216, -0.068273088, -0.06024096, -0.068273088,
-0.06024096, -0.06024096, -0.06024096, -0.052208832, -0.06024096,
-0.072289152, -0.072289152, -0.072289152, -0.072289152, -0.08032128,
-0.084337344, -0.084337344, -0.068273088, -0.068273088, -0.068273088,
-0.068273088, -0.076305216, -0.068273088, -0.076305216, -0.068273088,
-0.068273088, -0.068273088, -0.084337344, -0.076305216, -0.076305216,
-0.032128512, -0.032128512, -0.04016064, -0.032128512, -0.032128512,
-0.032128512, -0.036144576, -0.048192768, -0.052208832, -0.048192768,
-0.032128512, -0.04016064, -0.028112448, -0.044176704, -0.048192768,
-0.056, -0.056, -0.04, -0.048, -0.036144576, -0.024096384, -0.028112448,
-0.028112448, -0.04016064, -0.04016064, -0.04016064, -0.044176704,
-0.04016064, -0.048192768, -0.04016064, -0.04016064, -0.044176704,
-0.052208832, -0.052208832, -0.052208832, -0.052208832, -0.048192768,
-0.056224896, -0.056224896, -0.044176704, -0.04016064, -0.024096384,
-0.04016064, -0.04016064, -0.04016064, -0.032128512, -0.044176704,
-0.048192768, -0.056224896, -0.056224896, -0.048192768), Diversified = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.004,
0.004, 0.004016064, 0.004, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.004, -0.004, -0.004, -0.004,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Energy = c(0, 0, -0.008032128,
-0.008032128, 0, -0.008032128, 0.004016064, -0.008032128, -0.00833981400000001,
0, 0.016064256, 0.024096384, 0.024096384, 0.032128512, 0.032128512,
0.024096384, 0.032128512, 0.032128512, 0.036144576, 0.036144576,
0.044176704, 0.036144576, 0.036144576, 0.044176704, 0.056224896,
0.052208832, 0.036144576, 0.02008032, 0.036144576, 0.028112448,
0.028112448, 0.036144576, 0.028112448, 0.028112448, 0.02008032,
0.032128512, 0.028112448, 0.036144576, 0.036144576, 0.036144576,
0.036, 0.028, 0.036144576, 0.028, 0.028112448, 0.036144576, 0.036144576,
0.036144576, 0.036144576, 0.036144576, 0.036144576, 0.036144576,
0.036144576, 0.044176704, 0.052208832, 0.044176704, 0.044176704,
0.036144576, 0.036144576, 0.036144576, 0.028112448, 0.044176704,
0.036144576, 0.036144576, 0.028112448, 0.028112448, 0.028112448,
0.02008032, 0.02008032, 0.012048192, 0.012048192, 0.012048192,
0.012048192, 0.012048192, 0.012048192, 0.012048192, 0.004016064,
-0.004016064, 0.02008032, 0.012048192, 0.028112448, 0.028112448,
0.028112448, 0.044176704, 0.044176704, 0.04016064, 0.044176704,
0.044176704, 0.044176704, 0.028, 0.044, 0.052, 0.052, 0.052208832,
0.036144576, 0.036144576, 0.02008032, 0.02008032, 0.028112448,
0.028112448, 0.036144576, 0.028112448, 0.044176704, 0.044176704,
0.044176704, 0.052208832, 0.052208832, 0.036144576, 0.024096384,
0.024096384, 0.024096384, 0.016064256, 0.024096384, 0.024096384,
0.024096384, 0.024096384, 0.008032128, 0.02008032, 0.016064256,
-0.008032128, 0, 0.008032128, -0.008032128, -0.008032128, -0.008032128
), Financial = c(0.08032128, 0.08032128, 0.08032128, 0.08032128,
0.08064516, 0.08032128, 0.08032128, 0.068273088, 0.071706168,
0.08032128, 0.064257024, 0.056224896, 0.04016064, 0.024096384,
0.008032128, 0, -0.024096384, -0.032128512, -0.048192768, -0.048192768,
-0.056224896, -0.064257024, -0.072289152, -0.056224896, -0.064257024,
-0.08032128, -0.072289152, -0.064257024, -0.056224896, -0.056224896,
-0.056224896, -0.056224896, -0.064257024, -0.064257024, -0.056224896,
-0.052208832, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.04, -0.048, -0.048192768, -0.048, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.04016064, -0.048192768, -0.048192768,
-0.036144576, -0.048192768, -0.04016064, -0.064257024, -0.064257024,
-0.064257024, -0.036144576, -0.044176704, -0.036144576, -0.04016064,
-0.056224896, -0.048192768, -0.048192768, -0.04016064, -0.048192768,
-0.048192768, -0.04016064, -0.04016064, -0.032128512, -0.032128512,
-0.032128512, -0.008032128, 0.024096384, 0.02008032, 0.028112448,
0.036144576, 0.044176704, 0.036144576, 0.032128512, 0.036144576,
0.032128512, 0.044176704, 0.036144576, 0.036144576, 0.028112448,
0.02008032, 0.036144576, 0.036144576, 0.044, 0.044, 0.052, 0.06,
0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096,
0.068273088, 0.068273088, 0.068273088, 0.068273088, 0.064257024,
0.064257024, 0.064257024, 0.072289152, 0.072289152, 0.072289152,
0.072289152, 0.072289152, 0.076305216, 0.076305216, 0.076305216,
0.068273088, 0.06024096, 0.06024096, 0.06024096, 0.056224896,
0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.06024096, 0.064257024
), Industrial = c(0.048192768, 0.044176704, 0.052208832, 0.048192768,
0.06048387, 0.064257024, 0.052208832, 0.068273088, 0.059852304,
0.06024096, 0.06024096, 0.052208832, 0.076305216, 0.076305216,
0.06024096, 0.068273088, 0.068273088, 0.076305216, 0.076305216,
0.092369472, 0.08032128, 0.1004016, 0.096385536, 0.088353408,
0.084337344, 0.092369472, 0.096385536, 0.112449792, 0.108433728,
0.1004016, 0.108433728, 0.104417664, 0.1004016, 0.1004016, 0.1004016,
0.088353408, 0.092369472, 0.096385536, 0.084337344, 0.076305216,
0.084, 0.076, 0.084337344, 0.084, 0.076305216, 0.084337344, 0.084337344,
0.088353408, 0.092369472, 0.092369472, 0.1004016, 0.084337344,
0.084337344, 0.076305216, 0.088353408, 0.084337344, 0.068273088,
0.06024096, 0.068273088, 0.06024096, 0.068273088, 0.06024096,
0.052208832, 0.044176704, 0.052208832, 0.052208832, 0.06024096,
0.06024096, 0.06024096, 0.052208832, 0.052208832, 0.06024096,
0.044176704, 0.028112448, 0.02008032, 0.028112448, 0.028112448,
0.028112448, 0.02008032, 0.012048192, -0.004016064, -0.012048192,
-0.004016064, -0.004016064, -0.016064256, -0.02008032, -0.028112448,
-0.028112448, -0.02008032, -0.012, -0.02, -0.02, -0.02, -0.02008032,
0, -0.012048192, 0.012048192, 0.024096384, 0.04016064, 0.032128512,
0.028112448, 0.02008032, 0.024096384, 0.02008032, 0.02008032,
0.024096384, 0.028112448, 0.04016064, 0.032128512, 0.032128512,
0.04016064, 0.044176704, 0.028112448, 0.024096384, 0.028112448,
0.028112448, 0.028112448, 0.024096384, 0.032128512, 0.024096384,
0.04016064, 0.028112448, 0.048192768, 0.048192768, 0.048192768
), Technology = c(-0.068273088, -0.068273088, -0.06024096, -0.052208832,
-0.048387096, -0.06024096, -0.052208832, -0.044176704, -0.056759298,
-0.036144576, -0.036144576, -0.036144576, -0.048192768, -0.036144576,
-0.02008032, -0.048192768, -0.036144576, -0.036144576, -0.028112448,
-0.024096384, 0.012048192, -0.012048192, 0.004016064, 0.012048192,
0.012048192, 0.024096384, 0.028112448, 0.028112448, 0.028112448,
0.032128512, 0.036144576, 0.044176704, 0.06024096, 0.052208832,
0.036144576, 0.044176704, 0.048192768, 0.048192768, 0.056224896,
0.056224896, 0.044, 0.036, 0.048192768, 0.044, 0.044176704, 0.024096384,
0.016064256, 0.028112448, 0.016064256, 0.016064256, 0.024096384,
0.02008032, 0.032128512, 0.028112448, 0.028112448, 0.028112448,
0.028112448, 0.02008032, 0.036144576, 0.04016064, 0.04016064,
0.04016064, 0.048192768, 0.048192768, 0.048192768, 0.048192768,
0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.04016064, 0.04016064,
0.032128512, 0.016064256, 0.008032128, 0, 0, -0.008032128, -0.032128512,
-0.024096384, -0.024096384, -0.024096384, -0.016064256, -0.008032128,
-0.016064256, -0.016064256, -0.016064256, -0.016064256, -0.02008032,
-0.024, -0.016, -0.032, -0.032, -0.04016064, -0.04016064, -0.032128512,
-0.032128512, -0.032128512, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.048192768, -0.04016064, -0.048192768,
-0.056224896, -0.048192768, -0.044176704, -0.04016064, -0.04016064,
-0.032128512, -0.032128512, -0.032128512, -0.04016064, -0.04016064,
-0.032128512, -0.04016064, -0.04016064, -0.032128512, -0.04016064,
-0.024096384, -0.024096384, -0.024096384, -0.024096384), Utilities = c(0.048192768,
0.048192768, 0.04016064, 0.04016064, 0.04032258, 0.04016064,
0.04016064, 0.04016064, 0.0399987, 0.04016064, 0.04016064, 0.04016064,
0.04016064, 0.044176704, 0.04016064, 0.048192768, 0.048192768,
0.04016064, 0.024096384, 0.008032128, 0.008032128, 0.008032128,
0, -0.008032128, -0.008032128, -0.008032128, -0.016064256, -0.024096384,
-0.032128512, -0.024096384, -0.032128512, -0.04016064, -0.044176704,
-0.04016064, -0.032128512, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.056, -0.048, -0.048192768, -0.048,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.04016064, -0.04016064, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.048192768, -0.04016064, -0.04016064, -0.04016064,
-0.04016064, -0.04016064, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.048192768, -0.048192768, -0.048192768, -0.048192768,
-0.048192768, -0.032128512, -0.024096384, -0.032128512, -0.024096384,
-0.024096384, -0.024096384, 0, 0.008032128, 0.024096384, 0.024,
0.024, 0.024, 0.024, 0.024096384, 0.024096384, 0.024096384, 0.024096384,
0.024096384, 0.024096384, 0.024096384, 0.024096384, 0.024096384,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.024096384,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.032128512, 0.032128512, 0.032128512,
0.032128512, 0.032128512, 0.024096384), NA. = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("date",
"V1", "Basic.Materials", "Communications", "Consumer..Cyclical",
"Consumer..Non.cyclical", "Diversified", "Energy", "Financial",
"Industrial", "Technology", "Utilities", "NA."), row.names = 755:879, class = "data.frame")
我通过分别创建长图和短图解决了这个问题,然后将长数据集中的所有负值设置为 0,反之亦然。所以,我只是用这个修改了代码。
sectorMelt <- melt(exampleData, id = 'date')
longSector <- sectorMelt
shortSector <- sectorMelt
longSector$value[which(longSector$value < 0)] <- rep(0, length(which(longSector$value < 0)))
shortSector$value[which(shortSector$value > 0)] <- rep(0, length(which(shortSector$value > 0)))
ggplot() +
geom_area(data = longSector, aes(x = date, y = value, fill = variable), stat = 'identity') +
geom_area(data = shortSector, aes(x = date, y = value, fill = variable), stat = 'identity')
所以现在看起来像这样: