! Demo about reading data from a file. ! Author: Francois Tonneau size 15 15 set font ss hei 0.5 amove 3 3 ! We will plot the percent salary gap between women and men as a function of ! the number of men and women per college. We begin by preparing the plot area ! -- no dataset is loaded: begin graph size 10 10 fullsize xaxis min 55 max 115 ftick 60 dticks 10 yaxis min 70 max 190 ftick 80 dticks 20 axis grid lwidth 0.03 color #c8c8c8 labels color black xtitle "Number of women" dist 0.8 ytitle "Number of men" dist 0.8 end graph ! We now use GLE's file-handling routines to draw a bubble plot. First, we open ! ('fopen') the bubbles.dat file in the 'read' mode, creating a handle that we ! call, 'stream' (without quotes; any other name would do). Then we run a loop ! to read data from the file until its end, feof(stream). In each step of the ! loop, we read the 'women', 'men', and 'gap' variables and we draw a circle ! in accordance with these values. The radius is a square root, so that the ! area inside the circle is a linear function of the salary gap. scaling = 0.125 set color #55a868 just cc lwidth 0.05 fopen "bubble.dat" stream read until feof(stream) fread stream women men gap amove xg(women) yg(men) radius = sqrt(gap) circle radius*scaling next fclose stream ! Now we add labels to the bubbles. This is done in a separate loop to have the ! circles behind the labels -- otherwise the former would obscure the latter. ! Note that the positions of two labels are adjusted to avoid label overlap. adj = 0.07 set color black just cc hei 0.45 fopen "bubble.dat" stream read until feof(stream) fread stream women men gap amove xg(women) yg(men) if gap = 55 then rmove adj -adj if gap = 58 then rmove -adj adj write gap next fclose stream ! We end with a short plot description: set color black hei 0.5 just cl amove xg(65) yg(180) begin box add 0.1 fill white nobox write "Gender Gap in Earnings (%)" end box ! Done. We have learned about file handling and 'until ... next' loops.