【问题标题】:How to plot timelines with R or Gnuplot如何使用 R 或 Gnuplot 绘制时间线
【发布时间】:2012-10-17 19:32:41
【问题描述】:

使用 R 或 Gnuplot 绘制如图所示的时间线的最佳且简单的方法是什么:

【问题讨论】:

  • 样本数据会很好! :) dput() 请或创建虚拟数据。
  • this question 有类似的情节。使用带有“空白”的堆积条形图。

标签: r gnuplot timeline


【解决方案1】:

我的解决方案使用“烛台”绘图样式,该样式似乎在 X 和 Y 列输入方面是固定的。因此,必须手动旋转输出图像,但这似乎是生成从基轴“浮动”的条形的唯一选项。可能您会想要稍微调整样式,但这应该可以帮助您入门。

样本输入数据;注意我必须在第 1 列中添加任意 Y 轴(旋转 X 轴)坐标:

1 proj1     768.83      2011-05-12 2013-06-19
2 proj2     426.79      2011-06-15 2012-08-15
3 proj3     429.89      2011-06-17 2012-08-20
4 proj4     318.02      2012-01-06 2012-11-19
5 proj5     278.72      2011-11-10 2012-08-15
6 proj6     231.16      2011-11-14 2012-07-02
7 proj7     230.27      2011-11-15 2012-07-02
8 proj8     230.23      2011-11-15 2012-07-02
9 proj9     581.81      2011-11-15 2013-06-19
10 proj10   230.08      2011-11-15 2012-07-02

(为下面的脚本保存为“dates4.dat”。)

gnuplot 脚本:

# horizontal timelines / time bars with gnuplot ; manually rotate result image at end

# size specification controls landscape vs. portrait, this is for A4 paper size
set terminal pdf size 21cm,29.7cm
set output 'dates.pdf'

# margins get confused so set explicitly
set lmargin at screen 0.04
set bmargin at screen 0.07
set tmargin at screen 0.97

# rotation will swap Y and X axis

# input Y data as date values
set ydata time
set timefmt "%Y-%m-%d"

# y coordinates now specified in time values
set yrange ['2011-03-01':'2014-01-01']

# normal Y axis labels end up on top of graph, don't want that
unset ytics

# format y2 axis for time scale, this will appear along bottom of graph
set y2data time
set format y2 "%b %Y"
set y2tics font "Courier, 8"
set y2tics rotate by 90 

# y2tics 'incr' measured in seconds for y2data time, this is 4 months = 4*30*24*60*60
set y2tics '2011-05-12',10368000,'2013-12-01'

set xrange [-1:28]
set xtics font "Courier, 8"
set xtics rotate by 90 offset 0,-5 out nomirror

# cannot rotate key (dataset label), so must create manually
unset key
set label 'elapsed project time' at 0,'2013-06-15' rotate font "Courier, 8"
set object 1 rect from -0.1,'2013-11-01' to 0,'2013-12-01' fillstyle solid noborder fillcolor rgb "red"

# note duplication of date columns 4 and 5 so don't get whiskers
plot "dates4.dat" using 1:4:4:5:5:xticlabels(2) with candlesticks fillcolor rgb "red"

希望有帮助!

【讨论】:

    【解决方案2】:

    不确定 r,但在 gnuplot 中,您可以将轴声明为时间格式。要实现您的 y 轴示例:

    set ydata time # tells gnuplot that y is a time series
    set ylabel 'Submission day in August'
    set timefmt '%Y-%m-%d' # this should be set to the format of your date series
    set format y '%d' # sets the axis labels format
    

    除此之外,gnuplot 支持多个系列、网格和任意形状,所以你应该没问题。

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      使用@robm 的示例数据和包vistime,你会得到

      install.packages("vistime")
      library(vistime)
      data<-read.csv(text="proj1,768.83,2011-05-12,2013-06-19
                      proj2,426.79,2011-06-15,2012-08-15
                      proj3,429.89,2011-06-17,2012-08-20
                      proj4,318.02,2012-01-06,2012-11-19
                      proj5,278.72,2011-11-10,2012-08-15
                      proj6,231.16,2011-11-14,2012-07-02
                      proj7,230.27,2011-11-15,2012-07-02
                      proj8,230.23,2011-11-15,2012-07-02
                      proj9,581.81,2011-11-15,2013-06-19
                      proj10,230.08,2011-11-15,2012-07-02", header=F, sep=",")
      data$tooltip=paste(data$V1, data$V2)
      vistime(data, events="V1", start="V3", end="V4")
      

      或者,或者,

      vistime(data, events="V2", start="V3", end="V4", groups="V1")
      

      【讨论】:

        猜你喜欢
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 2017-07-25
        • 2011-06-02
        • 2016-01-28
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        相关资源
        最近更新 更多