Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: how to add short labels to line graphs in lieu of a legend?
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: how to add short labels to line graphs in lieu of a legend?
Date
Fri, 6 Jan 2012 19:51:44 +0000
I use -text()- for this purpose. For example,
separate index, by(cty2) veryshortlabel
bysort cty2 (qdate) : gen last = index[_N]
local options
foreach cty in DE IE IT SE UK US {
su last if cty2 == "`cty'", meanonly
local options `options' text(`r(max)' 206 "`cty'", place(e))
}
di `"`options'"'
line index? qdate, `options' xsc(r(. 207)) legend(off)
See also
SJ-5-4 gr0023 . . . . Stata tip 27: Classifying data points on scatter plots
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q4/05 SJ 5(4):604--606 (no commands)
tips for using separate, gray-scale gradation, and text
characters as class symbols to classify data points on
a scatter plot
Nick
On Fri, Jan 6, 2012 at 7:00 PM, <[email protected]> wrote:
> I have quarterly data for a panel of 6 countries (DE, IE, IT, SE, UK,
> US) which begin in 2007q1 and run until 2011q3 for 4 countries (DE, SE,
> UK, US), but only to 2011q2 for IE and IT
>
> I am graphing a variable "index" against time for each country
> separately, and want to add a country marker label to the right of each
> country's series that shows the relevant country's 2-letter acronym
> (held in string var "cty2"). I want the country marker labels to be in
> the same position relative to the horizontal axis -- i.e. all the
> country labels should be vertically aligned, even for the 2 countries
> with shorter series.
>
> [I currently also identify the separate series using a legend, but I may
> drop that if I can solve my problem.]
>
> The problem is that I can't get the marker labels shown for IE and IT
> (the 2 countries with shorter series), let alone aligned.
>
> My code follows below. There is also a -clist- of the data at the end
> (in the form they were before applying the code).
>
> Please note that I want to do this using command line code. I know that
> I could add these labels using the graph editor (and have done so) --
> but I am not able to guarantee vertical alignment using the editor.
>
> Currently, my strategy is to create 'fake' data for the missing data
> points for IT and IE and overlay a one-point scatter in those cases. But
> I can't get this to work. The code below produces marker labels in the
> right places for the 4 countries with full data, but no marker labels
> for IE and IT.
>
> Suggestions please.
>
>
> * IE and IT don't have 2011q3 data, so add dummy data so that can put
> marker label in right place
>
> set obs `=_N +2'
>
> replace cty2 = "IE" in `=_N-1'
> replace year = 2011 in `=_N-1'
> replace quarter = 3 in `=_N-1'
> replace country = 2 in `=_N-1'
>
>
> replace cty2 = "IT" in `=_N'
> replace year = 2011 in `=_N'
> replace quarter = 3 in `=_N'
> replace country = 3 in `=_N'
> list country cty2 year quarter if cty2 == "IE"
>
> su index if cty2 == "IE" & year == 2011 & quarter == 2, meanonly
> replace index = r(mean) if cty2 == "IE" & year == 2011 & quarter == 3
>
> su index if cty2 == "IT" & year == 2011 & quarter == 2, meanonly
> replace index = r(mean) if cty2 == "IT" & year == 2011 & quarter == 3
>
> list country cty2 year quarter index if cty2 == "IE" | cty2 == "IT",
> sepby(cty2)
>
> twoway ///
> (line index qdate if cty2 == "DE", lcol(black) lpattern(solid) )
> ///
> (line index qdate if cty2 == "IE" & !(year == 2011 & quarter ==
> 3), ///
> lcol(black) lpattern(shortdash) ) ///
> (line index qdate if cty2 == "IT" & !(year == 2011 & quarter ==
> 3), ///
> lcol(black) lpattern(longdash) ) ///
> (line index qdate if cty2 == "SE", lcol(black) lpattern(dash) )
> ///
> (line index qdate if cty2 == "UK", lcol(black) lpattern(dot) )
> ///
> (line index qdate if cty2 == "US", lcol(black)
> lpattern(longdash_dot) ) ///
> (scatter index qdate if cty2 == "DE" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> (scatter index qdate if cty2 == "IE" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> (scatter index qdate if cty2 == "IT" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> (scatter index qdate if cty2 == "SE" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> (scatter index qdate if cty2 == "UK" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> (scatter index qdate if cty2 == "US" & year == 2011 & quarter ==
> 3, ///
> mc(black) mlab(cty2) msize(small) msym(i) ) ///
> , ysize(6) xsize(5) ylabel(85(5)110, angle(0)) xtitle("") ///
> ymtick(85(1)110) xmtick(188(1)206) ///
> legend(label(1 "Germany") label(2 "Ireland") label(3 "Italy")
> label(4 "Sweden") ///
> label(5 "United Kingdom") label(6 "United States") ///
> region(lstyle(none)) order(1 2 3 4 5 6) ) ///
> scheme(s2mono) graphregion(color(white))
>
> * the data
> . clist cty2 year quarter qdate index , noobs
>
> cty2 year quarter qdate index
> DE 2007 1 2007q1 100
> DE 2007 2 2007q2 100.5835
> DE 2007 3 2007q3 101.4588
> DE 2007 4 2007q4 101.7129
> DE 2008 1 2008q1 102.8071
> DE 2008 2 2008q2 102.4118
> DE 2008 3 2008q3 102.0071
> DE 2008 4 2008q4 99.79524
> DE 2009 1 2009q1 95.79527
> DE 2009 2 2009q2 96.09646
> DE 2009 3 2009q3 96.87766
> DE 2009 4 2009q4 97.58356
> DE 2010 1 2010q1 98.08001
> DE 2010 2 2010q2 99.99059
> DE 2010 3 2010q3 100.7812
> DE 2010 4 2010q4 101.2612
> DE 2011 1 2011q1 102.6259
> DE 2011 2 2011q2 102.9082
> DE 2011 3 2011q3 103.4259
> IE 2007 1 2007q1 100
> IE 2007 2 2007q2 98.41753
> IE 2007 3 2007q3 97.83268
> IE 2007 4 2007q4 101.1959
> IE 2008 1 2008q1 98.89506
> IE 2008 2 2008q2 96.79379
> IE 2008 3 2008q3 96.69672
> IE 2008 4 2008q4 93.21848
> IE 2009 1 2009q1 90.6559
> IE 2009 2 2009q2 90.08192
> IE 2009 3 2009q3 89.46284
> IE 2009 4 2009q4 88.39236
> IE 2010 1 2010q1 89.73387
> IE 2010 2 2010q2 89.36792
> IE 2010 3 2010q3 89.56984
> IE 2010 4 2010q4 88.35535
> IE 2011 1 2011q1 90.00674
> IE 2011 2 2011q2 91.41292
> IT 2007 1 2007q1 100
> IT 2007 2 2007q2 100.0841
> IT 2007 3 2007q3 100.2987
> IT 2007 4 2007q4 99.88922
> IT 2008 1 2008q1 100.2963
> IT 2008 2 2008q2 99.64062
> IT 2008 3 2008q3 98.51964
> IT 2008 4 2008q4 96.50864
> IT 2009 1 2009q1 93.61101
> IT 2009 2 2009q2 93.35072
> IT 2009 3 2009q3 93.68325
> IT 2009 4 2009q4 93.63736
> IT 2010 1 2010q1 94.23814
> IT 2010 2 2010q2 94.68584
> IT 2010 3 2010q3 94.98286
> IT 2010 4 2010q4 95.05025
> IT 2011 1 2011q1 95.17928
> IT 2011 2 2011q2 95.46375
> SE 2007 1 2007q1 100
> SE 2007 2 2007q2 100.5711
> SE 2007 3 2007q3 101.1683
> SE 2007 4 2007q4 102.2971
> SE 2008 1 2008q1 101.3358
> SE 2008 2 2008q2 101.2693
> SE 2008 3 2008q3 101.2779
> SE 2008 4 2008q4 97.02799
> SE 2009 1 2009q1 94.67907
> SE 2009 2 2009q2 95.13698
> SE 2009 3 2009q3 94.84025
> SE 2009 4 2009q4 95.67493
> SE 2010 1 2010q1 97.45189
> SE 2010 2 2010q2 99.40958
> SE 2010 3 2010q3 101.2571
> SE 2010 4 2010q4 102.4612
> SE 2011 1 2011q1 103.1432
> SE 2011 2 2011q2 104.2145
> SE 2011 3 2011q3 105.881
> UK 2007 1 2007q1 100
> UK 2007 2 2007q2 101.1683
> UK 2007 3 2007q3 102.3857
> UK 2007 4 2007q4 103.0422
> UK 2008 1 2008q1 103.0663
> UK 2008 2 2008q2 101.7623
> UK 2008 3 2008q3 99.7664
> UK 2008 4 2008q4 97.517
> UK 2009 1 2009q1 95.98917
> UK 2009 2 2009q2 95.79511
> UK 2009 3 2009q3 96.01694
> UK 2009 4 2009q4 96.72504
> UK 2010 1 2010q1 96.88013
> UK 2010 2 2010q2 97.90317
> UK 2010 3 2010q3 98.50919
> UK 2010 4 2010q4 98.00609
> UK 2011 1 2011q1 98.39365
> UK 2011 2 2011q2 98.49461
> UK 2011 3 2011q3 98.98818
> US 2007 1 2007q1 100
> US 2007 2 2007q2 100.9
> US 2007 3 2007q3 101.6368
> US 2007 4 2007q4 102.0672
> US 2008 1 2008q1 101.6138
> US 2008 2 2008q2 101.9485
> US 2008 3 2008q3 101.0018
> US 2008 4 2008q4 98.67801
> US 2009 1 2009q1 96.99068
> US 2009 2 2009q2 96.82294
> US 2009 3 2009q3 97.23042
> US 2009 4 2009q4 98.14186
> US 2010 1 2010q1 99.09315
> US 2010 2 2010q2 100.0184
> US 2010 3 2010q3 100.6395
> US 2010 4 2010q4 101.2255
> US 2011 1 2011q1 101.3159
> US 2011 2 2011q2 101.6521
> US 2011 3 2011q3 102.1576
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/