{"id":262,"date":"2011-03-03T16:59:13","date_gmt":"2011-03-04T00:59:13","guid":{"rendered":"http:\/\/www.rensecorten.dds.nl\/?p=262"},"modified":"2011-03-03T17:01:32","modified_gmt":"2011-03-04T01:01:32","slug":"labeling-logarithmic-axes-in-stata-graphs","status":"publish","type":"post","link":"http:\/\/www.rensecorten.org\/index.php\/2011\/03\/labeling-logarithmic-axes-in-stata-graphs\/","title":{"rendered":"Labeling logarithmic axes in Stata graphs"},"content":{"rendered":"<p style=\"text-align: justify;\">When you want to use logarithmic axes in Stata graphs, Stata doesn&#8217;t exactly make your life easy. Specifying the logarithmic scales is easy enough (via the <tt>xscale(log)<\/tt> and <tt>xscale(log)<\/tt> options, but making them look <em>nice <\/em>is a different story. For example, you&#8217;d probably want the labeling of the axes to keep pace with the logarithmic scale, (1, 10, 100, 1000&#8230;) instead of (1, 2 ,3, 4&#8230;). But while labeling a range of values on a normal scale is straightforward using a &#8220;rule&#8221; (e.g., <tt>xlabel(1(10)1000)<\/tt> labels the x-axis from 1 to 1000 in steps of 10), there are no multiplicative versions of these rules. Thus, to label a logarithmic x-axis as (1, 10,100,1000&#8230;), you would have to specify each of these values separately in the <tt>xlabel()<\/tt> option, which quickly becomes cumbersome, especially if you need to change the labeling later on.<br \/>\nIn addition, you might want to place ticks on the axes in a logarithmic way (5, 10, 50, 100, 500, 1000&#8230;etc), but you run into the same problem, but worse, because there are more ticks than labels.<br \/>\nFinally, it is often useful to display labels on logarithmic axes as powers of ten (or another base), to avoid very long numbers on the axes. Stata has the <tt>%e<\/tt> format which you can use in axis labels, but I don&#8217;t find the results very nice looking (e.g., 100 is displayed as &#8220;1.0e+02&#8221;). I would rather see something like &#8220;10^2&#8221; (actual superscripts would be even nicer, but I didn&#8217;t manage to make that look decent using <tt>{superscript:}<\/tt>; the superscripts end up too far from the other characters).<\/p>\n<p style=\"text-align: justify;\">The code below solves\u00a0 these problems by automating the specification of the location of labels and ticks, and putting these entire specifications in local macros which are then fed to the graph command. In a similar way, the problem of the formatting of the labels is solved by specifying value labels in a macro, feeding that macro to a <tt>label define<\/tt> command, and using the resulting value labels in a suboption to <tt>xlabel()<\/tt>. Note that for this to work from a do-file, you need to run the code for the macros (2-4) and the code for the graph (5) together.<\/p>\n<p style=\"text-align: justify;\">I admit that this code is altogether longer than what you would need to specify all the values manually, but if you need to change the specification a couple of times, this is still quicker. And of course, you could use it for multiple graphs.<\/p>\n<pre><code>\r\n\/\/ (1) Generate some data to plot\r\nclear\r\nset obs 10000\r\ngen x = _n\r\ngen y = 1000*x^-.5\r\n\r\n\/\/ (2) Setting up the axis label values\r\nlocal labels = \"\"\r\nforval x = 1(1)4{\r\n  local y = 10^`x'\r\n  local labels =  \"`labels'`y' \"\r\n}\r\n\r\n\/\/ (3) Setting up value labels (note the use of compound quotes)\r\nlocal vallabel = \"\"\r\nforval x = 1(1)4{\r\n  local y = 10^`x'\r\n  local vallabel =  `\"`vallabel'`y' \"10^`x'\" \"'\r\n}\r\nlabel define expon `vallabel',replace\r\nlabel val x expon\r\nlabel val y expon\r\n\r\n\/\/ (4) Setting up ticks\r\nlocal ticks = \"\"\r\nforval x = 0(1)3{\r\n  forval z = 1(1)9{\r\n    local y = 10^(`x')*`z'\r\n    local ticks = \"`ticks'`y' \"\r\n  }\r\n}\r\n\r\n\/\/ (5) The actual graph\r\ntwoway  scatter y x, \/\/\/\r\n          yscale(log) \/\/\/      The actual log scales defined here\r\n          xscale(log) \/\/\/\r\n          xlabel(`labels', \/\/\/  Using the axis label values defined in (2)\r\n            grid \/\/\/\r\n            valuelabel) \/\/\/     Using value labels defined in (3)\r\n          xmtick(`ticks') \/\/\/  Using the axis tick values defined in (4)\r\n          ylabel(`labels', \/\/\/\r\n            grid \/\/\/\r\n            angle(horizontal) \/\/\/\r\n            valuelabel) \/\/\/\r\n          ymtick(`ticks') \/\/\/\r\n          aspect(1) \/\/\/\r\n          scheme(s1color)\r\n<\/code>\r\n<\/pre>\n<p>The resulting graph:<\/p>\n<p>\ufeff<a href=\"http:\/\/www.rensecorten.org\/wordpress\/wp-content\/uploads\/logaxes1.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-268\" title=\"logaxes\" src=\"http:\/\/www.rensecorten.org\/wordpress\/wp-content\/uploads\/logaxes1.jpg\" alt=\"\" width=\"611\" height=\"445\" srcset=\"http:\/\/www.rensecorten.org\/wordpress\/wp-content\/uploads\/logaxes1.jpg 611w, http:\/\/www.rensecorten.org\/wordpress\/wp-content\/uploads\/logaxes1-300x218.jpg 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/a><\/p>\n<p>\ufeff<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you want to use logarithmic axes in Stata graphs, Stata doesn&#8217;t exactly make your life easy. Specifying the logarithmic scales is easy enough (via the xscale(log) and xscale(log) options, but making&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/262"}],"collection":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":18,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":281,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/262\/revisions\/281"}],"wp:attachment":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}