Announcement

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

  • xtline with background colour dependent on y-value

    I would like to format the background colour on a xtline graph according to the values on the y-axis.
    I have a dataset with 50 persons' BMI at two timepoints: baseline (1) and after intervention (2). I want a graph where each of these 50 BMIs are shown from time 1 to time 2, which I have done using xtline. However, my problem is to change the colour of the background according to the BMI, such that a y-value >=40 has a dark gray background, a y-value 35-39.9 has a semi-dark gray background, etc. I can do this via the scatteri recast(area) function, however, I can't manage to combine the scatteri recast(area)-graph with the xtline graph - I thought I could do this using the overlay command, but I get an error: "xtline is not a twoway plot type", so I'm looking for another way to do this.

    Below is my code. The scatteri recast(area)-graph works perfectly (on its own), as does the xtline, so the problem is to combine them.

    Code:
    xtset id time
    xtline egfr, overlay xlabel (1 2) ylabel (14 45) legend(off)

    Code:
    twoway scatteri 35 1 45 1 45 2 35 2, recast(area) color(gs16%50) || scatteri 30 1 35 1 35 2 30 2, recast(area) color(gs14%50) || scatteri 25 1 29 1 29 2 25 2, recast(area) color(gs12%50) || scatteri 24 1 18.5 1 18.5 2 24 2, recast(area) color(gs10%50) || scatteri 16 1 18.4 1 18.4 2 16 2, recast(area) color(gs8%50) || scatteri 15 1 16 1 16 2 15 2, recast(area) color(gs6%50) xlabel(1 "Pre" 2 "Post") legend(order(1 "Severly obese" 2 "Obese" 3 "Overweight" 4 "Normalweight" 5 "Underweight" 6 "Severe underweight") pos(11)) ytitle(BMI) xtitle("Time")

    Thank you for your time.

    - Sara

  • #2
    Add this within -addplot()-

    Code:
    xtline egfr, overlay xlabel (1 2) ylabel (14 45) legend(off) addplot(scatteri ...)

    Comment


    • #3
      Thank you Andrew Musau! However, would it be possible to do the other way around, so the lines stands out more? If I start with xtline and put the scatteri on top, the lines aren't as visible as if I could start with the scatteri, and then put the lines on top?

      Also, your code results in the legend's colours describing the lines, rather than the background categories:
      Click image for larger version

Name:	legend.png
Views:	2
Size:	20.9 KB
ID:	1658901


      Instead of what I want:
      Click image for larger version

Name:	wanted_legend.png
Views:	1
Size:	21.6 KB
ID:	1658902
      Attached Files
      Last edited by Sara Hansen; 10 Apr 2022, 07:41.

      Comment


      • #4
        Provide a data example, e.g., by copying and pasting the result of

        Code:
        dataex id time egfr

        Comment


        • #5
          input float(id time egfr)
          3 1 30
          3 2 30
          20 1 27
          20 2 28
          33 1 22
          33 2 .
          43 1 38
          43 2 37

          Comment


          • #6
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(id time egfr)
             3 1 30
             3 2 30
            20 1 27
            20 2 28
            33 1 22
            33 2  .
            43 1 38
            43 2 37
            end
            
            set scheme s1color
            separate egfr, by(id) veryshortlabel
            twoway scatteri 35 1 45 1 45 2 35 2, recast(area) color(gs16%50) || ///
            scatteri 30 1 35 1 35 2 30 2, recast(area) color(gs14%50) || ///
            scatteri 25 1 29 1 29 2 25 2, recast(area) color(gs12%50) || ///
            scatteri 24 1 18.5 1 18.5 2 24 2, recast(area) color(gs10%50) || ///
            scatteri 16 1 18.4 1 18.4 2 16 2, recast(area) color(gs8%50) || ///
            scatteri 15 1 16 1 16 2 15 2, recast(area) color(gs6%50) || ///
            line egfr? time, sort lw(medthick) ||line egfr?? time, lw(medthick medthick medthick) sort ///
            xlabel(1 "Pre" 2 "Post") legend(order(1 "Severly obese" 2 "Obese" ///
            3 "Overweight" 4 "Normalweight" 5 "Underweight" 6 "Severe underweight") ///
            pos(11)) ytitle(BMI) xtitle("Time")
            Res.:

            Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.9 KB
ID:	1658933


            Comment


            • #7
              That's perfect! Thank you so much for your help!

              Comment

              Working...
              X