Announcement

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

  • added text specific to each by graph

    I'm looking for a way to insert added text into a histogram with by graphs, such that the added text contains a statistic specific to each by graph. I had done this a brute-force kind of way for the first set of graphs I needed by looping over each category, saving its graph, and then using graph combine. that worked well. however, i suspect there is a more efficient way but haven't had much luck searching through the statalist archives. apologies if i missed someone offering their brilliant solution in the past...

    So, the first set of graphs I created w/o any problem using my brute-force solution is below
    (where a1 is the string variable containing the region name, f1=1 is a completed interview)
    Code:
    foreach l in local region {
    preserve
    keep if a1=="`l'" & f1==1
    ...
    count if pmatch>=.85
    local matches=`r(N)'
    count if f1==1
    local denom=`r(N)'
    local number=(`matches'/`denom')*100
    hist pmatch, bin(25) title(`l') xline(.85) saving("$qc\pmatch_`l'.gph", replace) color(%75)  ///
                xlabel(0(.1)1, labsize(tiny)) ylabel(0(2)10, labsize(tiny)) text(8 0.85 "Percent of" "near-duplicates:" "{bf:`: di %2.0f `number''%}", size(vsmall) placement(e))
    ...
    restore
    }

    I'm now trying to do something similar, but with a very large number of by graphs (as in the below), and would like the analogous added text for each one...whereas in the above, I was making graphs for each geographic region, in the code below I am trying to make a single graph for each region with by graphs by interviewer within each region. In other words, within a new loop, I want to write something like this:
    Code:
    hist pmatch if a1=="`l'", by(interviewer)
    (where `l' is the region name), but then add something within the loop and/or command so that each by-graph would have the added text that pertains to each interviewer. If I had to duplicate the approach above and created a nested loop for levels of the interviewer variable, writing the command itself would not be overly cumbersome albeit inelegant (and create dozens of saved graphs) but I am suspecting that there is a much more efficient and less manual solution out there.

    Appreciate any assistance or links to solutions already posted I may have missed.



    Last edited by Danae Roumis; 13 Sep 2019, 14:24.

  • #2
    Your description is sufficient to suggest a solution, but in future, you should attempt to provide a reproducible example. In this case, that would entail adding a data example. Using the text option will not work with -by()- as the same text will be replicated across all graphs. There are three things working in your favor:

    1. You know exactly where to place the text, specified by the coordinate pair text(y x)
    2. hist command allows an -addplot()- option
    3. The text is common within by groups.

    Combining all these, you can create two variables using your y and x coordinates and a string variable with the required text (common within by groups). The following is a reproducible example.

    Code:
    sysuse census
    hist pop, by(region) scheme(s1mono)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.3 KB
ID:	1516297



    The plots show that we can fit text around the coordinate pair (2e-07 15000000).

    Code:
    gen y = 2e-07
    gen x=15000000
    *REGION SPECIFIC TEXT (EXAMPLE)
    decode region, gen(str_region)
    gen text= "This is "+ str_region+ " region"
    *PLOT INCLUDING REGION SPECIFIC TEXT
    hist pop, by(region, note("")) addplot(scatter y x, mcolor(none) mlab(text) mlabcolor(black)) scheme(s1mono) leg(order(1))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	26.5 KB
ID:	1516298


    Last edited by Andrew Musau; 14 Sep 2019, 05:51.

    Comment

    Working...
    X