1 SESSION 5 Graphs for data analysis
2 Objectives To be able to use STATA to produce exploratory and presentation graphs In particular Bar Charts Histograms and Box Plots Scatter Plots and Scatterplot Matrices
3 Graphics Menu Dialogue resulting from Two-way graph (scatter, line, etc.) and its option Create
4 Demonstration: Bar charts Open the file K_combined_labelled.dta We can demonstrate plotting a bar chart for a categorical variable using the menu sequence Graphics Histogram
5 Graphics Histogram dialogue
6 Completing the dialogue box In the histogram dialogue box, select the variable q34. This has codes 1 to 7 for main sources of drinking water. Select discrete Specify that the y axis should show Frequency Note that after clicking on Submit, the command histogram q34, discrete frequency appears in the Command & Results windows
7 Results
8 Improving your bar chart Use Graphics Histogram Ensure dialogue is as shown in slide 5 Click on Bar Properties and select Bar gap as 30 (see Figure on left). Click Accept, then OK. Is the graph better?
9 Learning to add bar labels Return to the full Histogram dialogue above Select option X-Axis, then click on Major tick/label properties. In the resulting dialogue, give the Axis rule as Custom and give the Custom rule as 1(1)7 – see figure on left of slide below. Move also to the Labels option and check Use value labels below and click Accept See dialogue as shown in next slide
10 Adding bar labels The 1(1)7 Rule means – place ticks (and therefore labels) from 1 to 7 in steps of 1 on the X axis
11 Bar chart with labels Look at the resulting graph) Is it better?
12 Using Stata commands The command to produce the above, on one line, is: histogram q34, discrete frequency gap(30) xlabel( 1(1)7, valuelabel) If there were no labels, or we wanted shorter ones, then use the command (again on one line): histogram q34, discrete frequency gap(30) xlabel(1 "pipe" 2 "pub" 3 "well1" 4 "well2" 5 "riv" 6 "vend" 7 "oth") BUT DO NOT TYPE THESE YET!
13 Bar charts to aid comparisons e.g. employment across gender Our aim now is to display results of this table in the form of two bar charts, one for males and one for females. Above produced using the command.tabulate q130 q11, column nofreq
14 Menu sequence for producing the corresponding charts Start with the Graphics Histogram dialogue The variable q130 gives the employment class of the head of the household. Select this variable, request Y- Axis to display percent and ensure bar gap is still at 30. In the X-Axis option, click on Major tick/label properties, and ensure Custom Rule is now 1(1)11, then move to Labels and ensure use value labels are ticked, and Angle is 45.
15 producing the charts cont… Now go to the By option, and select q11 (i.e. sex), as variable for subgraphs. Click on Subgroup organisation and in the resulting dialogue, request Rows/Columns to be 2 Rows. Click Accept, then OK. See next slide for the resulting graph.
16 Bar charts for comparisons This is an example of a graph that would be difficult with a spreadsheet Employment for Males shown separately from that for females
17 Do files Sometimes it is useful to use commands Stata commands can become quite complicated. Sometimes useful to run a set of commands. They can be saved in a Do file. What is a Do file? A set of commands In other packages these are called syntax or batch files Why use do files Good practice in data management Repeatability of steps
18 Opening a new Do file Through the Do-file Editor icon or Window, or press 8
19 Example of a Do-file To run a Do-file, use the Do current file icon or use Tools Do
20 Making a Do file Open a new Do file Copy & paste, or type in one or more of the graphics commands on slide 12 or others you have produced Include some comments, preceded by a * or included between /* */ to indicate the analysis being done Then save the Do file. This will have.do as its extension.
21 Your Turn Now…. Go through Section 6.3 Use the same histogram dialogue (page 76) to go through Section 6.4 Do the following exercises from the guide, saving your commands in a Do file as you go along Histogram and box plots for a continuous variable, in sections and Scatter plots, sections & Load your own data into Stata, and produce graphs that address an objective of interest.