Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • lpattern() option doesn't work in -twoway bar

    Dear Statalisters,

    I appear to be running into a problem that had been encountered by other people on the old statalist: http://www.stata.com/statalist/archi.../msg00943.html. If one attempts to overlay bars with twoway bar and to use the lpattern() option, the outcome is not satisfactory.

    The outcome is not so bad with histograms. For example, with:

    use http://www.ats.ucla.edu/stat/data/hsbdemo, clear

    twoway (histogram write if female==1, start(30) width(5) color(green)) ///
    (histogram write if female==0, start(30) width(5) ///
    fcolor(none) lcolor(black)), legend(order(1 "Female" 2 "Male" )) name(first)

    twoway (histogram write if female==1, start(30) width(5) color(green)) ///
    (histogram write if female==0, start(30) width(5) ///
    fcolor(none) lcolor(black) lpattern(dash)), legend(order(1 "Female" 2 "Male" )) name (second)
    the second plot has dashed contour lines, but there is a weird line going from (32.5, 0) to (65,0.18).

    With twoway bar, the outcome is very unsatisfactory, since only the contour line of the box in the legend is dashed:

    clear

    sysuse sp500

    gen noise = rnormal(0,200) gen volume_w_noise = volume + noise

    twoway (bar volume date, color(green)) /// (bar volume_w_noise date, fcolor(none) lcolor(black) lpattern(dash)) /// in 1/52

    I really like the strategy of overlaying one bar plot with fcolor(none) and lcolor(black) with another bar plot with color(green) because I then put this graph of an overlaid plot of bars at the bottom of a plot of overlaid lines to look at two dimensions of time-series of two objects that I am comparing, and overlaying the bar plots is the best way to line things up between the plot of lines and the plot of bars.

    Any suggestion of ways to fix the problem with twoway bar and the lpattern() option or alternative plotting scheme would be appreciated.

    Thanks!

  • #2
    Nick Cox comments: I could not read that at all easily. Here is an attempt to make it legible. Moreover, Maxime provided reproducible examples, so it's not clear why the graphs weren't shown too.

    Maxime's post, edited:

    I appear to be running into a problem that had been encountered by other
    people on the old statalist: http://www.stata.com/statalist/archi.../msg00943.html. If one attempts
    to overlay bars with twoway bar and to use the lpattern() option, the
    outcome is not satisfactory.

    The outcome is not so bad with histograms. For example, with:

    Code:
    use http://www.ats.ucla.edu/stat/data/hsbdemo, clear
    
    twoway (histogram write if female==1, start(30) width(5) color(green)) ///
    (histogram write if female==0, start(30) width(5) ///
    fcolor(none) lcolor(black)), legend(order(1 "Female" 2 "Male" )) name(first)
    
    twoway (histogram write if female==1, start(30) width(5) color(green)) ///
    (histogram write if female==0, start(30) width(5) ///
    fcolor(none) lcolor(black) lpattern(dash)), legend(order(1 "Female" 2 "Male" )) name (second)
    the second plot has dashed contour lines, but there is a weird line going from (32.5, 0) to (65,0.18).

    Click image for larger version

Name:	first.png
Views:	1
Size:	13.5 KB
ID:	1290840

    Click image for larger version

Name:	maxime_first.png
Views:	1
Size:	14.2 KB
ID:	1290839



    With twoway bar, the outcome is very unsatisfactory, since only the contour line of the box in the legend is dashed:

    Code:
    clear
    
    sysuse sp500
    
    gen noise = rnormal(0,200)
    
    gen volume_w_noise = volume + noise
    
    twoway (bar volume date, color(green)) ///
    (bar volume_w_noise date, fcolor(none) lcolor(black) lpattern(dash)) ///
    in 1/52
    Click image for larger version

Name:	second.png
Views:	1
Size:	19.7 KB
ID:	1290841



    I really like the strategy of overlaying one bar plot with fcolor(none) and lcolor(black) with another bar plot with color(green) because I then put this graph of an overlaid plot of bars at the bottom of a plot of overlaid lines to look at two dimensions of time-series of two objects that I am comparing, and overlaying the bar plots is the best way to line things up between the plot of lines and the plot of bars.

    Any suggestion of ways to fix the problem with twoway bar and the lpattern() option or alternative plotting scheme would be appreciated.


    Last edited by Nick Cox; 14 Apr 2015, 12:08.

    Comment


    • #3
      The previous was just my attempt to render Maxime's post easier to read. I guess I am not clear what is really sought here, as inability to change the line pattern doesn't seem to me crucial to making either graph much more effective.

      Instead of the histograms, there are several graphs that to me work better. Here is one:

      Click image for larger version

Name:	writing.png
Views:	1
Size:	9.9 KB
ID:	1290861


      This is a hybrid quantile-box plot with the ordered values in each group, conventional boxes showing medians and quartiles and reference lines showing the means.

      I can see here:

      1. The broad contrasts between females and males. Females do better. That is true in terms of any quartile and the means.

      2. Some left skewness. Interesting.

      3. The other details. I can see some small groupings which don't seem important. You can read off more detail yet, not important to me, but researchers who work with the data might want to keep that access.

      The program used in stripplot (SSC). Here is the code for any interested.

      Code:
       
      use http://www.ats.ucla.edu/stat/data/hsbdemo, clear
      stripplot write , over(female) vertical centre cumul cumprob box refline xtitle("") xla(, noticks) yla(, ang(h))
      Here's another.


      Click image for larger version

Name:	writing2.png
Views:	2
Size:	11.0 KB
ID:	1290863


      Code:
       
      stripplot write , over(female) box(barw(0.1))  refline stack ytitle("") yla(, noticks) boffset(-0.2) pctile(0) height(0.4)
      I appreciate that Maxime is most interested in the last plot, but I don't understand enough about the kind of data and the goals to comment.

      As usual, this reply is directed at anyone interested and may not quite match exactly what Maxime is seeking.

      Attached Files

      Comment


      • #4
        Thank you Mr. Cox!

        Comment


        • #5
          So, I'm having a similar problem as the one mentioned in the first post from the old statalist. Basically, when doing an area style graph, with no fill and a dashed outline, the legend overlays a solid line with a dashed line. I'm trying to see if there is a fix for this.

          For example, the following code yields the attached graph:

          sysuse auto, clear
          separate price, by(rep78>=3)
          graph bar (mean) price0 (mean) price1, over(rep78) ///
          nofill bar(2, fcolor(none) lcolor(black) ///
          lwidth(medthick) lpattern(dash)) legend(on)


          I'm actually using a marginsplot with a recastci(rarea) option in which the outline for the CI is dashed and there is no fill, but I am running into the same problem that is seen in the legend of the graph attached. Any help with fixing this, would be greatly appreciated!

          Comment


          • #6
            I can explain why lpattern does not work, but I'm afraid I can't tell you what does work.

            lpattern is an option of textbox_options. When used within the legend option of a graph, "textbox_options affect the rendition of the descriptive text associated with the keys." [G-3 legend_options]. lpattern would not come into play unless you had, for example, a box outlining the text that labels the keys: legend(on box lpattern(dash).

            Instead, you want to affect the rendition of the keys, as there appears to be a border around the key that obscures the dashed line. Unfortunately, I don't see a obvious way to directly change this.
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              I opened the graph in post #5 in the Graph Editor and also found no way to remove the solid line around the key for "mean of price 1".

              Comment


              • #8
                Ok, I think I figured out a way to do this and in the process have discovered an interesting difference when using fcolor(none) vs fcolor(white) [I'm using the s2 color scheme; your fcolor option would probably change to match the graph region color in your scheme].

                Amanda was having a problem displaying the line pattern of a bar in a graph with fcolor(none). Rather than seeing the dash specified in the lpattern, the key's solid swatch border obscured the dashes.

                Amanda's original code:
                Code:
                sysuse auto, clear
                separate price, by(rep78>=3)
                graph bar (mean) price0 (mean) price1, over(rep78) ///
                nofill bar(2, fcolor(none) lcolor(black) ///
                lwidth(medthick) lpattern(dash)) legend(on)
                produced:

                Click image for larger version

Name:	org.png
Views:	1
Size:	24.0 KB
ID:	1328330


                However, using fcolor(white) instead of fcolor(none) produces the desired outcome:


                Click image for larger version

Name:	new.png
Views:	1
Size:	24.1 KB
ID:	1328331



                Amanda, says that her real issue is a case of recasted confidence intervals in marginsplot. In this simple example, I observe the same difference between fcolor(none) vs fcolor(white):

                Code:
                webuse nhanes2
                regress bpsystol agegrp##sex
                margins agegrp#sex
                #delimit ;
                marginsplot,   recastci(rarea) 
                    ciopts(fcolor(none) lpattern(dash) )
                    legend( order(3 "Men" 4 "Women" 1 "confidence interval men" 2 "confidence interval women"))
                ;
                #delimit ;
                marginsplot,   recastci(rarea) 
                    ciopts(fcolor(white) lpattern(dash) )
                    legend( order(3 "Men" 4 "Women" 1 "confidence interval men" 2 "confidence interval women"))
                ;

                As a work around, you can also "trick" the legend in marginsplot by adding line graphs with addplot that are effectively invisible (though you will have to play around with the function to get it in the proper area):
                Code:
                webuse nhanes2
                regress bpsystol agegrp##sex
                margins agegrp#sex
                #delimit ;
                marginsplot,   recastci(rarea) 
                    ciopts(fcolor(none)  lpattern(dash) ) 
                    addplot(function y=x+110, range(1 2) n(1) lcolor(navy) lpattern(dash) || function y=x+110, range(1 2) n(1) lcolor(maroon) lpattern(dash) legend( order(3 "Men" 4 "Women" 5 "Confidence Interval (Men)" 6 "Confidence Interval (Women)")))
                ;


                Stata/MP 14.1 (64-bit x86-64)
                Revision 19 May 2016
                Win 8.1

                Comment

                Working...
                X