Announcement

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

  • Long categories labels in graph hbar

    Dear Stata users,

    I encounter this question when I graph hbar. Value labels of my category variable (used as group variable -over(varname)- in bar plot) is very very long, so the resulting plot can not handle them. I have to keep those value labels unchanged because they are meaningful sentences that illustrate respondent's attitude. Can anyone help me out? Thank you very much.

    Code:
    sysuse citytemp
    graph hbar heatdd cooldd tempjan tempjuly, over(division) stack
    gen division2=division
    label define division2 1 "i don't know" 2 "i don't know either" 3 "i don't kown how to handle this knotty problem" 4 "but are you complaining publicly now?" 5 "you can just take it easy and relax" 6 "maybe take a cup of coffee" 7 "no it will have no use" 8 "make a regular label" 9 "how exactly long this label will be"
    label values division2 division2
    graph hbar heatdd cooldd tempjan tempjuly, over(division2) stack
    Click image for larger version

Name:	hbar.png
Views:	1
Size:	66.1 KB
ID:	1637303

  • #2
    This question comes up ever so often. One way is to use the -relabel()- option. Also, you will need to extend the left-hand side margin if you keep the legend.

    Code:
    graph hbar heatdd cooldd tempjan tempjuly, ///
    over(division2, relabel(3 "i don't know how to handle this knotty problem" ///
    4 "but are you complaining publicly now?" 5 "you can just take it easy and relax" ///
    9 "how exactly long this label will be")  axis(outergap(*40))) stack
    Last edited by Andrew Musau; 19 Nov 2021, 20:25.

    Comment


    • #3
      Dear Andrew Musau thank you very much. I take your suggestion, however I still cannot make the bar plot better.
      P.S. In #1, what I mean is that I want bar plot to display full value labels of each catogery, and that I don't want to shorten those value labels.

      Code:
      graph hbar heatdd cooldd tempjan tempjuly, ///
      over(division2, relabel(3 "i don't know how to handle this knotty problem" ///
      4 "but are you complaining publicly now?" 5 "you can just take it easy and relax" ///
      9 "how exactly long this label will be")  axis(outergap(*40))) stack scheme(s2color)
      Click image for larger version

Name:	hbar2.png
Views:	1
Size:	64.2 KB
ID:	1637309

      Last edited by Chen Samulsion; 19 Nov 2021, 20:36.

      Comment


      • #4
        If I drop the legend and run your code with #2

        Code:
        sysuse citytemp
        graph hbar heatdd cooldd tempjan tempjuly, over(division) stack
        gen division2=division
        label define division2 1 "i don't know" 2 "i don't know either" ///
        3 "i don't kown how to handle this knotty problem" 4 "but are you complaining publicly now?" ///
        5 "you can just take it easy and relax" 6 "maybe take a cup of coffee" 7 "no it will have no use" ///
        8 "make a regular label" 9 "how exactly long this label will be"
        label values division2 division2
        graph hbar heatdd cooldd tempjan tempjuly, ///
        over(division2, relabel(3 "i don't know how to handle this knotty problem" ///
        4 "but are you complaining publicly now?" 5 "you can just take it easy and relax" ///
        9 "how exactly long this label will be")) stack leg(off)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	27.7 KB
ID:	1637312



        Even with the legend and extended margin, the labels display in full.

        Comment


        • #5
          Well, I don't know why we produce different results. I try to use different scheme --s2color, s1color, and some community contributed scheme--, however, I can't reproduce your plot in #4.

          Comment


          • #6
            Your labels are truncated in #1, but not in #3. It appears that your font is different, but we are using the default Stata scheme (s2color). Anyway, you need to increase the outer margin in your plot. Try

            Code:
            graph hbar heatdd cooldd tempjan tempjuly, ///
            over(division2, relabel(3 "i don't know how to handle this knotty problem" ///
            4 "but are you complaining publicly now?" 5 "you can just take it easy and relax" ///
            9 "how exactly long this label will be") axis(outergap(*200))) stack scheme(s2color)
            or some larger (smaller) number.

            Comment


            • #7
              Actually value labels of my over variable are recorded in Chinese. I am not sure whether this is relevent. I try to add options axis(outergap(*100)) and change label size simultaneously by adding label(labsize(small)), and this time I get a better looking plot. Thank you so much Andrew!

              Comment


              • #8
                A solution using -splitvallabels- (from SSC) to handle long Chinese value labels.

                Code:
                sysuse citytemp, clear
                graph hbar heatdd cooldd tempjan tempjuly, over(division) stack
                gen division2=division
                label define division2 1 "一个标签" 2 "另一个标签" ///
                3 "一个不短的标签" 4 "一个不短不长的标签" ///
                5 "一个比较长可能需要分行展示的标签" 6 "一个短标签" 7 "一个正常标签" ///
                8 "一个不长不短的标签" 9 "一个吃葡萄不吐葡萄皮不吃葡萄倒吐葡萄皮的长标签"
                label values division2 division2
                
                splitvallabels division2, length(25)
                
                graph hbar heatdd cooldd tempjan tempjuly, ///
                over(division2, relabel(`r(relabel)')) stack leg(span)
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	155.1 KB
ID:	1637321

                Last edited by Fei Wang; 19 Nov 2021, 21:54.

                Comment


                • #9
                  Thank you very much Fei Wang. I used -splitvallabels- but failed before posting here. I try your codes and it is sucessful. I'll check my codes carefully. I must add in end that your solution is so perfect! Thank you.

                  Comment


                  • #10
                    Fei Wang , your response here was very useful to me. By any chance, do you know if it is possible to left adjust the split lines? Thanks!

                    Comment

                    Working...
                    X