Announcement

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

  • Destring to graph

    Hi. I have imported an xlsx file in stata, which contains the following table. I am trying to get a stacked bar, but after importing the data, I am unable to destrng the first column of circunstance. I get this message Circunstance: contains nonnumeric characters; no replace

    It loos like this
    circunstance year percent
    etni 2014 1.2
    etni 2017 0.34
    etni 2021 0.52
    edumo1 2014 6.9
    edumo1 2017 8.62
    edumo1 2021 5.2
    edumo2 2014 38.27
    edumo2 2017 40.34
    edumo2 2021 44.39
    edufa1 2014 5.46
    edufa1 2017 5.99
    edufa1 2021 4.51
    edufa2 2014 39.98
    edufa2 2017 40.64
    edufa2 2021 44.78
    female 2014 3.64
    female 2017 0.74
    female 2021 0.04
    birthreg1 2014 1.77
    birthreg1 2017 0.4
    birthreg1 2021 0.19
    birthreg2 2014 1.8
    birthreg2 2017 0.85
    birthreg2 2021 0.31
    desplaced 2014 0.97
    desplaced 2017 2.08
    desplaced 2021 0.05

    Can someone help me indicating how to change that column in order to be able to do the graph?

    Thanks


  • #2
    I managed to use the circunstance variabe, by creating another one

    encode Circunstancia, gen(circunstancia)

    But now, I am not able in gettng the stacked bar. I am using stata14

    Comment


    • #3
      I am getting this which does not reflect the percentages I have. I have used

      graph bar (percent), over(circunstance) over(year) stack asyvars



      Attached Files

      Comment


      • #4
        destring on circonstance is not a good idea, as you realised. The only numeric characters are suffixes 1 and 2.

        encode just follows alphabetical order, which is not an especially good idea.

        A stacked bar won't work well here, or almost anywhere.

        1. A legend will spring into being, which obliges the reader to go back and forth between legend and graph, or (quite likely) to give up.

        2. Some of your percents are very small. They are going to be hard to spot on a stacked bar chart.

        I used tabplot from the Stata Journal. Some output omitted.

        Code:
        search tabplot, sj 
        
        SJ-22-2 gr0066_3  . . . . . . . . . . . . . . . .  Software update for tabplot
                (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/22   SJ 22(2):467
                bug fixed; help file updated to include further references
        
        SJ-16-2 gr0066  . . . . . .  Speaking Stata: Multiple bar charts in table form
                (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/16   SJ 16(2):491--510
                provides multiple bar charts in table form representing
                contingency tables for one, two, or three categorical variables
        It still uses alphabetical order. One alternative is some kind of sort. I used myaxis from the Stata Journal.

        Code:
        . search myaxis, sj
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        SJ-21-3 st0654  . . Speaking Stata: Ordering or ranking groups of observations
                (help myaxis if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/21   SJ 21(3):818--837
                discusses procedures for datasets based on aggregate
                frequencies and for datasets based on individuals and
                introduce a new convenience command, myaxis, that handles
                many cases directly
        Here is my code. There is a great deal of work needed.

        1. More informative text. Who other than you knows what etni means?

        2. There is likely to be an even better order for the y axis.

        3. Cosmetic changes may appeal, e.g. a mix of colours.

        In English, the spelling is circumstance. Why English follows circum from the original Latin, while Spanish changes it to circun, I do not know.

        Code:
        clear 
        input str9 circunstance    year    percent
        etni    2014    1.2
        etni    2017    0.34
        etni    2021    0.52
        edumo1    2014    6.9
        edumo1    2017    8.62
        edumo1    2021    5.2
        edumo2    2014    38.27
        edumo2    2017    40.34
        edumo2    2021    44.39
        edufa1    2014    5.46
        edufa1    2017    5.99
        edufa1    2021    4.51
        edufa2    2014    39.98
        edufa2    2017    40.64
        edufa2    2021    44.78
        female    2014    3.64
        female    2017    0.74
        female    2021    0.04
        birthreg1    2014    1.77
        birthreg1    2017    0.4
        birthreg1    2021    0.19
        birthreg2    2014    1.8
        birthreg2    2017    0.85
        birthreg2    2021    0.31
        desplaced    2014    0.97
        desplaced    2017    2.08
        desplaced    2021    0.05
        end 
        
        bysort year : egen total = total(percent)
        
        tabdisp year, c(total)
        
        tabplot circunstance year [iw=percent], showval(offset(0.25) format(%03.2f)) horizontal xsc(r(0.8 .)) xtitle("")
        
        myaxis y=circunstance, sort(mean percent) descending subset(year == 2021)
        
        tabplot y year [iw=percent], showval(offset(0.25) format(%03.2f)) horizontal xsc(r(0.8 .)) xtitle("")
        Click image for larger version

Name:	silvana_G1.png
Views:	1
Size:	48.3 KB
ID:	1768119
        Click image for larger version

Name:	silvana_G2.png
Views:	1
Size:	48.4 KB
ID:	1768120

        Comment


        • #5
          One way to get the graph you were trying to get in #3 is

          Code:
          catplot [iw=percent], over(circunstance) over(year) stack asyvars vertical

          where catplot is from SSC. Make sure you install the latest version. https://www.statalist.org/forums/for...tegorical-data

          I am not going to show it here. I think it's a terrible graph.

          Comment


          • #6
            Nick, thank you for helping me with this. I am always learning tons in Statalist.

            Comment

            Working...
            X