I know this is nearly 8 years old, but I had the same issue and based on Christoph's comment above I was able to figure out a way.
Below is the graph I wanted:

However selecting certain rows only by way of the ternary and NaN
does not play nice with smooth freq
, and my histogram was wrong (seemed bins were drawn over one another and frequencies weren't as high as they should've been).
This did not work:
plot \
'filename' using ($1 >= 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "green", \
'filename' using ($1 < 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "red"
In the manual for gnuplot 5.4.2, this section describes an experimental feature which, combined with set table
, allowed me to achieve the above graph.
[EXPERIMENTAL] To select only a subset of the data points for tabulation you can provide an input filter condition (if ) at the end of the command. Note that the input filter may reference data columns that are not part of the output. This feature may change substantially before appearing in a released version of gnuplot.
plot <file> using 1:2:($4+$5) with table if (strcol(3) eq "Red")
-- p207 gnuplot v5.4.2 manual
So the approach is:
- Use
set table $my_data_block_green
to set the next plot command to output to the $my_data_block_green
data block. We'll create one data block for each colour and this is the first.
- Use
plot <file> with table if (<condition_for_green>)
to write to the green data block only rows matching <condition_for_green>
.
- Use
set table $my_data_block_red
(as in point 1).
- Use
plot <file> with table if (<condition_for_red>)
to write to the red data block only rows matching <condition_for_red>
.
- Cease writing plot commands to tables with
unset table
.
- Plot as normal, referencing the data blocks instead of
<file>
.
Relevant code (not the full code for graph above):
set table $db1
plot <filename> using 7:(1) with table if ($7 >= 0)
set table $db2
plot <filename> using 7:(1) with table if ($7 < 0)
unset table
plot \
'$db1' using $1:.. .. fillcolor rgb "green", \
'$db2' using $1:.. .. fillcolor rgb "red"
Hope that saves someone a few mins.