Question

I'm wondering if there is a way to draw duplicated points in gnuplot without letting them be invisible, putting the point to (X+rand(-0.5;0.5),Y+rand(-0.5;0.5)). So the dot is not placed at X/Y of the previous point but - depending on randomize - a little bit up, down, left or right. Then no dot becomes invisible.

This effect is called "jitter". This is a good example: http://www.ats.ucla.edu/stat/spss/faq/jitter.htm

Was it helpful?

Solution

To add random deviations to a data point, just use the rand function:

myrand(x) = (x + rand(0) - 0.5)
plot 'data.dat' using (myrand($1)):(myrand($2))

rand(0) generates a value in the range [0:1].

If you want to plot each point multiple times, you need to iterate as many times:

plot for [i=0:4] 'data.dat' using (myrand($1)):(myrand($2))

In order to take into account the number of occurences, like shown in your link, one has to trick a bit.

Consider the data file (taken from http://www.ats.ucla.edu/stat/spss/faq/jitter.htm), with the third column holding the number of occurences:

1 1 4
1 2 7
1 3 6
2 1 9
2 2 5
2 3 11
3 1 1
3 2 2
3 3 3
4 1 12
4 2 8
4 3 10

One way is to extract the maximum number of occurences from the data file and then plot with iterations. In each iteration a point is added, if its number of occurences is higher than the current iteration index. A point is skipped by setting one coordinate to 1/0:

stats 'data.dat' using 3 nooutput
set style line 1 pointtype 6 linecolor -1
myrand(x) = x + 0.3 * (rand(0) - 0.5)
plot for [i=0:int(STATS_max)-1] 'data.dat' \
    using (myrand($1)):(i < $3 ? myrand($2) : 1/0) linestyle 1 notitle

That gives (with 4.6.3):

enter image description here

where I used the additional settings:

set terminal pngcairo
set output 'data.png'
set xtics 1
set ytics 1
set autoscale fix
set offset 0.5,0.5,0.5,0.5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top