ggplot中是否有工作日处理多行数据(熔融数据)?

时间:2019-05-26 20:37:00

标签: r ggplot2

我正在尝试绘制多行数据,并且正在使用库(bdscale)校正工作日的x比例尺,但是x比例尺放置不正确。我想知道外面是否有人已经遇到过同样的问题并且可以帮助我。谢谢!

我已经检查了之前的两个答案the question was with facetingtransform the date into factors,但是不幸的是,这两个答案都不是我想要的解决方案。

我包括了我的数据样本。

dput(pred_final_price_melt)
structure(list(date_hist = structure(c(18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040, 18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040, 18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040, 18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040, 18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040, 18017, 18018, 18019, 18022, 
18023, 18024, 18025, 18026, 18029, 18030, 18031, 18032, 18033, 
18036, 18037, 18038, 18039, 18040), class = "Date"), variable = c("Model_1", 
"Model_1", "Model_1", "Model_1", "Model_1", "Model_1", "Model_1", 
"Model_1", "Model_1", "Model_1", "Model_1", "Model_1", "Model_1", 
"Model_1", "Model_1", "Model_1", "Model_1", "Model_1", "Model_2", 
"Model_2", "Model_2", "Model_2", "Model_2", "Model_2", "Model_2", 
"Model_2", "Model_2", "Model_2", "Model_2", "Model_2", "Model_2", 
"Model_2", "Model_2", "Model_2", "Model_2", "Model_2", "Model_3", 
"Model_3", "Model_3", "Model_3", "Model_3", "Model_3", "Model_3", 
"Model_3", "Model_3", "Model_3", "Model_3", "Model_3", "Model_3", 
"Model_3", "Model_3", "Model_3", "Model_3", "Model_3", "Model_4", 
"Model_4", "Model_4", "Model_4", "Model_4", "Model_4", "Model_4", 
"Model_4", "Model_4", "Model_4", "Model_4", "Model_4", "Model_4", 
"Model_4", "Model_4", "Model_4", "Model_4", "Model_4", "BH", 
"BH", "BH", "BH", "BH", "BH", "BH", "BH", "BH", "BH", "BH", "BH", 
"BH", "BH", "BH", "BH", "BH", "BH", "BH_Intra", "BH_Intra", "BH_Intra", 
"BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", 
"BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", "BH_Intra", 
"BH_Intra", "BH_Intra", "BH_Intra"), value = c(0.000884284992420437, 
-0.00353937555302741, -0.000760649087221177, 0.000505816894284195, 
0.00063035804336864, 0.0013869625520111, -0.00202045712842525, 
-0.000629722921914322, 0.00264417023419794, -0.000627982918864567, 
0.00226102248461246, 0.00213032581453632, 0.000250972518509252, 
-0.00175702811244982, -0.00113150616042246, 0.00239144115796108, 
0.00564759036144569, -0.000375187593796844, -0.000884284992420437, 
-0.00353937555302741, 0.000760649087221177, 0.000505816894284195, 
-0.00063035804336864, 0.0013869625520111, -0.00202045712842525, 
0.000629722921914322, -0.00264417023419794, 0.000627982918864567, 
-0.00226102248461246, -0.00213032581453632, 0.000250972518509252, 
0.00175702811244982, 0.00113150616042246, 0.00239144115796108, 
0.00564759036144569, -0.000375187593796844, -0.000884284992420437, 
-0.00353937555302741, 0.000760649087221177, 0.000505816894284195, 
-0.00063035804336864, 0.0013869625520111, -0.00202045712842525, 
-0.000629722921914322, -0.00264417023419794, -0.000627982918864567, 
0.00226102248461246, -0.00213032581453632, 0.000250972518509252, 
0.00175702811244982, -0.00113150616042246, -0.00239144115796108, 
-0.00564759036144569, 0.000375187593796844, 0.000884284992420437, 
-0.00353937555302741, 0.000760649087221177, 0.000505816894284195, 
0.00063035804336864, -0.0013869625520111, 0.00202045712842525, 
0.000629722921914322, -0.00264417023419794, 0.000627982918864567, 
-0.00226102248461246, 0.00213032581453632, 0.000250972518509252, 
0.00175702811244982, 0.00113150616042246, 0.00239144115796108, 
-0.00564759036144569, 0.000375187593796844, -0.000758054327226754, 
-0.00328739410797829, 0.00139540783965497, 0.00228021281986313, 
0.003159757330637, -0.00214186720423337, 0.00189393939393945, 
0, 0.00352867044738492, -0.000753484867512255, 0.002764861128566, 
-0.00200526381752097, 0.000502323245008096, -0.00163173088992097, 
-0.00113150616042246, 0.00239144115796108, 0.00615268709191352, 
-0.00249594409085241, -0.000884284992420437, -0.00353937555302741, 
0.000760649087221177, 0.000505816894284195, 0.00063035804336864, 
-0.0013869625520111, 0.00202045712842525, -0.000629722921914322, 
0.00264417023419794, -0.000627982918864567, 0.00226102248461246, 
-0.00213032581453632, -0.000250972518509252, -0.00175702811244982, 
-0.00113150616042246, 0.00239144115796108, 0.00564759036144569, 
-0.000375187593796844), ret_accum = c(0.993020566092887, 0.989505893377605, 
0.988753226623007, 0.989253354709311, 0.989876938518382, 0.991249860763206, 
0.989247082915976, 0.988624131352427, 0.991238221853359, 0.99061574118151, 
0.992855545645932, 0.994970651444927, 0.995220361735163, 0.993471731581512, 
0.992347612197022, 0.994720753119834, 1.00033852845748, 0.999963213852009, 
0.993817657624321, 0.990300163702759, 0.991053434618354, 0.991554726188723, 
0.990929691691629, 0.992304074065681, 0.99029916622567, 0.990922780310195, 
0.98830261179011, 0.988923248948984, 0.986687271247554, 0.984585305882741, 
0.984832409736645, 0.986562787966604, 0.987679089838832, 0.99004106626513, 
0.995632412648405, 0.995258863719197, 1.00633565703449, 1.00277385721184, 
1.00353661623102, 1.00404422200554, 1.0034113146543, 1.00480300857199, 
1.00277284717066, 1.00214137812332, 0.999491545720829, 0.998863882102567, 
1.00112233579907, 0.998989619043607, 0.999240337984263, 1.00099603134919, 
0.999863398173165, 0.997472283690435, 0.991838968835255, 0.992211094511407, 
1.02637315186551, 1.02274043182351, 1.02351837839944, 1.02403609128685, 
1.02468160067369, 1.02326040566582, 1.02532785944668, 1.02597353190225, 
1.02326068322812, 1.02390327345873, 1.02158820513538, 1.0237645208606, 
1.02402145762076, 1.02582069210955, 1.02698141454216, 1.02943738016536, 
1.02362353953943, 1.02400759039218, 1.04741093894848, 1.04396768639915, 
1.0454244470931, 1.04780823731956, 1.05111905707853, 1.04886769964243, 
1.05085419149781, 1.05085419149781, 1.05456230962786, 1.05376771288571, 
1.05668123427361, 1.05456230962786, 1.0550920407893, 1.05337041451463, 
1.0521785194014, 1.05469474241822, 1.06118394914581, 1.05853529333863, 
1.0447374250804, 1.04103970697874, 1.04183157288161, 1.04235854889217, 
1.04301560798754, 1.0415689843981, 1.04367342987737, 1.04301620479559, 
1.04577411719809, 1.0451173889155, 1.0474804228309, 1.04524894824592, 
1.04498661948491, 1.04315054861734, 1.04197021734533, 1.04446202780846, 
1.05036072148961, 1.0499666391779), Month = c(5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5)), class = c("data.table", 
"data.frame"), row.names = c(NA, -108L), .internal.selfref = <pointer: 0x0000000007a41ef0>)

我的代码:

library(ggplot2)
library(bdscale)

ggplot(pred_final_price_melt,aes(date_hist,ret_accum,colour=variable,shape=variable))+
  geom_line()+
  geom_point() +
  xlab("Date") +
  ylab("Accumulated return") +
  scale_x_bd(business.dates = pred_final_price_melt$date_hist,
             max.major.breaks = 10,
             labels = date_format("%y-%m-%d"))

produced graph

1 个答案:

答案 0 :(得分:2)

我提供了使用#include "Octave.h" #include <utility> #include <cmath> #include <iostream> Octave::Octave(std::mt19937_64 *rnd, double frequency, double amplitude) : m_frequency(frequency), m_amplitude(amplitude) { //fill in basic array for(int i=0;i<PERMUTATION_TABLE_PART_SIZE;++i){ for(int j=0;j<PERMUTATION_TABLE_PART_COUNT;++j){ m_permutationTable[i+PERMUTATION_TABLE_PART_SIZE*j] = i; } } //shuffle array for(int i=0;i<PERMUTATION_TABLE_SIZE;++i){ int swapWith = ((*rnd)() % (PERMUTATION_TABLE_SIZE-i))+i; std::swap(m_permutationTable[i], m_permutationTable[swapWith]); } } double Octave::noise(double x, double y, double z) { x /= m_frequency; y /= m_frequency; z /= m_frequency; int intX = std::floor(x); int intY = std::floor(y); int intZ = std::floor(z); double floatX = x - intX; double floatY = y - intY; double floatZ = z - intZ; double influence1 = influence(intX, intY, intZ, floatX, floatY, floatZ); double influence2 = influence(intX+1, intY, intZ, floatX-1, floatY, floatZ); double influence3 = influence(intX+1, intY+1, intZ, floatX-1, floatY-1, floatZ); double influence4 = influence(intX, intY+1, intZ, floatX, floatY-1, floatZ); double influence5 = influence(intX, intY, intZ+1, floatX, floatY, floatZ-1); double influence6 = influence(intX+1, intY, intZ+1, floatX-1, floatY, floatZ); double influence7 = influence(intX+1, intY+1, intZ+1, floatX-1, floatY-1, floatZ-1); double influence8 = influence(intX, intY+1, intZ+1, floatX, floatY-1, floatZ-1); double frontUpperInterpolatedValue = interpolate(influence4, influence3, floatX); double backUpperInterpolatedValue = interpolate(influence8, influence7, floatX); double frontLowerInterpolatedValue = interpolate(influence1, influence2, floatX); double backLowerInterpolatedValue = interpolate(influence5, influence6, floatX); double upperInterpolatedValue = interpolate(frontUpperInterpolatedValue, backUpperInterpolatedValue, floatZ); double lowerInterpolatedValue = interpolate(frontLowerInterpolatedValue, backLowerInterpolatedValue, floatZ); return interpolate(lowerInterpolatedValue, upperInterpolatedValue, floatY)*m_amplitude; } double Octave::influence(int x, int y, int z, double distanceX, double distanceY, double distanceZ) { //create un-normalized gradient vector //the ordering of x, y, and z is arbitrary but different to produce different x y and z double gradientX = (m_permutationTable[m_permutationTable[m_permutationTable[x]+y]+z]/static_cast<double>(PERMUTATION_TABLE_PART_SIZE))*2-1; double gradientY = (m_permutationTable[m_permutationTable[m_permutationTable[y]+x]+z]/static_cast<double>(PERMUTATION_TABLE_PART_SIZE))*2-1; double gradientZ = (m_permutationTable[m_permutationTable[m_permutationTable[y]+x]+z]/static_cast<double>(PERMUTATION_TABLE_PART_SIZE))*2-1; //normalize gradient vector double gradientVectorInverseLength = 1/vectorLength(gradientX, gradientY, gradientZ); gradientX *= gradientVectorInverseLength; gradientY *= gradientVectorInverseLength; gradientZ *= gradientVectorInverseLength; //compute dot product double dot = gradientX*distanceX+gradientY*distanceY+gradientZ*distanceZ; return dot; } double Octave::vectorLength(double x, double y, double z) { return std::sqrt(square(x)+square(y)+square(z)); } double Octave::interpolate(double a, double b, double x) { return (b-a)*(6*x*x*x*x*x-15*x*x*x*x+10*x*x*x)+a; } ggplot2软件包的建议。

scales

enter image description here


library(ggplot2)
library(scales)

datas <- unique(pred_final_price_melt$date_hist)

ggplot(pred_final_price_melt,aes(date_hist,ret_accum,colour=variable))+
  geom_line()+
  geom_point(aes(shape = variable)) +
  xlab("Date") +
  ylab("Accumulated return") +
  scale_x_date(breaks = datas,
               labels = scales::date_format("%Y-%m-%d"),
               limits = range(datas)) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90))

enter image description here


  • 数据集
df <- pred_final_price_melt %>% 
  dplyr::mutate(date_hist = as.factor(date_hist))

library(lemon)

ggplot(df,
       aes(date_hist, ret_accum,
           shape = variable, col = variable, group = variable)) +
  lemon::geom_pointline() +
  xlab("Date") +
  ylab("Accumulated return") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90))