Stata features a wealthy set of instruments for creating publication-quality graphics. Between the big variety of graphs you can also make and the sheer variety of particulars you may management in a graph, Stata graphics generally is a daunting topic. Nevertheless, you may in all probability solely have to make a number of totally different sorts of graphs, and typically Stata’s default settings will likely be high-quality. What’s extra, Stata’s Graphical Consumer Interface (GUI) organizes the varied graphing choices in an intuitive approach so you will discover them once you want them with out memorizing the syntax for every one. That does not imply you should not put your graph instructions in do information as soon as you’ve got created them, however it does imply that for advanced graphs you should utilize the GUI to create the instructions you may retailer.

Stata 10 additionally added a graph editor which lets you modify a graph after you’ve got created it. This offers you much more management over your graph, however not like the GUI for making a graph the editor doesn’t provide you with a command you may rerun later. If you happen to later make any modifications to your knowledge, you may have to start out your graph over from scratch. Thus it is higher to create the graph you need within the first place the place attainable, however the editor remains to be a really helpful addition.

This text will focus on creating graphs utilizing the Stata GUI, the construction of the graph command syntax (with out dwelling an excessive amount of on the small print), some widespread varieties of graphs, and the graph editor. The easiest way to make use of this text is to learn it on the laptop and truly perform the steps described. The examples on this article will use the car dataset that comes with Stata, so start by typing:

sysuse auto

to load it. Be at liberty to experiment as you go, particularly with the settings we do not focus on (often as a result of they’re both pretty apparent or not often used). Making a graph won’t ever change your knowledge, so the worst that may occur is that your graph seems to be ineffective or simply plain ugly.

Creating Graphs Utilizing the Graphical Consumer Interface

We’ll begin with a easy scatter plot with weight because the X variable and mpg because the Y variable. Stata refers to any graph which has a Y variable and an X variable as a twoway graph, so click on Graphics, Twoway graph. The following step is to outline a plot. In Stata phrases, a plot is a few particular knowledge visualized in a particular approach, for instance “a scatter plot of mpg on weight.” A graph is a whole picture, together with axes, titles, legends, and many others. Additionally, a single graph might include a number of plots. The fundamental twoway dialog field will mean you can outline as many plots as you want, plus management the opposite traits of the graph.

Twoway main dialog box

To outline a plot, click on Create. You may then get a dialog field the place you may describe the plot you need. Depart the class set to Fundamental plots and the sort set to Scatter. For the Y variable choose or kind mpg, and for the X variable choose or kind weight. (Observe: Linux Stata doesn’t mean you can choose variables so you may have to kind their names. In Home windows Stata you are able to do both.)

Defining a scatter plot

If you happen to click on Submit, the graph will likely be created with out closing the dialog field. This lets you look over the outcomes after which simply make changes and check out once more. Click on Submit now and you need to get a easy however skilled wanting scatter plot.

Basic Scatter Plot

Including Extra Variables

If you wish to add a second Y variable to the scatterplot, the best approach is to kind its identify into the Y variable field after the one you’ve got already chosen. Stata doesn’t mean you can choose a number of variables from the record with the mouse, however it has no bother understanding a number of variables within the Y variables field so long as you set them there your self.

To make a smart graph with two Y variables (with out having a number of scales) we’d like variables with related values. The trunk variable qualifies, so kind it within the Y variable field after mpg and click on Submit once more.

Defining a scatterplot with two Y variables

The end result will likely be a scatter plot with each variables. Observe how Stata mechanically places the 2 variables in several colours and provides a legend explaining which is which. The legend textual content is drawn from the variable labels, however you could possibly override all these default behaviors should you so desired.

Scatter Plot with Two Variables

Plotting a Subset of the Knowledge

If you happen to solely need to embody sure observations, you may specify which of them by clicking on the if/in tab. You may then kind a situation within the If: field, and solely observations that meet the situation will likely be included within the plot. Don’t kind the phrase if, as that’s assumed.

To solely plot international vehicles, kind international within the If: field.

Adding an if condition

Recall that in Stata one is true and nil is fake, and international is coded accordingly. Thus if international is equal to if international==1. Click on Submit and you will get a a lot sparser graph.

Scatter Plot with just foreign cars

You can too use extra difficult expressions. For instance, kind international & value<10000 and you will get simply the vehicles that are each international and value lower than $10,000 (in 1978 {dollars}).

Plotting a subset of the data

Scatter plot with a more complicated if

Controlling the Markers

By default Stata marks every level with a small dot, however you may change this. First click on on the Plot tab once more, and for finest outcomes set the Y variable again to only mpg. Then click on Marker Properties. Set Image to Hole circle. Subsequent examine Add labels to markers and set the Variable to make, then click on Submit.

Marker Properties

Scatter plot with labels

As you see, every level is now a hole circle with the identify of the automobile printed subsequent to it, however lots of the names overlap. You may enhance the scenario considerably by altering Label angle to 45 levels, however basically you may solely put helpful labels on a scatter plot if it has a small variety of observations and so they’re unfold out.

Scatter plot with angled labels

Overlaying Plots

Subsequent we’ll mix a number of plots. Uncheck Add labels to markers. Click on Settle for to settle for these settings for Marker properties and return to the Plot 1 window, then click on Settle for once more to simply accept the plot as it’s and return to the principle twoway window. Click on Create so as to add one other plot to the graph. This time we’ll make a line plot. Set the plot kind to Line, and once more select mpg and weight because the Y and X variables. Click on Undergo see the end result.

Adding a line graph

Scatter plot and line plot, but with a problem

It is in all probability not what you expected–in reality it seems like a scribble. That is as a result of by default Stata attracts the road from commentary one to commentary two to commentary three and so forth. What you need is a line from the commentary with the bottom weight to the one with the following lowest weight, and many others. That is why Stata included the checkbox Kind on x variable. This doesn’t change the precise order of the observations in your knowledge set, simply the order by which they’re linked in your line plot. Verify it and click on Submit once more. This time you need to get the graph you anticipated.

Setting the line graph to be sorted

Scatter and line, now sorted

Observe that whereas the road connects all of the factors within the scatter plot, it goes to a number of factors that the scatter plot doesn’t embody. That is since you did not set an if situation for the road plot, so it is plotting all of the observations within the knowledge. The power to set totally different if circumstances for various plots may be very helpful, although you may word that the twoway window has its personal if/in tab which applies to all of the plots.

Line properties

You may management what the road seems like by clicking Line Properties. Essentially the most generally used possibility right here is Sample. By default Stata distinguishes totally different line plots by coloration, however that does not assist if the graph must be printed in black and white. So as a substitute you may set a sample for every line. (Alternatively you may select a scheme designed for printing). To see it in motion set Sample to Sprint. Additionally strive setting Connecting methodology to Stairstep.

Line properties

Dashed line and stairstep

Plotting Subsamples

Let’s return to only plotting mpg vs. weight, however this time let’s make the international vehicles one coloration and the home vehicles one other. You are able to do this by creating two plots, one for the international vehicles and one for the home vehicles, every having an if situation that limits it to the correct subpopulation. Then Stata will make them totally different colours mechanically.

Start by resetting all the pieces. Click on Settle for twice to get again to the principle twoway window, then click on the R button (Reset button) within the decrease left to reset the plots. Subsequent click on Create, depart the sort as Scatter, set the Y variable to mpg and set the X variable to weight. Then click on if/in and within the If: field kind !international. The exclamation mark is Stata for “not” so this implies “not foreign” and is equal to international==0. Thus this plot will solely embody the home vehicles.

Click on Settle for to get again to the principle twoway window, then click on Create once more and repeat the whole course of with one important distinction: this time within the If: field simply kind international. This plot will embody solely the international vehicles. Click on Undergo see the end result.

See also  How to set Gif Wallpaper in Windows 10? – Animated Desktop Background

Plotting two subpopulations in separate plots

The ensuing graph very properly makes the home vehicles blue and the international vehicles crimson. Nevertheless, the legend offers you no indication which is which. To try this you may have to take management of the legend your self.

Controlling the Look of a Graph

You have not seen any choices for controlling the legend, as a result of to date we have been targeted on the properties of particular person plots. The legend will not be related with a specific plot as a result of it doubtlessly accommodates info from all of the plots. Thus to get to it you’ll want to click on Settle for and get again to the twoway window. That is the place you management facets of the graph as an entire, together with the legend.


Click on on the Legend tab. The Legend habits simply controls whether or not the legend is proven or not. Since Stata thinks our graph ought to have one and we agree, we will depart it set to Default. Then again, the default keys do not present any helpful info for this graph, so we have to override them. Verify Override default keys. Then within the field beneath you’ll want to kind the variety of every plot adopted by the way you need it to be labeled (in citation marks). For this graph kind:

1 “Domestic Cars” 2 “Foreign Cars”

Controlling the legend

Now click on Submit and you will get a graph with a helpful legend. Observe that the order by which you record the plots is the order by which they will seem within the legend: should you typed 2 “Foreign Cars” 1 “Domestic Cars” the legend would put the international vehicles first.

Plot with a useful legend

The Group / Look button opens up a dialog field with all types of choices referring to how the legend seems. Click on the Group tab and set Rows/Columns to Columns (leaving it set to only one column).


Organizing the Legend

The Labels and Area tabs mean you can management the looks of the legend textual content and the whole legend field respectively. The varied choices like sizes and colours are self-explanatory, however these identical choices seem in many various contexts so it is value taking a second to experiment and see how they work. Select some totally different colours and such simply to see how they work. Then click on Settle for to get again to the principle twoway window.

Choosing sizes and colors

The Placement button permits you to management the place the legend is positioned within the graph. To know how places are specified, think about placing the face of an analog clock over your graph: 12 o’clock is the highest, 6 o’clock the underside, 3 o’clock the precise facet, and many others. You can too specify that the legend goes contained in the plot area moderately than within the border space round it–just be certain it would not cowl up something essential.

Attempt setting the Place to 12 o’clock and examine Place legend inside plot area, then click on Undergo see the outcomes.

Legend positions

Plot after changing the legend's appearance


You may add a title to the legend utilizing the Title tab of the within the Legend group and look properties window, however you are more likely to need to add a title to the graph itself. To take action click on Settle for to get again to the principle twoway window once more, after which click on on its Titles tab. This offers you 4 packing containers the place you may put textual content: the Title, Subtitle, Caption and Observe. The simplest method to see what every one means is to strive them out, so kind Title, Subtitle, Caption and Observe of their respective packing containers, and click on Undergo see the end result.

Adding titles

Plot with titles

As you see, Title and Subtitle go on the high, whereas Caption and Observe go within the backside left. Every additionally has a specific font dimension related to it. Nevertheless, these are merely defaults. Click on on the Properties button related to every one and you’ll change its place and format. (Legend titles work in the identical approach, however they seem contained in the legend field.)

In order for you the textual content to be break up throughout a number of strains, put every line in quotes. To see this in motion, exchange Title with “First Line” “Second Line” and click on Submit.

Title with multiple lines

Plot with a multi-line title


Subsequent we’ll think about the axes of the graph. Click on on the Y axis tab (the X axis tab is equivalent) and the very first thing you may see is the Title field. If a plot has only one Y variable, the default title for the Y axis is that variable’s label if it has one, or else merely its identify. If all of the plots have a number of Y variables, the default is not any title in any respect. Nevertheless, you may override the default by typing one thing right here, and management its format by clicking Properties. Kind in Miles Per Gallon and click on Undergo see the impact.

Setting an axis title

Plot with a custom Y axis title

Main ticks are the marks alongside the axis with numbers by them that we have been seeing all alongside, however by clicking Main tick/label properties you may management what number of there are and the place they’re positioned. That is performed by selecting one in all a number of guidelines. Use the default rule permits Stata to decide on, which it often does fairly effectively. Recommend # of ticks leaves a lot of the management to Stata however permits you to set roughly what number of ticks there will likely be. Min Max places ticks at simply the underside and high of the axis. Vary/Delta offers you full management so long as you need the ticks to observe a daily rule. For instance, choose Vary/Delta, set the Minimal worth to 0, the Most worth to 40, and the Delta to five, then click on Undergo see the outcomes.

Controlling ticks  with Range/Delta

Plot with more ticks on the Y axis

The result’s a Y axis that begins at zero and has ticks each 5 items, however word that the graph continues as much as 41 as a result of one automobile bought 41 miles per gallon. You may lengthen a graph by extending an axis (bear in mind the default model of this graph began at ten) however you can’t truncate it. Stata will at all times make the graph large enough to plot all of the observations in your knowledge. If you happen to actually wished the graph to cease at 40 you’d have so as to add an if situation like mpg<=40.

Customized guidelines mean you can place ticks anyplace you want and have them say no matter you want. The syntax is solely a listing of numbers and related labels. For instance, choose Customized and within the Customized rule field kind 10 “Terrible” 20 “Mediocre” 30 “Very Good”.

Custom tick rule

Plot with labeled ticks

The result’s a much less informative however extra eloquent graph. You could possibly accomplish the identical factor by placing worth labels on the variable. If you have already got worth labels assigned simply click on on the Labels tab and examine Use worth labels.

Each tick doesn’t want a textual content label. For instance, you could possibly set the customized rule to 10 “Terrible” 15 20 “Mediocre” 25 30 “Very Good” 35 40 and get ticks each 5 items with labels at simply 10, 20 and 30. Nevertheless, this can trigger the labels to overlap, or a minimum of crowd one another. To repair that, click on on the Labels tab and set Angle to Horizontal. Since this can power Stata to increase the margin to the left of the plot with the intention to make room for the labels, set the Measurement to v. Small.

Formatting labels

Plot with some ticks labeled and others not

Nevertheless, the textual content labels do not embody the numbers 10, 20 or 30, which seems odd for the reason that different tick marks do have numbers. To make these numbers seem, add them to the top of their respective textual content labels: 10 “Terrible 10” 15 20 “Mediocre 20” 25 30 “Very Good 30” 35 40. That approach they will line up with the opposite numbers.

Plot with text labels that include numbers

Minor tick/label properties controls smaller ticks that go in between main ticks. They observe the identical guidelines as main ticks, however the guidelines apply to the areas between the most important ticks moderately than the entire axis. Clearly the default dimension is smaller as effectively, however they’re in any other case related.

Axis line properties controls the looks of the axis itself, whereas Axis scale properties controls its scale. This contains the flexibility to vary it to a logarithmic scale, to reverse the size, and to increase the size (however to not truncate it–see the dialogue above).

Utilizing By with Graphs

Utilizing by in a graph doesn’t provide you with a totally separate graph for every subpopulation, however it does provide you with a separate plots inside a single graph.

The graph we now have been working with has turn out to be unwieldy, so begin by clicking the reset button (Reset Button). Then arrange a scatter plot of mpg on weight identical to we now have made earlier than. Click on Settle for to return to the twoway window, then click on on the By tab. Verify the field for Draw subgraphs for distinctive values of variables and within the Variables field kind or choose international.

Setting up By

Click on Submit and you may see two scatter plots side-by-side, one for home vehicles and one for international vehicles.

Subpopulations plotted side-by-side using By

Now exchange international with rep78. This variable takes on 5 values, plus it has some lacking values. Verify the packing containers for Add a graph with totals and Add graphs for lacking values and click on Submit.

Setting up a more complex By

Plot using By with more values

The ensuing graph has seven plots: one for every of the 5 commonplace values taken by rep78, one for observations that are lacking rep78, and one for all observations. Nevertheless, the default association of those plots will not be notably enticing, so click on on Subgraph group.

See also  Our Favorite Software for Remote Access / RMM as of 2019

One possibility could be to place all of the plots on one row for simple comparability: set Model to Compact, Rows/Columns to Rows after which the variety of rows to 1. Click on Undergo see the result–obviously should you wished to make use of this you’d need to do one thing concerning the X axis labels, however you understand how to try this.

Reorganizing the plots

Many By plots in a row

One other risk could be to return to the unique format, however transfer the Complete graph to the middle of the underside row. First set the Model again to Default and Rows/Columns again to Default, and click on Undergo take one other look. This association has room for 9 plots in three rows with three plots every, and Stata numbers them left to proper after which high to backside. The Complete graph is at present within the seventh place. If we inform Stata to go away the seventh place clean, it can mechanically transfer to Complete graph to the eighth place, which is within the heart of the underside row. To take action, examine Drive holes within the placement of graphs and within the Holes field kind 7. Observe that you could possibly put a number of numbers on this field should you wished to go away a number of holes. Click on Undergo see the end result.

Leaving holes in by plots

Many By plots with the last one centered

Now click on Settle for to get again to the twoway window. The Subgraph titles and Subgraph areas are all about controlling look and are just like what you’ve got seen earlier than. Subgraph axes are somewhat extra attention-grabbing. One downside with our final graph is that Stata tried to cut back muddle by solely placing one set of axes per row and column, however given the structure it seems random. You may repair that by clicking Subgraph axes after which below Present Axes set each Y axes and X axes to Sure. When you’re right here, additionally set Rescale axes to Sure for for each axes. This tells Stata to decide on the size for every subgraph primarily based on the factors it accommodates moderately than selecting one scale for all of them. This makes every subgraph look nicer, however makes it unattainable to match them (which will be the total level of the graph).

Controlling the axes on each By plot

Many By plots, all with different scales

General Choices

Lastly click on on the General tab to see some general-purpose choices for the entire graph.

Schemes are saved units of defaults. For instance, the Economist scheme implements the graphics tips of the journal The Economist. The 2 monochrome schemes (s1 monochrome and s2 monochrome) are notably helpful if you’ll want to print your graph in black and white. Be at liberty to experiment. It is also attainable to create your individual schemes. Attempt setting Scheme to s1 monochrome after which click on Submit.

Choosing a Scheme

Monochrome plot

Graph dimension could be complicated as a result of once you’re working with the graph in Stata, Stata will match the graph in no matter dimension window you select. Nevertheless, on the subject of printing or exporting the graph Stata will make it match the dimensions you set right here.

Area properties refers back to the Plot area (all the pieces inside a plot) and the Graph area (all the pieces outdoors a plot). That is the place you can also make world modifications to the looks of your graph like altering background colours.

Saving a Graph

As soon as you’ve got created the graph you need, it can save you it by clicking File, Save As within the graph’s window. Nevertheless, you need to select what kind of file to reserve it as rigorously. The default, Stata Graph format (.gph), has the benefit of permitting you to make additional modifications to the graph with the graph editor. Nevertheless, .gph information can’t be positioned in different information like Phrase paperwork or net pages. If you wish to put your graph in a Phrase doc use Enhanced Metafile format (.emf) or for net pages we recommend Moveable Community Graphics (.png).

Saving a graph and choosing a file format

For rather more info see Utilizing Stata Graphs in Paperwork.

Creating Graphs Utilizing Instructions

As you labored via the earlier sections you’ve got in all probability observed that Stata pasted a command into the Outcomes window each time you created a graph. Stata used all the pieces you clicked on to formulate the command, however it’s the command that really created the graph. As you may see, the instructions to create graphs are sometimes very difficult. If you happen to write the instructions your self you could possibly make them considerably easier, however not a lot.

So why discover ways to create graphs utilizing instructions in any respect? Initially, for easy graphs it may be a lot sooner. For instance, you may create your fundamental scatter plot of mpg on weight just by typing

scatter mpg weight

Second, even should you’ve created a graph utilizing the graphical person interface you could need to modify it by recalling the command and making modifications. Third, there are a only a few issues that can not be performed utilizing the graphical person interface.

Then again, there’s little level in making an attempt to be taught the correct syntax for each graph possibility. Let the graphical person interface provide help to with particulars like selecting textual content sizes and colours. What you have to know is the essential syntax construction, and that is what we’ll focus on.

Regardless of the way you created your graph, you need to retailer the command in a do file. This lets you recreate the graph later, if wanted. It additionally serves as a document of the way you created the graph.

Graph Instructions

All graph instructions actually come down to 1 command: graph. It’s adopted by both a kind of graph (e.g. graph bar) or a household of graphs adopted by a kind of graph (e.g. graph twoway scatter). Then comes no matter info is required to create the graph itself. Nevertheless, the commonest graph sorts and households have been additionally made standalone instructions, which is why you may simply kind scatter mpg weight.

For the ever present twoway household (mainly graphs with a Y axis and an X axis) what follows is a varlist. The final variable within the record is taken to be the X variable, and all of the others are Y’s–similar to how regress handles a varlist however within the reverse approach.

Graph instructions can use if and in identical to common Stata instructions: they go on the finish of the command however earlier than any choices.

Combining Plots

If you wish to embody a number of plots in a graph, they should be separated both by placing them in parentheses or by placing two pipe characters between them (||). Thus to create a graph containing two scatter plots of mpg on weight, one for international vehicles and one for home vehicles, you may kind both:

scatter mpg weight if international || scatter mpg weight if !international


twoway (scatter mpg weight if international) (scatter mpg weight if !international)

Observe how within the second case you needed to begin with twoway. That is as a result of a Stata command cannot begin with a parenthesis, however as soon as you set it within the context of a twoway graph Stata will perceive what you imply.


Graph instructions can not use the usual by: prefix, however most permit a by possibility that does the identical factor:

scatter mpg weight, by(international)


Graph choices observe the final possibility rule of going on the finish of the command following a comma, however by that definition a single graph can include a number of “commands.” For instance, the Kind on x variable checkbox for line plots interprets into the type possibility, however it has to stick with the road plot if the graph has a couple of. Thus should you typed:

line mpg weight || scatter mpg weight, type

the type possibility could be utilized to the scatter plot, the place it could do little good. As a substitute you need to kind

line mpg weight, type || scatter mpg weight

Issues like titles are additionally choices:

scatter mpg weight, title(MPG vs Weight)

Observe that the textual content of the title doesn’t need to go in quotes–unless it has a comma in it. You should use quotes to separate strains, identical to once you typed it within the Title field.

However what actually makes life attention-grabbing is when your choices have choices. Suppose you wished the title to be merely medium giant (medlarge) moderately than the default very giant (vlarge), and likewise a darkish crimson (particularly, cranberry). You may make it so with the dimensions and coloration choices, however to make it clear what they’re controlling the dimensions and coloration of they need to go contained in the title’s parentheses. But they nonetheless observe a (single) comma. Thus the command turns into:

scatter mpg weight, title(MPG vs Weight, dimension(medlarge) coloration(cranberry))

After all it is at this level that it turns into enticing to let the graphical person interface preserve monitor of the correct placement of such issues for you, to not point out letting you already know that medlarge and cranberry are among the many accessible choices.

For the remainder of this text we’ll kind out the graph instructions moderately than utilizing the graphical person interface, partially for observe however principally as a result of we’ll be making pretty easy graphs and that is the quickest approach to take action.

Different Kinds of Graphs

Stata can create dozens of various sorts of graphs–for a a lot bigger sampling, see the Visible Overview on Stata’s web page. This part will focus on a number of extra which are pretty generally used on the SSCC.

Match Strains

Stata can plot a number of totally different sorts of match strains mechanically. The most typical are lfit (linear match), qfit (quadratic match), lfitci (linear match with confidence intervals) and qfitci (quadratic match with confidence intervals. They don’t seem to be very attention-grabbing by themselves–usually they’re overlaid on a scatter plot. The syntax is rather like every other twoway graph. Kind:

See also  No Remorse for free

scatter mpg weight || lfit mpg weight

(Or should you desire, arrange the scatter plot within the graphical person interface as traditional, then add a second plot setting the plot class and sort to Match plots and selecting Linear prediction–but typing it is going to be an entire lot sooner.)

Setting up a linear fit

Scatter plot with a fit line

The variations that present confidence intervals want somewhat care as a result of they’ll cowl issues up. Attempt:

scatter mpg weight || qfitci mpg weight

Fit line with a shaded confidence interval covering up part of a scatter plot

Any factors throughout the confidence interval are coated up by the shaded area. You may repair that by reversing the order of the plots within the command. Nevertheless, qfitci will not be one of many plot sorts that’s acknowledged by itself, so you’ll want to begin with twoway:

twoway qfitci mpg weight || scatter mpg weight

Plot with the scatter plot over the shaded confidence interval

Now all of the factors are proven. Consider a Stata graph as a portray performed with fully opaque paints. If in case you have a number of plots every plot is a layer of paint, and layers which are added later cowl up what was there earlier than. Thus plots that cowl areas ought to be listed first, and something that should go on high ought to be listed final.

Nevertheless, all these plots do is regress Y on X (or Y on X and X^2) and plot the anticipated values for Y. If you wish to run a extra difficult regression there isn’t any computerized command to plot the anticipated values. Then again, you may simply retailer the anticipated values in a brand new variable and plot it.

Suppose you wished to plot the anticipated chances you bought from a logit of international on mpg (a transparent case of reversing trigger and impact, however the methodology would not care). No graph command will do that for you, however you are able to do it your self:

logit international mpg
predict phat
line phat mpg,type

Plot of predicted probability against mpg

The end result seems cheap, however strive it as a scatter plot:

scatter phat mpg

Scatter plot of predicted probability vs. mpg

This demonstrates that the one values of mpg represented within the graph are these represented in your knowledge set (and remember many observations have the identical worth of mpg). Which will or is probably not an issue, however should you wished to plot a clean curve over the complete vary of mpg you could possibly exchange the precise values of mpg with made-up numbers which are evenly distributed from 14 to 41:

gen realMPG=mpg
exchange mpg=(41-14)*(_n-1)/(_N-1)+14
predict phat2
scatter phat2 mpg
exchange mpg=realMPG

Smoother plot of phat vs. mpg

See Making Predictions with Counter-Factual Knowledge in Stata for extra clarification of how this methodology works.

Observe that with multivariate regressions if you wish to plot the impact of 1 variable you’ll in all probability have to set the others to their means earlier than making predictions. In any other case the road will bounce throughout because of the results of the opposite variables.

Bar Plots

Stata does two very totally different sorts of bar plots. twoway bar is only a variation on what we have seen earlier than:

twoway bar mpg weight

Bar plot of mpg vs. weight

However there’s additionally graph bar which isn’t a part of the twoway household. It plots abstract statistics (the default is imply) moderately than values, and is generally used to match between variables or between teams. Variables are merely listed, and the group variable is specified utilizing the over possibility. For instance:

graph bar weight value, over(international)

Bar graph with means of weight and price over foreign

hbar is nearly identical–it simply turns all the pieces sideways.

graph hbar weight value, over(international)

Horizontal bar plot

If you wish to evaluate one thing apart from the imply, put that statistic in parentheses earlier than the record of variables:

graph hbar (median) weight value, over(international)

Bar plot comparing medians


Histograms are quite simple to create:

hist mpg

Histogram of mpg

Observe that by default Stata doesn’t put frequency on the Y axis, however likelihood density. This suits with the concept of a histogram being an approximation to the likelihood density operate, however in all probability is not the way you have been taught to make histograms again in grade college. You may have it plot frequencies utilizing the frequency possibility (% is one other that’s ceaselessly used).

If you happen to don’t love Stata’s default bins you may take management by both setting the variety of bins (bin possibility) or the width of a bin (width possibility). You may get the identical outcomes both approach. The mpg variable goes from 12 to 41, for a complete vary of 29. Stata selected eight bins, which implies every had a width of 29/8=3.625. If you happen to wished 10 bins, you could possibly kind:

hist mpg, bin(10) frequency

Histogram of mpg with more bins

However you may get the very same graph from

hist mpg, width(2.9) frequency

If in case you have discrete knowledge, the discrete possibility tells Stata to decide on bins accordingly. For instance, should you simply kind:

hist rep78

Stata will select eight bins despite the fact that rep78 solely has 5 values. The result’s a complicated histogram of pretty easy knowledge.

Histogram with too many bins

Nevertheless, should you add the discrete possibility Stata creates 5 bins for the 5 values:

hist rep78, discrete

The result’s a a lot clearer graph.

Histogram with the right number of bins

Kernel Densities

You may overlay a kernel density in your histogram simply by including the kdensity possibility (there’s additionally a standard possibility so as to add a standard density).

hist mpg, width(2.9) frequency kdensity

Histogram of mpg with an overlaid kernel density plot

You can too create an impartial kernel density plot with the kdensity command:

kdensity mpg

Kernel density plot of mpg

Essentially the most generally used possibility for kernel densities is bwidth, which units the bandwidth. Examine:

kdensity mpg, bwidth(5)

Kernel density with bandwidth=5


kdensity mpg, bwidth(1)

Kernel density with bandwidth=1

The Graph Editor

Stata features a graph editor that can be utilized to change a graph after it has been created. Its large drawback is that it would not provide you with a command it can save you in a do file and use to recreate the graph at will. Nevertheless, it does mean you can make modifications very merely and simply.

To see it in motion, make a easy scatter plot:

scatter mpg weight

Then click on File, Begin Graph Editor. You are now able to make modifications to this graph.

The Stata Graph Editor

On the left are a number of buttons that place the graph editor in a specific mode. The highest button, the arrow, places it in pointer mode, which lets you choose an merchandise on the graph to edit. For instance, should you click on on the plot a part of the graph (i.e. contained in the axes) you can edit the plot area. Colour and Margin will seem within the instrument bar on the high, however should you click on Extra you can change many extra properties.

Editing the plot region

If you happen to then click on on a specific level you may change the plot itself. Now the rapid choices are Plottype, Colour, Measurement, and Image, however you may once more click on Extra to see different issues.

Editing a plot

As you do, word the Object Browser on the precise. Stata retains monitor of the varied elements of a graph in a form of tree construction, and as you click on on issues it can spotlight the article you’ve got chosen. You can too choose an object instantly. For instance, click on title and you’ll add a title by typing it within the Textual content field. Kind in MPG vs. Weight (1978 Automobiles) and press Enter to see the outcomes.

Graph editor, setting a title

The following three buttons (the big T, the diagonal line, and the circle with a plus signal) mean you can add textual content, draw strains and place new factors anyplace you want them. For example, let’s add the Toyota Prius for comparability (46 mpg and about 3000 kilos for a 2008 four-door). The good approach could be so as to add it to the dataset earlier than making the graph, however we’ll do it utilizing the graph editor as an train.

The primary downside is that the graph ends at 42 mpg. To repair that choose yaxis1 within the Object Browser and click on Extra. Then set the Axis rule to Vary/Delta with a Minimal worth of 10, a Most worth of fifty and a Delta of 5. Click on OK and the graph will likely be rescaled–and extra ticks will likely be added so we now have a greater likelihood at marking 46 precisely.

Editing the Y axis

Plot with rescaled Y axis

Now click on the circle with a plus signal on the left after which click on as finest you may on the location mpg=46 (simply above the road) and weight=3000. This may create a hole circle at that location by default, however word that you could possibly change its look at will. Seeing the purpose on the graph makes it clearer that the actual benefit of a Prius over a 1978 VW Diesel (the 1978 automobile with 41 MPG) will not be the Prius’ considerably larger MPG, however the truth that it will get that MPG regardless of being a a lot heavier automobile.

Plot with a point added

Now click on on the T so as to add textual content after which click on to the precise of the brand new level. This will provide you with the Textbox properties window. Within the Textual content field kind 2008 Toyota Prius for comparability and set the Measurement to Small. Then click on OK.

Addind a textbox

Plot with a label for the added point

The underside button on the left is the “grid editor.” This lets you make drastic modifications to your graph, like rearranging the elements. Use with discretion, and saving your graph earlier than altering something is extremely really useful.

This can be a good time to say why you would possibly need to save graphs in Stata format: whereas it is true which you can’t put them into paperwork that approach, you can also make modifications to them within the graph editor. Thus you would possibly need to preserve two copies of essential graphs, one in Stata graph format in case you’ll want to make modifications, and one in one thing like .emf format which you can insert into your paper.

Leave a Reply

Your email address will not be published.