Graphs

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

binscatter

Binned scatter plot

Stata

binscatter y x

R Package:binsreg

binsreg::binsreg(y, x, data = data)

Differs from Stata statar::stat_binmean() adds binned means to a ggplot: ggplot(data, aes(x, y)) + statar::stat_binmean(n = 20). binsreg() defaults to data-driven bin selection and inference; binscatter defaults to fixed quantile bins and an OLS fit. Match bins and fit settings explicitly.

coefplot

Coefficient plot

Stata

coefplot

R Package:modelsummary

modelsummary::modelplot(fit)

dotplot

Show individual observations by category

Stata

dotplot y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::geom_point(position = ggplot2::position_jitter(width = 0.15, height = 0))

Differs from Stata This uses horizontally jittered observations rather than Stata dot-stacking rules.

graph

Bar chart of counts

Stata

graph bar (count), over(x)

R Package:ggplot2

ggplot(data, aes(x)) + geom_bar()

Bar chart of group means

Stata

graph bar (mean) y, over(group)

R Package:ggplot2

ggplot(data, aes(group, y)) + stat_summary(fun = mean, geom = "bar")

Box plot

Stata

graph box y, over(group)

R Package:ggplot2

ggplot(data, aes(group, y)) + geom_boxplot()

Differs from Stata ggplot2 boxplot hinges use R quantile rules; Stata uses Stata percentiles, which may differ in small samples and with ties.

Save the last graph

Stata

graph export figure.png

R Package:ggplot2

ggsave("figure.png")

Pie chart

Stata

graph pie, over(group)

R Package:ggplot2

ggplot(data, aes(x = "", fill = group)) + geom_bar() + coord_polar(theta = "y")
16 more examples and alternatives

Combine plots

Stata

graph combine g1.gph g2.gph

R Package:patchwork

patchwork::wrap_plots(p1, p2)

With patchwork attached, p1 + p2 does the same.

Dot chart

Stata

graph dot (mean) y, over(group)

R Package:ggplot2

ggplot(data, aes(y, group)) + stat_summary(fun = mean, geom = "point")

Horizontal bar chart

Stata

graph hbar (mean) y, over(group)

R Package:ggplot2

ggplot(data, aes(group, y)) + stat_summary(fun = mean, geom = "bar") + coord_flip()

Horizontal box plot

Stata

graph hbox y, over(group)

R Package:ggplot2

ggplot(data, aes(y, group)) + geom_boxplot()

Differs from Stata ggplot2 boxplot hinges use R quantile rules; Stata uses Stata percentiles, which may differ in small samples and with ties.

Scatter-plot matrix

Stata

graph matrix x1 x2 x3

R Package:graphics

pairs(select(data, x1, x2, x3))

GGally::ggpairs() draws a ggplot2 version.

Save a plot to edit later

Stata

graph save fig.gph

R Package:base

saveRDS(p, "fig.rds")

Plot group frequencies

Stata

graph bar (count), over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group)) + ggplot2::geom_bar()

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Plot group means as bars

Stata

graph bar (mean) y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::stat_summary(fun = mean, geom = "col")

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Label summary bars with their values

Stata

graph bar (mean) y, over(group) blabel(bar)

R Package:ggplot2

{ plot_data <- aggregate(y ~ group, data, mean); ggplot2::ggplot(plot_data, ggplot2::aes(group, y)) + ggplot2::geom_col() + ggplot2::geom_text(ggplot2::aes(label = round(y, 1)), vjust = -0.3) }

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Plot group medians as bars

Stata

graph bar (median) y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::stat_summary(fun = median, geom = "col")

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Plot group percentages

Stata

graph bar (percent), over(group)

R Package:ggplot2

{ counts <- as.data.frame(table(data$group)); ggplot2::ggplot(counts, ggplot2::aes(Var1, Freq / sum(Freq) * 100)) + ggplot2::geom_col() }

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Plot group totals as bars

Stata

graph bar (sum) y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::stat_summary(fun = sum, geom = "col")

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Compare distributions with box plots

Stata

graph box y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::geom_boxplot()

Differs from Stata Box-plot hinges and whisker defaults should be checked when numerical parity is required.

Hide box-plot outlier markers

Stata

graph box y, over(group) nooutsides

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(group, y)) + ggplot2::geom_boxplot(outlier.shape = NA)

Differs from Stata Box-plot hinges and whisker defaults should be checked when numerical parity is required.

Plot group means as dots

Stata

graph dot (mean) y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y, group)) + ggplot2::stat_summary(fun = mean, geom = "point")

Differs from Stata Reproduces the displayed count, percent or summary layer, rather than Stata graph display defaults. Match category order and missingness; ggplot themes, axis scales and bar annotations follow R conventions.

Draw horizontal box plots

Stata

graph hbox y, over(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y, group)) + ggplot2::geom_boxplot()

Differs from Stata Box-plot hinges and whisker defaults should be checked when numerical parity is required.

heatplot

Heat map of the mean of z over binned x and y

Stata

heatplot z y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y, z = z)) + ggplot2::stat_summary_2d(fun = mean)

Differs from Stata Community command. Both average z within cells, but bin counts and boundaries differ; set bins explicitly (bins() in Stata, bins = in R) when comparing. Colour ramps follow each program’s defaults.

1 more examples and alternatives

Heat map of the mean of z over categories of x and y

Stata

heatplot z i.y i.x

R Package:ggplot2

ggplot2::ggplot(stats::aggregate(z ~ x + y, data = data, FUN = mean), ggplot2::aes(factor(x), factor(y), fill = z)) + ggplot2::geom_tile()

Differs from Stata Community command. One tile per observed x-y combination, filled with the mean of z; empty combinations are left blank in both.

histogram

Histogram

Stata

histogram x

R Package:ggplot2

ggplot(data, aes(x)) + geom_histogram()

Differs from Stata geom_histogram() defaults to counts and 30 bins; Stata histogram defaults to density with a different bin rule. Specify breaks/binwidth and the y statistic.

6 more examples and alternatives

Compare histograms across groups

Stata

histogram y, by(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_histogram(bins = 10) + ggplot2::facet_wrap(~group)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

Histogram on the density scale

Stata

histogram y, density

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y, ggplot2::after_stat(density))) + ggplot2::geom_histogram(bins = 10)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

Histogram on the frequency scale

Stata

histogram y, frequency

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_histogram(bins = 10)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

Histogram on the percentage scale

Stata

histogram y, percent

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y, ggplot2::after_stat(count / sum(count) * 100))) + ggplot2::geom_histogram(bins = 10)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

Choose histogram width and boundary

Stata

histogram y, width(2) start(0)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_histogram(binwidth = 2, boundary = 0)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

Histogram for integer-valued observations

Stata

histogram z, discrete

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(z)) + ggplot2::geom_histogram(binwidth = 1, boundary = 0.5)

Differs from Stata Set boundaries and interval closure explicitly when matching Stata bin counts.

kdensity

Density plot

Stata

kdensity x

R Package:ggplot2

ggplot(data, aes(x)) + geom_density()

Differs from Stata Default kernel-density bandwidth rules differ; specify kernel, bandwidth and evaluation range before comparing curves.

4 more examples and alternatives

Plot a kernel density estimate

Stata

kdensity y

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_density()

Differs from Stata R and Stata bandwidth rules and local smoothing algorithms can differ; this is a graphical alternative, not an exact numerical replication.

Estimate a density for a subgroup

Stata

kdensity y if group == "A"

R Package:ggplot2

ggplot2::ggplot(subset(data, group == "A"), ggplot2::aes(y)) + ggplot2::geom_density()

Differs from Stata R and Stata bandwidth rules and local smoothing algorithms can differ; this is a graphical alternative, not an exact numerical replication.

Specify a kernel bandwidth

Stata

kdensity y, bwidth(1)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_density(bw = 1)

Differs from Stata R and Stata bandwidth rules and local smoothing algorithms can differ; this is a graphical alternative, not an exact numerical replication.

Use a Gaussian density kernel

Stata

kdensity y, kernel(gaussian)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(y)) + ggplot2::geom_density(kernel = "gaussian")

Differs from Stata R and Stata bandwidth rules and local smoothing algorithms can differ; this is a graphical alternative, not an exact numerical replication.

line

Line plot

Stata

line y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_line()

Differs from Stata geom_line() sorts by x within groups; Stata line/connected uses observation order without the sort option.

3 more examples and alternatives

Line plot sorted by the horizontal variable

Stata

line y x, sort

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_line()

Differs from Stata Maps the stated axes and layer; line sorts by x in ggplot while Stata uses sort here. Plot themes, axis expansion, missing-point warnings and facet scales follow R defaults.

Draw line plots in separate group panels

Stata

line y x, sort by(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_line() + ggplot2::facet_wrap(~group)

Differs from Stata Maps the stated axes and layer; line sorts by x in ggplot while Stata uses sort here. Plot themes, axis expansion, missing-point warnings and facet scales follow R defaults.

Set a line colour

Stata

line y x, sort lcolor(red)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_line(colour = "red")

Differs from Stata Maps the stated axes and layer; line sorts by x in ggplot while Stata uses sort here. Plot themes, axis expansion, missing-point warnings and facet scales follow R defaults.

lowess

Smoothed scatter plot

Stata

lowess y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_point() + geom_smooth(method = "loess")

Differs from Stata LOESS local polynomials differ from Stata LOWESS smoothing, robustness iterations and bandwidth defaults; the fitted curves need not coincide.

1 more examples and alternatives

Draw a local regression smooth

Stata

lowess y x, bwidth(0.5)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_smooth(method = "loess", formula = y ~ x, span = 0.5, se = FALSE)

Differs from Stata R and Stata bandwidth rules and local smoothing algorithms can differ; this is a graphical alternative, not an exact numerical replication.

pchi

Compare empirical and chi-squared probabilities

Stata

pchi y, df(2)

R Package:graphics

graphics::plot(ppoints(length(data$y)), pchisq(sort(data$y), df = 2))

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

pnorm

Assess a standardized normal P-P relationship

Stata

pnorm y

R Package:graphics

graphics::plot(ppoints(length(data$y)), pnorm(as.numeric(scale(sort(data$y)))), xlab = "Empirical", ylab = "Normal")

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

qchi

Compare sample quantiles with chi-squared quantiles

Stata

qchi y, df(2)

R Package:graphics

graphics::plot(qchisq(ppoints(length(data$y)), df = 2), sort(data$y))

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

qnorm

Normal quantile plot

Stata

qnorm x

R Package:ggplot2

ggplot(data, aes(sample = x)) + geom_qq() + geom_qq_line()

Differs from Stata geom_qq() defaults to theoretical standard-normal quantiles; qnorm scales the normal axis by the sample mean and SD. Match axis scaling to compare points.

qqplot

Compare two empirical distributions with a Q-Q plot

Stata

qqplot y x

R Package:stats

stats::qqplot(data$x, data$y)

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

quantile

Plot ordered data against cumulative fractions

Stata

quantile y

R Package:graphics

graphics::plot(ppoints(sum(!is.na(data$y))), sort(data$y), xlab = "Fraction", ylab = "y")

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

scatter

Scatter plot

Stata

scatter y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_point()
23 more examples and alternatives

Label the points

Stata

scatter y x, mlabel(name)

R Package:ggplot2

ggplot(data, aes(x, y, label = name)) + geom_point() + geom_text()

Add a reference line

Stata

scatter y x, xline(0)

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_point() + geom_vline(xintercept = 0)

geom_hline(yintercept = 0) is yline().

Scatter plot with ggplot2

Stata

scatter y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point()

Differs from Stata Maps the stated axes and layer; line sorts by x in ggplot while Stata uses sort here. Plot themes, axis expansion, missing-point warnings and facet scales follow R defaults.

Make the plotting panel square

Stata

scatter y x, aspectratio(1)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::theme(aspect.ratio = 1)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Separate scatter plots by group

Stata

scatter y x, by(group)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::facet_wrap(~group)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Jitter overlapping observations

Stata

scatter y x, jitter(2)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_jitter(width = 0.2, height = 0.2)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Hide a graph legend

Stata

scatter y x, legend(off)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point(ggplot2::aes(colour = group)) + ggplot2::theme(legend.position = "none")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Set the marker colour

Stata

scatter y x, mcolor(red)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point(colour = "red")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Label each point with a variable

Stata

scatter y x, mlabel(label)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_text(ggplot2::aes(label = label))

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Increase marker size

Stata

scatter y x, msize(large)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point(size = 3)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Draw hollow circular markers

Stata

scatter y x, msymbol(Oh)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point(shape = 1)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Add a source note to a graph

Stata

scatter y x, note("Source: survey")

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::labs(caption = "Source: survey")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Use a monochrome-friendly theme

Stata

scatter y x, scheme(s2mono)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::theme_bw()

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Add a graph subtitle

Stata

scatter y x, subtitle("Wave 1")

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::labs(subtitle = "Wave 1")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Add a graph title

Stata

scatter y x, title("Sample")

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::labs(title = "Sample")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Choose horizontal axis breaks

Stata

scatter y x, xlabel(0(5)30)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::scale_x_continuous(breaks = seq(0, 30, 5))

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Add a vertical reference line

Stata

scatter y x, xline(10)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::geom_vline(xintercept = 10)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Use a logarithmic predictor axis

Stata

scatter y x, xscale(log)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::scale_x_log10()

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Label the horizontal axis

Stata

scatter y x, xtitle("Age")

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::labs(x = "Age")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Choose vertical axis breaks

Stata

scatter y x, ylabel(0(5)20)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::scale_y_continuous(breaks = seq(0, 20, 5))

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Add a horizontal reference line

Stata

scatter y x, yline(5)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::geom_hline(yintercept = 5)

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Use a logarithmic response axis

Stata

scatter y x, yscale(log)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::scale_y_log10()

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

Label the vertical axis

Stata

scatter y x, ytitle("Income")

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::labs(y = "Income")

Differs from Stata The R example implements the graphical intent; size, jitter and theme scales are not numerically identical to Stata.

serrbar

Plot supplied estimates and standard errors

Stata

serrbar y se x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_errorbar(ggplot2::aes(ymin = y - se, ymax = y + se)) + ggplot2::geom_point()

Differs from Stata Here se is a supplied standard-error variable; Stata serrbar defaults to one standard error. Rename the variable to suit your data.

set

Change the look of every graph

Stata

set scheme s1mono

R Package:ggplot2

theme_set(theme_bw())

spikeplot

Plot spikes for the frequencies of distinct values

Stata

spikeplot z

R Package:graphics

{ counts <- as.data.frame(table(data$z)); graphics::plot(as.numeric(as.character(counts$Var1)), counts$Freq, type = "h") }

Differs from Stata Draws one spike for each observed value with its count. Axis ticks, spike widths, handling of missing values and graphics device styling differ from Stata spikeplot.

symplot

Check symmetry around the sample median

Stata

symplot y

R Package:graphics

{ values <- sort(data$y); half <- length(values) %/% 2L; centre <- median(values); graphics::plot(centre - rev(head(values, half)), tail(values, half) - centre) }

Differs from Stata A diagnostic plotting workflow; plotting-position and standardization defaults can differ from Stata.

twoway

Scatter plot with a fitted line

Stata

twoway (scatter y x) (lfit y x)

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_point() + geom_smooth(method = "lm")

Differs from Stata geom_smooth(method = “lm”) adds a confidence band by default; lfit has no band. Use se = FALSE for the same plotted layer.

Area plot

Stata

twoway area y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_area()

Connected line plot

Stata

twoway connected y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_line() + geom_point()

Differs from Stata geom_line() sorts by x within groups; Stata line/connected uses observation order without the sort option.

Shaded range

Stata

twoway rarea hi lo x

R Package:ggplot2

ggplot(data, aes(x, ymin = lo, ymax = hi)) + geom_ribbon()

Range plot with capped spikes

Stata

twoway rcap hi lo x

R Package:ggplot2

ggplot(data, aes(x, ymin = lo, ymax = hi)) + geom_errorbar()
20 more examples and alternatives

Colour points by group

Stata

twoway (scatter y x if g == 1) (scatter y x if g == 2)

R Package:ggplot2

ggplot(data, aes(x, y, colour = factor(g))) + geom_point()

Linear fit with a confidence band

Stata

twoway lfitci y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_smooth(method = "lm")

Quadratic fit

Stata

twoway qfit y x

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE)

One panel per group

Stata

twoway scatter y x, by(group)

R Package:ggplot2

ggplot(data, aes(x, y)) + geom_point() + facet_wrap(~group)

Overlay a linear fitted relationship

Stata

twoway (scatter y x) (lfit y x)

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point() + ggplot2::geom_smooth(method = "lm", formula = y ~ x, se = FALSE)

Differs from Stata Graphical workflow; plotting defaults can differ.

Fill the area beneath a line

Stata

twoway area y x, sort

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_area()

Differs from Stata Graphical workflow; plotting defaults can differ.

Connect points in horizontal order

Stata

twoway connected y x, sort

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_line() + ggplot2::geom_point()

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw contour lines on a regular grid

Stata

twoway contour y x z

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_contour(ggplot2::aes(z = z))

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw points with drop lines

Stata

twoway dropline y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_segment(ggplot2::aes(xend = x, yend = 0)) + ggplot2::geom_point()

Differs from Stata Graphical workflow; plotting defaults can differ.

Plot a linear fit with confidence limits

Stata

twoway lfitci y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_smooth(method = "lm", formula = y ~ x, level = 0.95)

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw arrows between paired coordinates

Stata

twoway pcarrow y x hi z

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_segment(ggplot2::aes(xend = z, yend = hi), arrow = grid::arrow())

Differs from Stata Graphical workflow; plotting defaults can differ.

Connect paired coordinate endpoints

Stata

twoway pcspike y x hi z

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_segment(ggplot2::aes(xend = z, yend = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Plot a quadratic fit with confidence limits

Stata

twoway qfitci y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_smooth(method = "lm", formula = y ~ poly(x, 2), level = 0.95)

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw a shaded uncertainty band

Stata

twoway rarea lo hi x, sort

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_ribbon(ggplot2::aes(ymin = lo, ymax = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw bars spanning lower and upper values

Stata

twoway rbar lo hi x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_rect(ggplot2::aes(xmin = x - 0.4, xmax = x + 0.4, ymin = lo, ymax = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw capped uncertainty intervals

Stata

twoway rcap lo hi x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_errorbar(ggplot2::aes(ymin = lo, ymax = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Plot both endpoints of a range

Stata

twoway rscatter lo hi x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point(ggplot2::aes(y = lo)) + ggplot2::geom_point(ggplot2::aes(y = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Show ranges with vertical lines

Stata

twoway rspike lo hi x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_linerange(ggplot2::aes(ymin = lo, ymax = hi))

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw a two-variable scatter plot

Stata

twoway scatter y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_point()

Differs from Stata Graphical workflow; plotting defaults can differ.

Draw vertical spikes from zero

Stata

twoway spike y x

R Package:ggplot2

ggplot2::ggplot(data, ggplot2::aes(x, y)) + ggplot2::geom_segment(ggplot2::aes(xend = x, yend = 0))

Differs from Stata Graphical workflow; plotting defaults can differ.

vioplot

Violin plot

Stata

vioplot y, over(group)

R Package:ggplot2

ggplot(data, aes(group, y)) + geom_violin()