Announcement

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

  • Twoway scatter: invalid 'mlabels', but only after adding graph options

    I am trying to run a boxplot graph, using scatter to add value labels to the median point. The code runs fin (and gives me the desired result, complete with labels) until I add the , graph options for labelling etc, at which point I get the newly-ecountered error message "invalid 'mlabels'". I have tried moving the separator comma to the end of the previous line, tried with and without gaps etc, but can't get it to run. Secondary issue: in earlier versions of the graph, it was also not truncating the xscale at 4.5 as requested, but running out to 5. Can anyone help me spot the error? Code and small sample od data below. Thanks.

    Code:
    twoway ///
    rbar p25 med offset if ecat == 1, barw(.2) color(dkgreen) || ///
    rbar med p75 offset if ecat == 1, barw(.2) color(eltgreen) || ///
    rbar p25 med offset if ecat == 0, barw(.2) color(red) || ///
    rbar med p75 offset if ecat == 0, barw(.2) color(red*0.5) || ///
    scatter med offset_lab if ecat==1, msymbol(none) mlabel(medlabel_str) mlabcolor(green) mlabposition(3) mlabsize(vsmall) || ///
    scatter med offset_lab if ecat==0, msymbol(none) mlabel(medlabel_str) mlabcolor(red) mlabposition(3) mlabsize(vsmall) ///
    , ylabel(#5, axis(1) labsize(small) format(%3.0fc) angle(0)) ///
    title("Persen selisi harga $graph_nmeds obat studi per fase pengadaan ecatalog dan manual""dibandingkan dengan harga swasta ekatalog di fase yang sama", size(small)) ///
    xtitle("Fase pengadaan ecatalog") ///
    xlabel(2 "Ecatalog 2021-2022" 3 "Obatpedia" 4 "Obatpedia + konsolidasi", labsize(vsmall) noticks) xscale(range(1.5 4.5)) ///
    subtitle("$graph_nfaskes faskes mitra PROmeds", size(vsmall)) ///
    legend(off) ///
    plotregion(lstyle(none))
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ecat phase) double(offset p25 p75 med) str4 medlabel_str double(offset_lab phase_lab)
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    0 2 2.25 -4.028416300538851e-06 115.10749354005169 18.242105263157903 "18.2" 2.33 2
    end
    label values phase phase
    label def phase 2 "2 Ecatlog 2021-22", modify
    label values phase_lab phase_lab
    label def phase_lab 2 "Ecatalog 2021-2022", modify
    Last edited by Elizabeth Pisani; 29 May 2026, 03:16.

  • #2
    Have you got one comma too many? I don't think there should be a comma before the ylabel() call.

    Comment


    • #3
      I can verify that Nick's solution in #2 resolves the problem of invalid 'mlabels'.

      But it does not address the issue raised about the scale on the x-axis. Your data set is a rather restricted subset of your full data, and, as your x-variable is value-labeled, it isn't clear to me that you actually are getting the x-axis extended to 5. It looks like it only goes to 4.5 to me, but I can't be sure.

      But there is a general Stata -graph- principle in play: the range of the x- or y-axis always includes the full range of the values of the x- or y- variables encountered in the data that is included in the graph. Command options such as -xlabel()- or -xrange()- can extend the axis if you specify values that are beyond the data range, but they can never limit the axis to less than the full range of the variable.

      So I imagine that in your full data set there are values of offset larger than 4.5. In order to restrict the x-axis to a maximum of 4.5 you will have to exclude those observations from the graph. You could do that by -drop-ping those observations from the data set before you create the graph, or by adding -if inrange(offset, 1.5, 4.5)- conditions to all of the -rbar- and -scatter- commands. The former is obviously easier to type, if you need those values of offset later in the code, then the latter preserves them. (You can also have it both ways by -preserve-ing the data set, -drop-ping the observations, graphing the data, then -restore-ing the original data. This would still be less typing than all of those -if inrange(...)- conditions on every -rbar- command.)

      Comment

      Working...
      X