更改日期的时间部分在图形上的显示方式

时间:2018-08-07 02:27:36

标签: date datetime graph stata

这里是dataex制作的可复制示例

* Example generated by -dataex-. To install: ssc install dataex
clear
input str23 eventname str10(eventdate scrapedate) byte pricepart float(thpercentile median v7 mean) str5 timestamp str19 datetime
"Home Team vs. Away Team" "2016-01-03" "2015-12-04" 1   117.02 153.635  215.135 178.74034 "07:59" "2015-12-04 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-04" 2   117.02 153.635  215.135 178.74034 "16:00" "2015-12-04 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-04" 3   117.02 153.635  215.135 178.74034 "23:59" "2015-12-04 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-05" 1   117.02 153.635  215.135 178.64935 "07:59" "2015-12-05 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-05" 2  110.335  150.62  210.775  175.1715 "16:00" "2015-12-05 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-05" 3        .       .        .         . "23:59" "2015-12-05 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-06" 1  110.335  149.55 208.6575 174.38936 "07:59" "2015-12-06 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-06" 2   110.99  151.32      210 175.17485 "16:00" "2015-12-06 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-06" 3        .       .        .         . "23:59" "2015-12-06 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-07" 1   110.77   151.5   214.98  176.9686 "07:59" "2015-12-07 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-07" 2   107.66  147.99   205.75  171.1186 "16:00" "2015-12-07 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-07" 3   104.97  145.01    205.2  169.3728 "23:59" "2015-12-07 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-08" 1  105.415  145.33  205.265  169.3202 "07:59" "2015-12-08 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-08" 2   104.97  145.65   205.54  169.6171 "16:00" "2015-12-08 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-08" 3        .       .        .         . "23:59" "2015-12-08 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-09" 1   104.97  145.92   207.24 170.63264 "07:59" "2015-12-09 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-09" 2   104.97  147.16  205.435 169.48587 "16:00" "2015-12-09 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-09" 3        .       .        .         . "23:59" "2015-12-09 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-10" 1   104.97  147.16  208.315 173.76367 "07:59" "2015-12-10 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-10" 2   104.99  147.99      210 176.39133 "16:00" "2015-12-10 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-10" 3        .       .        .         . "23:59" "2015-12-10 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-11" 1   105.69  148.75   214.67  174.9753 "07:59" "2015-12-11 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-11" 2   105.69  148.75 214.2775  174.9714 "16:00" "2015-12-11 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-11" 3   105.69 148.965  214.825 182.88837 "23:59" "2015-12-11 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-12" 1   109.76   151.5   214.98   177.088 "07:59" "2015-12-12 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-12" 2   109.57   151.5  212.325 176.69136 "16:00" "2015-12-12 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-12" 3 110.1225  148.75   207.97 175.44093 "23:59" "2015-12-12 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-13" 1   110.55  148.75      210   175.927 "07:59" "2015-12-13 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-13" 2   110.55  148.75  209.995 176.09927 "16:00" "2015-12-13 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-13" 3   110.99   151.5   207.97  175.9855 "23:59" "2015-12-13 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-14" 1   110.99   151.5   207.97 176.13016 "07:59" "2015-12-14 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-14" 2   110.99   151.5   207.49 175.95207 "16:00" "2015-12-14 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-14" 3   113.65  151.32   207.25  176.1622 "23:59" "2015-12-14 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-15" 1 114.1825  151.41   207.49 176.63448 "07:59" "2015-12-15 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-15" 2   114.36  151.32   208.21 176.49957 "16:00" "2015-12-15 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-15" 3   112.24     150      210 174.69102 "23:59" "2015-12-15 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-16" 1   113.65  151.32    210.4  175.1356 "07:59" "2015-12-16 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-16" 2  113.065  151.32  211.775 176.25023 "16:00" "2015-12-16 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-16" 3   115.91   151.5  209.105  176.2387 "23:59" "2015-12-16 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-17" 1   114.36 150.545   207.25 173.55644 "07:59" "2015-12-17 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-17" 2   114.36  151.09   207.25  174.5637 "16:00" "2015-12-17 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-17" 3  112.135  148.75   207.25 172.02206 "23:59" "2015-12-17 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-18" 1   114.36  149.92   207.87  175.4566 "07:59" "2015-12-18 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-18" 2   113.65  148.75   207.25 173.69534 "16:00" "2015-12-18 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-18" 3   118.33   151.5   214.95  180.4201 "23:59" "2015-12-18 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-19" 1   118.56  152.99   215.71 180.30553 "07:59" "2015-12-19 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-19" 2   119.31  152.99   215.12   179.263 "16:00" "2015-12-19 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-19" 3   119.31   151.5 214.2325 178.78603 "23:59" "2015-12-19 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-20" 1 119.4925  152.99   215.71 181.24663 "07:59" "2015-12-20 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-20" 2   119.25  152.99   215.85  181.3266 "16:00" "2015-12-20 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-20" 3    119.5  154.05   216.61   181.498 "23:59" "2015-12-20 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-21" 1 120.2575   154.6  216.195 181.61243 "07:59" "2015-12-21 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-21" 2  119.705  154.05   216.46  181.6927 "16:00" "2015-12-21 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-21" 3   114.36  147.71   216.31 177.83945 "23:59" "2015-12-21 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-22" 1   114.36  148.75   217.78  179.7802 "07:59" "2015-12-22 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-22" 2  115.155  148.75   218.95 182.94615 "16:00" "2015-12-22 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-22" 3   114.12  147.85 214.8325 178.60695 "23:59" "2015-12-22 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-23" 1   114.36  148.44 215.1375  178.2195 "07:59" "2015-12-23 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-23" 2   114.36  147.71   214.28 176.39894 "16:00" "2015-12-23 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-23" 3    114.3  148.75 216.6775  181.7427 "23:59" "2015-12-23 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-24" 1   114.12  147.71   214.61  179.2775 "07:59" "2015-12-24 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-24" 2  113.885  147.71  214.445   177.577 "16:00" "2015-12-24 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-24" 3 118.4625  148.75   218.95  183.9876 "23:59" "2015-12-24 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-25" 1   118.33  148.75   217.78  182.2171 "07:59" "2015-12-25 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-25" 2   118.33  148.75   218.95  184.2348 "16:00" "2015-12-25 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-25" 3   118.33  148.75   218.95  184.0091 "23:59" "2015-12-25 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-26" 1  118.595  149.65   218.95 184.72743 "07:59" "2015-12-26 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-26" 2  116.575  148.75   218.95 183.86197 "16:00" "2015-12-26 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-26" 3   115.42  148.75   218.95  182.9738 "23:59" "2015-12-26 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-27" 1 115.5625  148.75 218.6575 183.38004 "07:59" "2015-12-27 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-27" 2   114.59  148.75   218.95  185.2042 "16:00" "2015-12-27 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-27" 3    119.5  149.93   218.95 186.70554 "23:59" "2015-12-27 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-28" 1    119.5  149.92   218.95  185.5134 "07:59" "2015-12-28 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-28" 2    119.5 151.195  219.345  188.6705 "16:00" "2015-12-28 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-28" 3 113.8275  141.56   202.37 170.03584 "23:59" "2015-12-28 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-29" 1   115.12   142.9  204.655 172.76167 "07:59" "2015-12-29 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-29" 2   115.42   142.9  204.655  171.8042 "16:00" "2015-12-29 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-29" 3  109.405  137.05  192.455 171.29094 "23:59" "2015-12-29 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-30" 1   108.86  131.96   181.35  161.5101 "07:59" "2015-12-30 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-30" 2   108.86  132.72   182.93  162.9379 "16:00" "2015-12-30 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-30" 3   103.32  128.86   180.84  163.9678 "23:59" "2015-12-30 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-31" 1   102.76  124.18   177.13 155.92645 "07:59" "2015-12-31 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-31" 2   103.12  125.35   176.13  156.4449 "16:00" "2015-12-31 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2015-12-31" 3   104.85  128.08      178  161.6073 "23:59" "2015-12-31 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-01" 1   104.86 130.025  177.985 161.02515 "07:59" "2016-01-01 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-01" 2   101.95   119.5  158.695 144.57149 "16:00" "2016-01-01 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-01" 3   104.86 127.885   177.12  163.0666 "23:59" "2016-01-01 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-02" 1   105.46   131.2   168.64 145.27736 "07:59" "2016-01-02 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-02" 2   105.46  131.19    166.3 144.06587 "16:00" "2016-01-02 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-02" 3  108.385 130.035 154.0075 138.77951 "23:59" "2016-01-02 23:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-03" 1 108.0925  129.74 148.7475 136.77663 "07:59" "2016-01-03 07:59:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-03" 2   109.55  125.35   145.57 134.23042 "16:00" "2016-01-03 16:00:00"
"Home Team vs. Away Team" "2016-01-03" "2016-01-03" 3        .       .        .         . "23:59" "2016-01-03 23:59:00"
end

这些是我的do文件的内容,向您显示了我的努力:

gen double eventtime = clock(datetime, "YMDhms")
tsset eventtime, format(%tcNN/DD/CCYY_HH:MM:SS)


twoway (connected mean eventtime, msymbol(point) mfcolor(none)) ///
(connected median eventtime, msymbol(point) mfcolor(none)) /// 
(connected thpercentile eventtime, msymbol(point) mfcolor(none)) ///
(connected v7 eventtime, msymbol(point) mfcolor(none)), ///
ytitle(Price in USD) ///
xtitle(Scrape Date) ///
xlabel(, angle(vertical) /// 
format(%tcNN/DD/CCYY_HH:MM:SS)) xmtick(##10, angle(vertical))

我的努力结果是这样的:

enter image description here

此输出的问题是我的csv文件中根本没有日期刻度标签的X轴HH:MM:SS部分!我的日期中代表日期的列具有3个模式"MM/DD/YYYY 07:59:00""MM/DD/YYYY 16:00:00""MM/DD/YYYY 23:59:00"。。但是我的图形输出在轴标签中具有日期的以下HH:MM:SS位置数据集中不存在的那些。您在X轴标签中看到的所有HH:MM:SS部分(例如05:46:4000:40:0019:33:20等都不存在于我的数据中!

问题:

  1. 是否有可能,使用我的csv中的"MM/DD/YYYY 07:59:00""MM/DD/YYYY 16:00:00""MM/DD/YYYY 23:59:00"模式显示12个特定日期,而不是用Stata来显示自己查看相对位置并相应地在X轴标签上设置HH:MM:SS?

  2. 是否可以在这些特定X轴日期点的4个点添加4条参考线,并在顶部添加自定义文本标记?我遇到的问题是,我似乎无法理解如何选择X轴上的位置,因为它们都是日期时间值,以及如何在特定的Y坐标处添加带有文本标记的多条参考线。

  3. 如何在图形内部而不是外部设置较小的图例?

我希望我的输出类似于以下内容。

enter image description here

交叉发布于Statalist

1 个答案:

答案 0 :(得分:3)

以下代码片段获取最小和最大原始日期/时间值,根据所需步骤(在本例中为12)找到观测值之间的间隔,然后遍历观测值以获取每个步骤的日期/时间值,并将所有内容插入列表:

sort eventtime
summarize eventtime
local min = r(min)
local max = r(max)

local plus = _N / 12
local total = _N / `plus'

local dtlist `dtlist' `min'
local counter = 0

forvalues i = 1 / `total' {
    local counter = `counter' + `plus'
    local dtlist `dtlist' `=eventtime[`counter']' 
}

local dtlist `dtlist' `max'

然后使用生成的列表绘制图形:

twoway (connected mean eventtime, msymbol(point) mfcolor(none)) ///
(connected median eventtime, msymbol(point) mfcolor(none)) /// 
(connected thpercentile eventtime, msymbol(point) mfcolor(none)) ///
(connected v7 eventtime, msymbol(point) mfcolor(none)), ///
ytitle(Price in USD) xtitle(Scrape Date) ///
xlabel(`dtlist', format(%tcNN/DD/CCYY_HH:MM:SS) angle(vertical))

此外,这是如何在选项xlines()中为参考线选择和插入值的方法:

display %15.0f eventtime[3]
 1764892740000

xline(1765238340000 1765670340000 1766102340000 1766534340000, lwidth(vthin) ///
lcolor(gray))

可以为每个参考线添加一个文本框,如下所示:

text(240 1765238340000 "CUSTOM" "TEXT" "MARKER 1", ///
orientation(horizontal) size(vsmall) justification(left) lwidth(vthin) fcolor(white) box)

类似地,可以通过以下方式添加图例:

legend(pos(1) ring(0) col(1) symxsize(small) size(vsmall) rowgap(zero))

显然,您必须使用各种文本框和图例选项的值才能获得所需的外观。