Announcement

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

  • Two level labels (markers) on a scatterplot

    Hi. I am trying to make the following two changes to the attached graph:

    1) I want to make my x-axis labels appear on two lines/levels so as to avoid the mess in the attached graph. I'd like the terms "Mandate" and "Platform" at the dashed, red lines to appear closest to the x-axis and the years to appear at a level/line below.
    2) the shading appears to remove the graph boundary line on top. How do I prevent this from happening? Is there any way I can set a y-axis limit to the height of the shading?

    I'd appreciate any help in doing this. I've been spinning my wheels for quite a while on this.

    Code:
    
    set scheme s1color
    
    su year, meanonly
    local first = r(min)
    local last = r(max)
    
    forval y = `first'/`last' {
        su mdate if year == `y', meanonly
        local labelcall `labelcall' `r(mean)' "`y'"
        local tickpos = r(max) + 0.5
        if `y' < `last' local tickcall `tickcall' `tickpos'
    }
    
    mac li
    
    
    
    scatter one mdate,  ///
    ytitle("Number of teleconsults") yscale(range(0 .)) ylabel(#6, nogrid labsize(vsmall) ///
    angle(horizontal)) mc(black) msize(vsmall)  ///
    xline(764.5, lwidth(16.1) lcolor(gs15)) ///
    xline(771.4, lwidth(15.85) lcolor(gs13)) ///
    xline(761 768, lpattern(dash) lcol("cranberry")) ///
    xla(`labelcall' 761 "Mandate" 768 "Platform", noticks labsize(vsmall) nogrid) ///
    xtick(`tickcall', tlength(*1)) ///
     xtitle("Year" "", height(6)) graphregion(fcolor(white))
    Attached Files
    Last edited by Scott Rick; 26 Dec 2024, 13:27.

  • #2
    You don't need the x-axis title 'Year,' as it is evident that the years are on the x-axis. You can use the -text()- option to include descriptions of the red dashed lines at your preferred positions.


    Code:
    help added_text_options

    Comment


    • #3
      Andrew Musau Thank you! That worked perfectly! Do you have any suggestions on how I can recover the boundary line at the top of the shaded regions?

      And, happy new year!

      Comment


      • #4
        See https://journals.sagepub.com/doi/pdf...867X1601600315.
        Last edited by Andrew Musau; 02 Jan 2025, 16:46.

        Comment


        • #5
          That SJ article won't help with this particular issue. I'll demonstrate.

          First, recreate the dataset.
          Code:
          local url "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/vitalstatisticspopulationandhealthreferencetables/current/annualreferencetables2021.xlsx"
          import excel year=A sc_births=L using "`url'", clear sheet("2") cellrange(A7:L141)
          destring year, replace
          tempfile births
          save `births'
          import excel year=A sc_deaths=AD using "`url'", clear sheet("3") cellrange(A7:AD141)
          merge 1:1 year using `births'
          Then recreate the graph in the SJ article.
          Code:
          set scheme sj
          local linecall sc_births sc_deaths year if inrange(year, 1900, 1960), lpattern(dash solid) xscale(r(1900 1967)) legend(off) ytitle(birth and death rates/1000)
          local scattericall 19.6 1960 "births" 11.9 1960 "deaths", mlabsize(*1.2) msymbol(none)
          generate upper = 30
          local barcall upper year if inrange(year, 1914, 1918) | inrange(year, 1939, 1945), bcolor(gs14) base(10)
          twoway bar `barcall' || line `linecall' || scatteri `scattericall' plotregion(margin(zero))
          Click image for larger version

Name:	image_36575.png
Views:	1
Size:	233.0 KB
ID:	1770396


          Seems fine, until you change the scheme.
          Code:
          graph display, scheme(s1color)
          Click image for larger version

Name:	broken.png
Views:	1
Size:	237.0 KB
ID:	1770397


          The fix is to make sure the bar outlines are drawn "inside" and the plot region outlines are drawn "outside".
          Code:
          twoway bar `barcall' lalign(inside) || line `linecall' || scatteri `scattericall' plotregion(margin(zero) lalign(outside)) scheme(s1color)
          Click image for larger version

Name:	fixed.png
Views:	1
Size:	236.7 KB
ID:	1770398

          Comment


          • #6
            Thanks Nils Enevoldsen. That worked perfectly!

            Comment

            Working...
            X