[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: -graph bar, over()- question and bug report
Friedrich Huebler <firstname.lastname@example.org> asked last week about emphasizing a
range of the x-axis (categorical axis) in a stacked bar graph.
> How can I emphasize some of the groups in a -graph bar, over()-
> graph? I have graphs similar to one produced with these commands:
> . sysuse uslifeexp
> . drop if year>1910
> . graph bar le_w le_b, over(year) stack
> Let's assume I want to emphasize the years 1902 to 1905. As far as I
> know, none of the following ideas can be implemented in Stata.
> - Draw the labels for 1902 to 1905 in a different color. This idea is
> my personal favorite but -relabel(# "text")- has no option for
> specifying the color of the text.
> - Add a horizontal line from 1902 to 1905 (horizontal y lines cross
> the entire graph).
> - Add two vertical lines, one to the left of 1902 and one to the
> right of 1905 (vertical lines cannot be added).
> I would appreciate any suggestions for adding emphasis to the years
> 1902 to 1905.
Friedrich also noted a bug, when using the -text()- option with some single
> Now to the bug report. I thought about creating a vertical line with
> the letters I or l but this leads to an error message.
> . graph bar le_w le_b, over(year) stack text(2 18 "I" 2 55 "I")
> invalid syntax r(198);
> The same error message appears when the letters i, j, or l are used.
Let me take the bug first. We gave some confusing advice about the -text()-
option with -graph bar-, so let's also clear that up. The -text()- option is
allowed after -graph bar- and the caveat about using a range between 0 and 100
for the range of the "x" value in the plotregion is correct.
The error that Friedrich shows us is the result of a bug, and a subtle one at
that. It has to do with the small width of the single letters being plotted.
The bug has been fixed, but it required a change the Stata executable, and
the fix has not yet been certified. We were already planning an executable
update in about 2 weeks and I would like to hold this fix for that update.
There is an interesting work around that Friedrich could try in the interim --
specify a larger font so that the small text width does not trigger the bug.
His graph command might become
. graph bar le_w le_b, over(year) stack text(2 18 "I" 2 55 "I" , size(*1.3))
I do have one suggestion about highlighting the axis labels for the year 1902
through 1905. It is admittedly not elegant and it required some fiddling to
find the right position for things, but the basic idea is to place some color
patches under tick labels using, believe it or not, the -text()- option.
Using Friedrich's example, the command would be something like,
. graph bar le_w le_b, over(year) stack ///
text(-3 22.0 " ", box bcolor(yellow)) ///
text(-3 31.5 " ", box bcolor(yellow)) ///
text(-3 41.0 " ", box bcolor(yellow)) ///
text(-3 50.0 " ", box bcolor(yellow))
We graph outside the plotregion by using a negative "y" position (-3) and plot
an empty string with a yellow background box. My first idea was to not use a
background box, but to write the actual label string in a different color.
Unfortunately the text boxes are written before the axis labels and so the
axis labels over wrote the nicely colored custom "labels".
As an alternative, Friedrich might prefer a single long patch of yellow (or
another color) rather than highlighting each label individually. He could get
that with a single long set of spaces placed at the location of the first
-text()- option in the command above.
* For searches and help try: