You are on page 1of 35

GRAPHICS IN STATA

Sandra Jumba
Contents

• Introduction
• Pie charts
• Bar graphs
• Histogram
• Box plot
• Twoway graphs (scatter plots)
• Regression fit and splines
• Line graphs
• Area plots
• Scatter matrix
• Combining graphs
Introduction

• STATA draws very good graphs

• By either using the point and click or the commands, you


can enhance the features of these graphs

• In this session, we shall learn how to draw the different


types of graphs for both categorical variables and
quantitative variables
PIE CHARTS

• Pie charts can summarize categorical or qualitative data.

• There are multiple options you can use

• Using the blood pressure dataset, let’s look at these


options critically.
Pie - options
• graph pie sex agegrp
• This option graphs the different y variables into the same
pie chart

• graph pie sex, over( agegrp)


• In this syntax, you supply a single y-variable and an over()
option.
• The y variable corresponds to the entire population of
gender types, the entire pie corresponds to the entire
population, and each slice corresponds to the percentage
of the pop for each (agegrp)
Pie options cont.
• graph pie, over(agegrp)
• Here no y-variable is supplied. The observation itself serves
as the y-variable.
• This is a visual frequency of the age groups

• graph pie, over(agegrp) missing


• This produces a chart with three slices corresponding to
freq of agegrp that are non-missing, and a last one where
age group is missing
Pie options cont.
• graph pie, over(agegrp)
• The slices are ordered according to the levels of agegrp,
rotating clockwise, starting the first slice, which is
positioned at 90 degrees

• Other options

• graph pie, over(agegrp) noclockwise

• graph pie, over(agegrp) angle0()

• graph pie, over( sex) sort


Pie options cont.
• graph pie, over(agegrp) sort descending

• graph pie, over( agegrp) sort(sex)


Changing pie slice color & exploding
• graph pie, over( agegrp) pie(3, explode)
• Explodes the third slice

• graph pie, over( agegrp) pie(3, explode(5) color(cyan))


• Exploded slice is off by 5 units and color is more
distinguishable

• graph pie, over( agegrp) pie(3, explode(5) color(cyan))


pie(1, color(gold) explode(3))
• Use pie command repeatedly to explode the first slice and
enhance with golden color and slice distance is 3 units
Color and explode cont.
• graph pie, over( agegrp) intensity(*1.5)
• Change color intensity of slices by increasing or decreasing
the colors from their default colorations

• graph pie, over( agegrp) line(lcolor(sienna) lwidth(thick))


• Draws lines between the slices and adds color to the line
separators
Pie slice labels
• graph pie, over( agegrp) plabel(_all sum)
• Labels the frequency of age groups in the slices

• graph pie, over( agegrp) plabel(_all percent)


• Labels the slices with exact percentage of respective age
groups

• graph pie, over( agegrp) plabel(_all name)


• Gives actual value labels or names to the slices
Pie slice labels cont.
• graph pie, over( agegrp) plabel(_all name) legend(off)
• When the name option is used, the legend is not as
necessary and can be suppressed using the legend(off)
option

• graph pie, over( agegrp) plabel(1 “youth=33.33%”) plabel(2


“adult=33.33%”) plabel(3 “elderly=33.33%”)
• You can add labels inside the slices the way you want
Slice label options cont.
• graph pie, over( agegrp) plabel(_all name, gap(-3))
plabel(_all percent, gap(3) format(“%2.0f” )) legend(off)
• Adds slice names and percentages, positions the names 3
units towards the center, positions the percentages 3 units
far from the center, formats the percentages and turns off
the legends
Controlling legends
• graph pie, over( agegrp) legend(label(1 "Youth") label(2
"Adult") label(3 "elderly"))
• Changes the legend labels to the required names

• graph pie, over( agegrp) legend(title (Age Category ))


• Gives a title to the legend

• graph pie, over( agegrp) legend(position(2) rows(3))


• Positions the legend and places three rows

• graph pie, over( agegrp) legend(position(9) cols(1) stack)


Graphing by groups
• graph pie, over( agegrp) by(sex)
• Graphs two pies side by side by gender type
• You can enhance the features of this chart with some of
the above options to suit your particular desires
BAR GRAPHS

• We shall draw bar graphs using the command graph bar

• Y variables bar graphs


• Here we shall draw bar graphs of continuous y variables
broken down in one or more categorical groups

• Using the blood pressure dataset, we shall apply the bar


graph command with the respective options

• However, most of the earlier options used in pie charts


would similarly apply for graphing bars
Bar graph options
• graph bar bp_before
• We have average of bp_before being graphed, but perhaps
this the most boring bar graph you would ever wish to have

• graph bar bp_before bp_after


• Here we are graphing multiple y variables plotted at one
time

• graph bar (median) bp_before bp_after


• Here we are graphing using the median than the mean
Bar graph options
• graph bar (median) bp_before (mean) bp_before
• Here you are comparing the mean and the median for the
same y variable plotted at one time

• graph bar bp_before bp_after


• Here we are simply getting the two variables plotted but
with different colors

• graph bar bp_before bp_after, ascategory


• This is for the purposes of over() options i.e. if you are
interested in grouping these variables
Bar graph options cont.
• graph bar bp_before bp_after, over(agegrp)
• Here we would like to see the mean of the two bp
measurements across age groups

• graph bar bp_before bp_after, over(agegrp) percent


• graph bar bp_before bp_after, over(agegrp) stack
• graph bar bp_before bp_after, over(agegrp) percentages
stack
• graph bar bp_before, over(agegrp) over( sex)
• graph hbar bp_before, over(agegrp) over( sex)
• graph hbar bp_before bp_after, over(agegrp) over( sex)
• graph hbar bp_before, over(agegrp) over( sex) asyvar
Bar graph options cont.
• graph hbar bp_before, over(agegrp) over( sex) asyvar
percent
• graph hbar bp_before, over(agegrp) over( sex) asyvar
percent stack
• graph hbar bp_before, over(agegrp, gap(*3)) over( sex)
• graph hbar bp_before, over(agegrp, gap(*0.3)) over( sex)
• graph hbar bp_before, over(agegrp, gap(*0.3)) over( sex,
descending)
• graph hbar bp_before, over(agegrp, gap(*0.3) sort(1))
over( sex)
• graph hbar bp_before bp_after, over(agegrp, gap(*0.3)
sort(1)) over( sex)
Bar graph options cont.
• graph hbar (sum) bp_before , over(agegrp, gap(*0.3))
over(sex) asyvar stack
Controlling the categorical axis & labels
• graph bar bp_before , over(agegrp, relabel(1 "Young" 2
"Adult" 3 "Elderly")) asyvars
• Re-labels the age categories
• graph bar bp_before , over(sex, label(nolabel))
blabel(group)
• Labels the bars at the top other than at the bottom
Title of graph & legend position
• graph bar bp_after, over(agegrp) over(sex) title("Graph of
age groups by gender")

• graph bar bp_after, over(agegrp) over(sex) b1title("Graph


of age groups by gender")

• graph bar bp_after, over(agegrp) over(sex)


legend(position(1)) asyvars

• graph bar bp_after, over(agegrp) over(sex)


legend(position(1) ring(0)) asyvars
Putting lines, bargaps & outerbars
• graph hbar bp_after, over(agegrp, label(nolabel))
blabel(name) over(sex) asyvar yline(130 200, lwidth(thick)
lcolor(red) lpattern(dash))

• graph bar bp_after, over(agegrp) over(sex) yline(130 200,


lwidth(thick) lcolor(red) lpattern(dash)) bargap(*50)

• graph bar bp_after, over(agegrp) over(sex) yline(130 200,


lwidth(thick) lcolor(red) lpattern(dash)) bargap(-50)

• graph bar bp_after, over(agegrp) over(sex) yline(130 200,


lwidth(thick) lcolor(red) lpattern(dash)) outergap(*10)
Colors
• graph bar bp_after, over(agegrp) over(sex) yline(130 200,
lwidth(thick) lcolor(red) lpattern(dash)) bargap(-50) asyvar
intensity(*2)

• graph bar bp_after, over(agegrp) over(sex) yline(130 200,


lwidth(thick) lcolor(red) lpattern(dash)) bargap(-50) asyvar
bar(1, bcolor(dkgreen))

• graph bar bp_after, over(agegrp) scheme(s2mono)


• For black and white graphs only
HISTOGRAMS
• hist bp_before

• hist bp_before, bin(20)

• hist bp_before, bin(10) color(gold)

• histogram bp_before, width(2) normal kdensity

• regress bp_before sex agegrp, robust

• ladder bp_before

• gladder bp_before
BOX PLOTS
• graph box bp_before, over(agegrp)

• graph box bp_before, over(sex)

• graph box bp_before, over(agegrp) nooutside


• For graphing box plots without outliers

• graph box bp_before, over(agegrp) over(sex) nooutside

• graph box bp_before, over(agegrp) over(sex) nooutside


asyvars
TWO WAY GRAPHS
• SCATTER PLOTS

• graph twoway scatter bp_before bp_after

• twoway scatter bp_after bp_before, msymbol(Sh)

• twoway scatter bp_after bp_before, mcolor(maroon) msymbol(Sh)

• twoway scatter bp_after bp_before, mcolor(maroon)

• twoway scatter bp_after bp_before, mcolor(maroon) msize(vlarge)

• twoway scatter bp_after bp_before, mlabel(sex)


Regression fits and splines
• twoway(scatter bp_before bp_after) (lfit bp_before
bp_after)

• twoway(scatter bp_before bp_after) (lfit bp_before


bp_after) (fpfit bp_before bp_after) (lowess bp_before
bp_after) (mspline bp_before bp_after) (mband bp_before
bp_after)
• twoway(scatter bp_before bp_after) (lfit bp_before
bp_after)
Scatter plots for regression fitted values
• regress bp_after bp_before

• predict yhat

• twoway scatter yhat bp_before

• twoway scatter yhat bp_before, connect(1) sort


msymbol(Sh)
Regression fit CI
• twoway(lfitci bp_after bp_before) (scatter bp_after
bp_before)

• twoway(lfitci bp_before bp_after, stdf) (scatter bp_before


bp_after)
Line graphs
• twoway line bp_after bp_before, sort

• twoway line bp_after bp_before, sort clcolor(red)

• Area plots
• twoway area bp_after bp_before, sort

• Matrix plots
• graph matrix bp_after bp_before , sort
COMBINING GRAPHS
• twoway line bp_after bp_before, sort

• save “D:/IRES/line graph.gph”

• twoway area bp_after bp_before, sort

• save “D:/IRES/area graph.gph”

• graph combine “D:/IRES/line graph.gph” “D:/IRES/area


graph.gph”
THE END
THANKS FOR LISTENING

IRES - Nairobi
jminyoso@gmail.com
+254 -713 -239 670

You might also like