0

Three charts

I have three different lines, where colours and their dashes means different things which I want on one plot, instead of three. How do I accomplish that?

set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD

set format x '%Y'
set xdata time
set timefmt "%Y-%m-%d"
plot '$sample' u 1:2 title "UK/Business" with linespoints dt 3 lw 5 pt 7 lc "red"
  • plot '$sample' u 1:2 title "USA/Social" with linespoints dt 3 lw 1 pt 7 lc "blue"
  • plot '$sample' u 1:2 title "UK/Social" with linespoints dt 3 lw 5 pt 7 lc "blue"

E.g. blue is "Social" and lw 1 (fine dots) is for USA.

hendry
  • 9,725
  • 18
  • 81
  • 139

1 Answers1

1

Please check the manual or help plot. Plot elements for the same plot are separated by comma.

Syntax:

  plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}

To improve readability you can split long code lines into several lines by using \. Note that no character is allowed after \ in the same line except carriage return/line feed.

Try this:

plot '$sample' u 1:2 title "UK/Business" w lp dt 3 lw 5 pt 7 lc "red", \
     '' u 1:2 title "USA/Social" w lp dt 3 lw 1 pt 7 lc "blue", \
     '' u 1:2 title "UK/Social" w lp dt 3 lw 5 pt 7 lc "blue"

Addition: (a filter depending on two (string)-columns)

You can implement a filter by using the ternary operator. Check help ternary and help strcol. A value which does not pass the filter will be set to NaN. If you still want to your points being connected with lines, you need to set datafile missing NaN.

Code:

### filtered data with different line colors
reset session
set datafile separator comma

$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD

myTimeFmt = "%Y-%m-%d"
set format x '%d.%m.%Y' timedate
set key top left
set datafile missing NaN

myFilter(colData,colFilter1,key1,colFilter2,key2) = (strcol(colFilter1) eq key1) && (strcol(colFilter2) eq key2) ?  column(colData) : NaN

plot $sample u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
     '' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"USA",4,"Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
     '' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
### end of code

or a bit shortened if the columns 2,3,4 do not change within the plot command...

...
...
myTime(col) = timecolumn(col,myTimeFmt)
myFilter(key1,key2) = (strcol(3) eq key1) && (strcol(4) eq key2) ?  column(2) : NaN

plot $sample u (myTime(1)):(myFilter("UK","Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
     '' u (myTime(1)):(myFilter("USA","Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
     '' u (myTime(1)):(myFilter("UK","Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • 1
    I understand you want the 3 lines in one plot. But not sure whether you also want to separate/filter by keyword? – theozh Aug 25 '20 at 06:06
  • yes, I want only "Social" to be blue, and only fine dots for USA. I need some sort of filter. Only if matches this string, plot with these colors. https://s.natalian.org/2020-08-25/not-what-i-want.png – hendry Aug 25 '20 at 08:21
  • I wonder if the line color and dots can be done with `lc variable` like https://stackoverflow.com/a/62823958/4534 – hendry Aug 26 '20 at 08:37
  • yes of course. check `help points`. You can have `ps var pt var lc var` (pointsize, pointtype, linecolor) as variable. As far as I know, you cannot have this with linewidth (`lw`) and dashtype (`dt`). But maybe you can do something with linestyle (`ls`) or linetype (`lt`). Check help for these keywords. – theozh Aug 26 '20 at 09:29