Announcement

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

  • Splitting labels in hbar in 2 parts

    Hi all,
    I am trying to using the following code
    Code:
    splitvallabels covid_prcny, nob
    gr hbar (count), over(covid_prcny, gap(*1) label(labsize(small)) relabel(`r(relabel)')) ///
    blabel(bar, size(vsmall) format(%4.1f)) yla(0(100)1300, angle(55) labsize(vsmall))  ysize(12) xsize(10)  ytitle("respondents", size(small))  title("impact", size(small) span)
    to split the x axis labels in hbar. However, it is splitting the label after each word. Instead, I want it split in 2 parts. How can we do that? Much appreciate any help. Thanks.
    Click image for larger version

Name:	Graph1.jpg
Views:	1
Size:	58.0 KB
ID:	1673275


  • #2
    I think that you need to show a reproducible example. Your description is not particularly helpful. splitvallabels is from SSC, as you are asked to explain (FAQ Advice #12).

    Comment


    • #3
      Code:
      // open example data
      webuse auto2, clear
      //create a very long value label we might want to split up
      label define repair 1 "absolutly horribly worthless", modify
      graph hbar (count), over(rep78)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.8 KB
ID:	1673346

      Code:
      //split the label in two parts
      label define repair 1 `""absolutly horribly" "worthless""', modify
      graph hbar (count), over(rep78)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.2 KB
ID:	1673347

      Code:
      //split the label in three parts
      label define repair 1 `""absolutly" "horribly" "worthless""', modify
      graph hbar (count), over(rep78)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.2 KB
ID:	1673348
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Maarten Buis Thank you, it's a helpful suggestion. However there are many more such labels for many more graphs. I was looking for something that would split the labels within the graph command without putting in the separate commands to modify each label individually.
        Last edited by Lars Pete; 13 Jul 2022, 11:30.

        Comment


        • #5
          Andrew Musau Please find the sample data here.
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte(covid_prcny gender) double age
          . .  .
          . 1 34
          . 2 32
          . 2 38
          . 2 32
          . 2 29
          . 1  .
          . 2 31
          . 4  .
          . 2 30
          . 1 30
          . 2  .
          . 1  .
          . 2 31
          . 4 31
          . 2 32
          . 1 33
          3 1 33
          3 1 38
          . 1  .
          . 2  .
          1 1  .
          2 4  .
          2 1  .
          . 2  .
          . 1  .
          . 1 31
          . 1 32
          3 2 27
          3 2 33
          3 1 34
          4 2 36
          2 1 30
          3 1 38
          4 1 39
          3 1 30
          . 2  .
          4 2 32
          3 1 32
          2 1  .
          3 2 31
          . 1 30
          3 1 53
          1 1  .
          3 2 40
          3 1 33
          3 2 34
          2 4 28
          3 2 38
          1 1 33
          3 2 29
          3 1 40
          3 1 28
          . 1 27
          1 2 42
          3 2 29
          3 2 36
          2 2 35
          4 4  .
          3 4  .
          . 1 26
          . 2  .
          2 1 35
          3 1 30
          3 2 30
          3 1 28
          . 1 30
          4 1 34
          3 2 31
          3 2 27
          3 2 27
          3 1 38
          3 2 30
          2 1 29
          3 1 31
          2 1 39
          3 2 41
          4 2 29
          4 2 40
          3 2 45
          3 1 32
          3 2 55
          . 1  .
          3 2 33
          3 2 31
          3 2 33
          1 1  .
          3 1 33
          . 2  .
          3 1 26
          1 1 38
          . 2 35
          3 1 33
          3 1 40
          2 1 33
          3 2 36
          3 2  .
          3 1 39
          3 . 37
          3 1 31
          end
          label values covid_prcny labels100
          label def labels100 1 "Yes, it increased my interest in practicing in New York.", modify
          label def labels100 2 "Yes, it decreased my interest in practicing in New York.", modify
          label def labels100 3 "No", modify
          label def labels100 4 "I don't know.", modify
          label values gender labels1
          label def labels1 1 "Male", modify
          label def labels1 2 "Female", modify
          label def labels1 4 "Prefer not to disclose", modify

          Comment


          • #6
            The issue isn't with the command splitvallabels. You need to increase the outer gap on the categorical axis.

            Code:
            splitvallabels covid_prcny , length(30) recode
            set scheme s1mono
            gr hbar (count), over(covid_prcny, gap(*1) label(labsize(small)) ///
            relabel(`r(relabel)') axis(outergap(40))) blabel(bar, size(vsmall) ///
            format(%4.1f)) yla(0(100)1300, angle(55) labsize(vsmall))  ysize(12) ///
            xsize(10)  ytitle("respondents", size(small))  title("impact", size(small) span)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	42.0 KB
ID:	1673420

            Comment


            • #7
              Thank you Andrew Musau

              Comment


              • #8
                Andrew May I ask a follow-up question? How to reduce the width of the individual bars (not the gap between them)? hbar is not allowing me to use the barwidth option.

                Click image for larger version

Name:	Graph_bar_width.jpg
Views:	1
Size:	40.0 KB
ID:	1673454

                Comment


                • #9
                  You need to specify -width()- within -over()-.

                  Comment


                  • #10
                    Originally posted by Andrew Musau View Post
                    You need to specify -width()- within -over()-.
                    I tried this but to no avail. It gives an error saying option width is not allowed. I checked the Stata pdf for hbar with couldn't find it there too. May I request you to use the example data and provide the syntax? I really appreciate it. Thank you.

                    Comment


                    • #11
                      Sorry, the option is -gap()- and not -width()-. Look at my reply in #6 of your previous thread: https://www.statalist.org/forums/for...abels-in-graph

                      ADDED IN EDIT:

                      How to reduce the width of the individual bars (not the gap between them)? hbar is not allowing me to use the barwidth option.
                      Decreasing the gap between bars increases the width of bars, but you point out that this is not what you want in #8. See if the thread below helps as it appears that the poster was asking the same question.

                      https://www.statalist.org/forums/for...plot-bar-width
                      Last edited by Andrew Musau; 14 Jul 2022, 04:42.

                      Comment


                      • #12
                        Hi Andrew, a combination of both gap() and aspect() worked and I finally got what I wanted. Thanks much.
                        Last edited by Lars Pete; 14 Jul 2022, 12:10.

                        Comment


                        • #13
                          Originally posted by Andrew Musau View Post
                          The issue isn't with the command splitvallabels. You need to increase the outer gap on the categorical axis.

                          Code:
                          splitvallabels covid_prcny , length(30) recode
                          set scheme s1mono
                          gr hbar (count), over(covid_prcny, gap(*1) label(labsize(small)) ///
                          relabel(`r(relabel)') axis(outergap(40))) blabel(bar, size(vsmall) ///
                          format(%4.1f)) yla(0(100)1300, angle(55) labsize(vsmall)) ysize(12) ///
                          xsize(10) ytitle("respondents", size(small)) title("impact", size(small) span)
                          [ATTACH=CONFIG]n1673420[/ATTACH]
                          I have the same problem here, but can not solve it yet. I have long labels in the variable "aspect" (I can not separate them individually), and I want them to be split into 2 parts.
                          This is my graph




                          I used this code after reading your answers:


                          splitvallabels aspect, length(30) recode
                          set scheme s1mono
                          local relabel `r(relabel)'
                          graph hbar (asis) v2023 v2022 if actor=="Alumnos" & v2023>=3 & highest==1, ///
                          over(aspect, sort(v2023) descending lab(labsize(small))) relabel(`r(relabel)')) blabel(total, color(white) position(inside)) ///
                          bar(1, color(orange) fintensity(inten95)) bar(2, color(gray) fintensity(inten50)) yl( , nogrid) ysc(off) span size(medium)) ///
                          legend(label(1 "Ciclo 2023-22") label(2 "Ciclo 2022-21")) legend(region(lstyle(none)))

                          or this other code:

                          graph hbar (asis) v2023 v2022 if actor=="Alumnos" & v2023>=3 & highest==1, ///
                          > over(aspecto, label(labsize(small)) relabel(`r(relabel)')) axis(outergap(30))


                          but I got this kind of errors:

                          --option relabel() not allowed
                          --option span not allowed
                          --option axis() not allowed


                          Much appreciate any help. Thanks.
                          Attached Files

                          Comment


                          • #14
                            You don't present a data example, but I think the following recreates your figure.

                            1. You need to split value labels, not a string variable. If you have a string variable to begin with, use encode to create a numerical variable with value labels.
                            2. The rest are just syntax errors resulting from including suboptions outside of the parentheses or omitting options altogether.

                            Code:
                            clear
                            input float(v2023 v2022) str100(aspecto)
                            3.85 2.93 "Participa en el cuidado del medio ambiente (actúa)"
                            3.48 1.54 "Confía en su capacidad para utilizar las TIC"
                            end
                            lab var v2023 "Ciclo 2023-22"
                            lab var v2022 "Ciclo 2022-21"
                            
                            encode aspecto, g(lab)
                            
                            cap noi ssc install splitvallabels, replace
                            splitvallabels lab, length(30) recode
                            set scheme s1mono
                            local relabel `r(relabel)'
                            graph hbar (asis) v2023 v2022, ///
                            over(aspect, sort(v2023) descending lab(labsize(small)) relabel(`r(relabel)')) blabel(total, color(white) position(inside)) ///
                            bar(1, color(orange) fintensity(inten95)) bar(2, color(gray) fintensity(inten50)) yl( , nogrid) ysc(off) title(Whatever, span size(medium)) ///
                            legend(label(1 "Ciclo 2023-22") label(2 "Ciclo 2022-21")) legend(region(lstyle(none)))
                            Res.:
                            Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.3 KB
ID:	1730097

                            Last edited by Andrew Musau; 13 Oct 2023, 03:22.

                            Comment


                            • #15
                              It works Andrew, many thanks!

                              Comment

                              Working...
                              X