{"id":889,"date":"2016-07-22T16:00:23","date_gmt":"2016-07-22T16:00:23","guid":{"rendered":"http:\/\/www.hargaden.com\/enda\/?p=872"},"modified":"2021-04-19T01:42:35","modified_gmt":"2021-04-19T01:42:35","slug":"using-rs-ggplot2-with-stata","status":"publish","type":"post","link":"https:\/\/hargaden.com\/enda\/using-rs-ggplot2-with-stata\/","title":{"rendered":"Using R&#8217;s ggplot2 with Stata"},"content":{"rendered":"\n<p>Stata is great, but it&#8217;s true that R makes prettier graphs, especially when you make use of the outstandingly excellent <a href=\"http:\/\/matthewlincoln.net\/2014\/12\/20\/adjacency-matrix-plots-with-r-and-ggplot2.html\"><tt>ggplot2<\/tt><\/a> Thanks to <a href=\"http:\/\/www.imperial.ac.uk\/nhli\/r.newson\/stata.htm\">Roger Newson<\/a> we can have both. This post walks you through exploiting <tt>ggplot2<\/tt> directly from Stata. I&#8217;ve tested this with both Stata 13 and Stata 14 on Windows 7 on two different computers.<\/p>\n<p><img decoding=\"async\" style=\"border: 1px solid black;\" src=\"https:\/\/hargaden.com\/enda\/wp-content\/ggplot_stata.png\" alt=\"A simple ggplot produced directly from Stata\" width=\"350px\"><\/p>\n<p>Here are the steps:<\/p>\n<ul>\n<li>First, you need the foreign and ggplot2 packages installed in R. Install them using the install.packages() command, e.g. <tt><b>install.packages(\"ggplot2\")<\/b><\/tt><\/li>\n<li>Second, you need the rsource package installed in Stata. You can do this with the <tt><b>ssc inst rsource<\/b><\/tt> command.<\/li>\n<li>Third, you need to find the R terminal program, named Rterm.exe. For me, this is located in C:\/Program Files\/R\/R-3.3.0\/bin\/x64\/Rterm.exe. You then need to change the line in the code that begins with <b><tt>global Rterm_path<\/tt><\/b> to wherever Rterm.exe is on your computer.<\/li>\n<\/ul>\n<p>Then you can use this code (<a href=\"..\/wp-content\/stata_ggplot.do\">click here for download<\/a>). It&#8217;s well commented below so you should be able to understand what it&#8217;s doing. It produces the graph you can see above.<\/p>\n<p><tt style=\"font-size: 9pt;\">** Open up R interactively through Stata<br>** Enda Patrick Hargaden<br>** Boyd Center \/ Economics<br>** University of Tennessee, July 2016<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">** For sample purposes, let's use the auto dataset. Obviously you change this to your data.<br>clear<br>sysuse auto<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">***********************************************<br>** You need to adjust this. Find the location of Rterm.exe on your machine<br>***********************************************<br>global Rterm_path `\"C:\/Program Files\/R\/R-3.3.0\/bin\/x64\/Rterm.exe\"'<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">** This records Stata's present working directory in R-compatible format<br>local r_pwd = subinstr(\"`c(pwd)'\",\"\",\"\/\",.)<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">** Temporarily make a copy of the dataset in a format R will probably understand<br>saveold holderfile.dta, version(12) replace<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">** Start R via rsource, pass your present working directory to it<br>rsource, terminator(\"end_r_stata\") roptions(`\" --vanilla --args \"`r_pwd'\" \"')<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## We're now in R, so switching the comment designation from star to hash<br>## Stata may mention an error anytime you include an R comment. Don't worry about it.<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## Use the argument (i.e. pwd) passed via Stata and move to it<br>stata_pwd = commandArgs(trailingOnly=TRUE);<br>setwd(stata_pwd[1]);<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## Load the packages.<br>library(\"foreign\");<br>library(\"ggplot2\");<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## Read and then delete the data<br>df = read.dta(\"holderfile.dta\", convert.f=TRUE)<br>file.remove(\"holderfile.dta\")<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## Draw the graph<br>ggplot(df, aes(x=mpg, y=price, color=foreign)) +<br>geom_point() +<br>ggtitle(\"Ahh, lovely R graphs through Stata\")<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## Save both a PDF and PNG version<br>ggsave(\"ggplot_stata.pdf\")<br>ggsave(\"ggplot_stata.png\")<\/tt><\/p>\n<p><tt style=\"font-size: 9pt;\">## And now you stop using R<br>end_r_stata<\/tt><\/p>\n<p>And there we have it. Transferring the data over to R and generating nice ggplots without ever leaving Stata. Of course you don&#8217;t need to restrict yourself to ggplots. With this basic idea you can use any of R&#8217;s capabilities directly from Stata.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stata is great, but it&#8217;s true that R makes prettier graphs, especially when you make use of the outstandingly excellent ggplot2 Thanks to Roger Newson we can have both. This post walks you through exploiting&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-889","post","type-post","status-publish","format-standard","hentry","category-stata"],"_links":{"self":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/comments?post=889"}],"version-history":[{"count":4,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/889\/revisions"}],"predecessor-version":[{"id":1219,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/889\/revisions\/1219"}],"wp:attachment":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/media?parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/categories?post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/tags?post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}