Announcement

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

  • Help: Define different background colours for different ranges of y-axis within twoway plot

    Dear stata forum,

    I´d like to ask your help in the following setting: I am using a toway plot (lpolyci) to plot outcomes of a cross-sectional follow-up among ca. 900 patients (see below). My x-axis displays the time in months, the y-axis ranges from 0-30 (symptom score of the patients).
    Now, I´d like to have different background settings for the inner region of the graph. Specifically, I´d like a green band for all values below 7, orange below 19, red below 35 (y-axis). Click image for larger version

Name:	Screen Shot 2021-01-19 at 19.27.27.png
Views:	1
Size:	134.0 KB
ID:	1590277

    Your help is greatly appreciated, would anyone know, how to do this?

    Thank you and kind regards,

    Philipp

  • #2
    tw scatteri provides one way, paying attention to the order of plots and the foreground colors. Spot the country!

    Code:
    webuse grunfeld, clear
    tw (scatteri 1500 1935 1500 1955, recast(area) ///
    color(black%99)) (scatteri 1000 1935 1000 1955, ///
    recast(area) color(red%99)) (scatteri 500 1935 500 ///
    1955, recast(area) color(yellow%99)) (line invest year ///
    if company==1, lcolor(blue)  plotregion(margin(zero)) ///
    scheme(s1color) leg(order(4)))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.1 KB
ID:	1590289

    Comment


    • #3
      Code:
       sysuse auto,clear
       twoway lpolyci price mpg ,  plotregion(margin(zero)) legend(off) ylabel(,nogrid)||  ///
        function y=41,range(0 5000) recast(area) color(red%20) base(0)  hor  || ///
       function y=41,range(5000 10000) recast(area) color(orange%20) base(0) hor|| ///
       function y=41,range(10000 15906) recast(area) color(green%20) base(0) hor
      Attached Files
      Last edited by Raymond Zhang; 19 Jan 2021, 13:15.
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        It seems that @Andrew Musau' graph is more beautiful than what i plot.
        Best regards.

        Raymond Zhang
        Stata 17.0,MP

        Comment


        • #5
          Only if you like the German flag. Multi-color backgrounds are hard to pull off and are best avoided if possible.

          Comment


          • #6
            Whether or not it makes sense to use multi-color backgrounds is open for discussion, but note that it matters which part of the syntax of Zhang is offered first to Stata:

            Code:
            sysuse auto,clear
            twoway function y=41,range(0 5000) recast(area) fc(red%20) lc(red%1) base(0)  hor  || ///
            function y=41,range(5000 10000) recast(area) fc(orange%20) lc(orange%1) base(0) hor || ///
            function y=41,range(10000 15906) recast(area) fc(green%20) lc(green%1) base(0) hor || ///
            lpolyci price mpg , plotregion(margin(zero)) legend(off) clc(gs3) ylabel(,nogrid)
            Note that I have change and added some syntax to get the visual effect of flat background sections, i.e. without a border, like: lc(green%1) clw(.1)
            In this case, using lc(none) will disable the coloring of the section where that is used.
            Also I changed the color of the lpoly line, using
            clc(gs3)

            Then you get:
            Click image for larger version

Name:	Tutorial_background_multi_color_example1.png
Views:	1
Size:	48.0 KB
ID:	1590312
            http://publicationslist.org/eric.melse

            Comment


            • #7
              After @ericmelse's corrected,it looks better.
              Attached Files
              Best regards.

              Raymond Zhang
              Stata 17.0,MP

              Comment


              • #8
                Dear Andrew, Raymond, and Eric,

                thank you all very much for your swift answer and great help!

                I have adapted my graph accordingly, which was almost no problem, exept for the hue of of the background. In my case the percentage option "fc(red%20)" somehow does not work:


                . twoway function y=140,range(0 7) recast(area) fc(green%20) lc(green*0.4) base(0) hor || function y=140,range(7 19
                > ) recast(area) fc(orange*0.5) lc(orange*0.5) base(0) hor || function y=140,range(19 35) recast(area) fc(red*0.4) lc
                > (red*0.4) base(0) hor || lpolyci postopipss Time_followup_months, plotregion(margin(zero)) legend(off) clc(gs3) yla
                > bel(,nogrid) xlabel(#10) xtick(#10) xtitle("Time follow-up months")ytitle("IPSS total") xsc(r(0 140)) yscale(range(
                > 0.75 35)) ytick(#10) ylabel(#10)

                (note: named style green % 20 not found in class color, default attributes used)


                However, I got around this by using color*percentage ("fc(green%20)"), and if I´m not mistaken, my graph looks almost identical (here´s the code):

                twoway function y=140,range(0 7) recast(area) fc(green*0.4) lc(green*0.4) base(0) hor || function y=140,range(7 19) recast(area) fc(orange*0.5) lc(orange*0.5) base(0) hor || function y=140,range(19 35) recast(area) fc(red*0.4) lc(red*0.4) base(0) hor || lpolyci postopipss Time_followup_months, plotregion(margin(zero)) legend(off) clc(gs3) ylabel(,nogrid) xlabel(#10) xtick(#10) xtitle("Time follow-up months")ytitle("IPSS total") xsc(r(0 140)) yscale(range(0.75 35)) ytick(#10) ylabel(#10)


                Click image for larger version

Name:	Screen Shot 2021-01-21 at 20.19.28.png
Views:	3
Size:	70.8 KB
ID:	1590619
                Attached Files

                Comment


                • #9
                  % means to adjust the transparency, while * adjusts the color depth.I don't know why you can not use %. Maybe you can copy my code and run in Stata, if Stata still shows "fc(green%20) does not work",then it maybe because of your Stata version.My stata version is 16.1.
                  Best regards.

                  Raymond Zhang
                  Stata 17.0,MP

                  Comment


                  • #10
                    @Philipp Gild if you are using Stata 15.1 or Stata 16, you can use % to adjust the color transparency, but if you use Stata 14 or older version,you can not adjust the color transparency.
                    Best regards.

                    Raymond Zhang
                    Stata 17.0,MP

                    Comment


                    • #11
                      Ok, I see. That seems to be the issue as I use STATA 14...

                      Comment

                      Working...
                      X