I've got a simple CSV file consisting of 3 columns: date, value 1, value 2. I'm using gnuplot to create a stacked bar chart and I'd like the last bar (average values) to be plotted with a different color. I'm using the following script:
set terminal png size 1000, 500
set output "bars.png"
set title "Trends"
set style data histograms
set style histogram rowstacked
set yrange [0:*]
set style fill solid
set boxwidth 0.9
set xlabel "Clicks"
set ylabel "Dates"
set xtics right rotate by 45
set datafile separator ","
plot newhistogram, "data.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff",'' using 3 title "Indirect" linecolor rgb '#00ff00'
Sample input data file:
2023-01-11,234,8756
2023-01-13,54,876
2023-01-14,3333,3566
2023-01-15,543,654
2023-01-16,657,767
2023-01-17,876,88
2023-01-18,1606,55
2023-01-20,888,77
Average,1024,1855
I tried to use a conditional value for linecolor but I'm getting an error I can't understand:
gnuplot> plot newhistogram, "/tmp/csv.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff", '' using 3 linecolor rgb (strcol(1) eq "Average" ? '#ff50ff' : '#43FF00') ^ line 0: stringcolumn() called from invalid context
I couldn't find a way to use a conditional for the linecolor directive, so I ended up with a conditional for using. The updated plot command which does the trick is as follows:
plot newhistogram, "data.csv" \
using (strcol(1) ne 'Average' ? $2 : NaN):xtic(1) title "Direct" lc rgb "#0000ff", \
'' using (strcol(1) ne 'Average' ? $3 : NaN) title "Indirect" lc rgb '#00ff00', \
'' using (strcol(1) eq 'Average' ? $2 : NaN) notitle lc rgb '#0088ff', \
'' using (strcol(1) eq 'Average' ? $3 : NaN) notitle lc rgb '#00ff88'
The first 2 lines plot the rows not starting with 'Average' and the last 2 plot the last row which does. Each column's color is plotted with an arbitrary color. The output now looks as intended.