我正在尝试在一张图表中创建三个直方图。选择适当的数据有效,但“平滑频率”无法按预期工作。
$Data <<EOD
Med Gender Age
4 f 33.14
4 f 53.81
4 f 32.99
4 m 39.78
4 f 25.06
2 m 51.06
4 f 39.93
4 f 44.92
2 m 45.68
2 m 73.47
2 m 61.65
4 m 26.82
4 f 24.93
4 f 29.79
3 m 80.54
3 m 81.42
2 f 71.9
2 f 73.18
3 m 64.76
4 m 33.45
2 m 58.92
2 f 73.51
4 f 36.09
EOD
数据集由三个不同的组组成。以下“函数”用于选择属于每个组的年龄值。
GROUP_LABELS = "2 3 4"
GROUP_NAMES = "Med_02 Med_03 Med_04"
is_true(c,x) = ( c == x ) ? 1.0 : NaN
age = "( column(\"Age\") )"
selected_age_values = "is_true( column(\"Med\"), i ) * @age"
x_min = 0
x_max = 100
n_bins = 20
bin_width = 1.*(x_max - x_min)/n_bins
bin(col) = floor(column(col)/bin_width)*bin_width
set boxwidth 0.5
set xtics out
set xrange[x_min:x_max]
plot for [i in GROUP_LABELS] $Data u ( @selected_age_values ):(1) smooth freq w boxes lc i-1 ti word( GROUP_NAMES, i-1 ) noenhanced
不幸的是,生成的图表仅显示每个数据点的一个尖峰,至少颜色正确。
我试着稍微简化一下你的脚本,但是三个直方图放在一个图中让它再次变得有点复杂(绘制和阅读)。
由于您有三个直方图,每个 binwidth(这里:5.0)被分成 3 个条。 例如:从 50 到 55 的范围包含第一组中的一个条,第二组中没有条,第三组中有一个条。 请注意,条形图以该值为中心绘制,因此您必须设置一些偏移量为半个框宽度的倍数。
如果
inGroup()
是否与组相同,函数 i
仅返回 1 或 0。 smooth freq
然后将总结 0 或 1.
我希望剩下的是不言自明的。
会有不同的表示方式:例如,每个范围(例如 50-55)有一个 xtic,对应于以该 tic 为中心的范围的 3 个柱。
脚本:
### three histograms in one plot
reset session
$Data <<EOD
Med Gender Age
4 f 33.14
4 f 53.81
4 f 32.99
4 m 39.78
4 f 25.06
2 m 51.06
4 f 39.93
4 f 44.92
2 m 45.68
2 m 73.47
2 m 61.65
4 m 26.82
4 f 24.93
4 f 29.79
3 m 80.54
3 m 81.42
2 f 71.9
2 f 73.18
3 m 64.76
4 m 33.45
2 m 58.92
2 f 73.51
4 f 36.09
EOD
GROUP_LABELS = "2 3 4"
GroupName(i) = sprintf("Med_%02d",int(i))
x_min = 0
x_max = 100
n_bins = 20
bin_width = real(x_max - x_min)/n_bins
myBoxwidth = bin_width/words(GROUP_LABELS)
bin(x) = floor(x/bin_width)*bin_width
inGroup(col,i) = column(col) == int(i)
set boxwidth myBoxwidth
set xlabel "Age"
set xrange[x_min:x_max]
set xtics 10 out
set mxtic 2
set ylabel "Count"
set ytics 1
set grid x, mx, y
set style fill transparent solid 0.3
plot for [i in GROUP_LABELS] $Data u (bin(column("Age"))+(i-1.5)*myBoxwidth):(inGroup(1,i)) \
smooth freq w boxes lc i-1 ti GroupName(i) noenhanced
### end of script
结果: