我试图使用一个简单的数据文件(.example
)建立一种条形图,只包含0或1。以下是.example
中包含的数据:
dest P1 P2 P3 P4 P5 NA
D1 0 1 1 0 0 0
D2 0 0 1 0 0 0
D3 0 1 0 1 0 0
""
GPV 1 1 1 1 1 1
,这里是我使用的代码:
set style histogram rowstacked title textcolor lt -1
set datafile missing 'nan'
set style data histograms
plot '.example' using ( $2==0 ? 1 : 0 ) ls 17 title 'NA',
'' using ( $2==1 ? 1 : 0 ) ls 1,
for [i=3:5] '.example' using ( column(i)==0 ? 1 : 0) ls 17 notitle,
for [i=3:5] '' using ( column(i)==1 ? 1 : 0) ls i-1
,其中最后两个命令遍历可能大量的根据column(i)
的值堆叠白色或彩色框的列。为了在直方图的不同列之间保持相同的颜色顺序,我需要使用两个命令将两个迭代合并为一个迭代。
有可能吗?有什么建议吗?
您可以使用嵌套循环,我认为这是您想要实现的。您可以在大量列上使用外部循环迭代,并在两个选项(白色与彩色)for [i=3:5] for [j=0:1]
上使用内部循环迭代,并告诉gnuplot,如果该列的内容与使用1/0
的j
的值不匹配,则忽略该列(或者使用对直方图有效的技巧,将其设置为0
,就像您已经在做的那样):
set style histogram rowstacked title textcolor lt -1
set datafile missing 'nan'
set style data histograms
plot '.example' using ( $2==0 ? 1 : 0 ) ls 17 title 'NA',
'' using ( $2==1 ? 1 : 0 ) ls 1,
for [i=3:5] for [j=0:1] '.example' using ( column(i) == j ? 1 : 0 )
ls ( j == 0 ? 17 : i-1 ) notitle
上面的代码相当于您已经拥有的,只有j
的值允许根据您是否有0
或1
作为列的值来切换样式。