Announcement

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

  • Manipulating subtitles in -twoway, by()- graphs

    Dear all:

    I need to make a graph kind of like this:

    Code:
    sysuse auto, clear
    gen id = _n
    twoway dot price id, by(rep78 foreign)
    i.e. there are two variables in the by() statement, and Stata will give me this:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	74.8 KB
ID:	1453639

    So in the title of the small multiples, Stata adds a comma between the labels/values of the by() variables. Is there any way to change that? E.g. to get it to add a colon?

    Thanks for your consideration!
    Go


  • #2
    Well, this works for the example you show:
    Code:
    sysuse auto, clear
    gen id = _n
    
    egen panel = group(rep78 foreign), label
    levelsof panel, local(panels)
    foreach p of local panels {
        local newlabel: label (panel) `p'
        local newlabel: subinstr local newlabel " " ": "
        label define panel `p' `"`newlabel'"', modify
    }
    
    twoway dot price id, by(panel)
    But if your in your real problem the variable whose role is played here by rep78 is a string (or is labeled with strings) that contain(s) embedded blanks, the colon will end up in the midst of that, rather than between rep78 and foreign. If you know the actual string values that are problematic, the code can be modified to be specific to it. But I can't think of a general solution that isn't horrendously complicated.

    I hope somebody else has a better idea.

    Wait: Here's a general solution that's clean:

    Code:
    sysuse auto, clear
    gen id = _n
    
    decode foreign, gen(_foreign)
    egen panel = concat(rep78 _foreign) if !missing(rep78, foreign), punct(:)
    
    twoway dot price id, by(panel)
    Last edited by Clyde Schechter; 16 Jul 2018, 11:18.

    Comment


    • #3
      Thanks, Clyde! This works (apart from the old problem of getting values of string variables into non-alphabetical order)!

      I still can't believe that there is no way to manipulate the comma.

      Thanks again
      Go

      Comment


      • #4
        I still can't believe that there is no way to manipulate the comma.
        Just because I didn't think of one doesn't mean there isn't any. Perhaps somebody else will respond with a more direct solution.

        Comment


        • #5
          I am a geographer and occasionally write on rivers. I assert that most pollution is best avoided by upstream solutions. Once those commas have got into the river it's just too much like hard work to fish them out one by one. Just make sure they never get in there.

          Code:
          sysuse auto, clear
          gen id = _n
          egen group = group(rep78 foreign), label
          
          * look, no commas
          twoway dot price id, by(group, note(Something here if you want it))
          For colons rather than commas: use instead something like.

          Code:
          egen GROUP = concat(rep78 foreign), p(":") decode
          Last edited by Nick Cox; 16 Jul 2018, 12:00.

          Comment


          • #6
            Thank you very much, Nick!

            Comment

            Working...
            X