Announcement

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

  • graph bar chart - unmatched quote -stata error

    hello I am trying to plot a bar chart for a variable : categorical variable: revreason which is coded in the following way

    For me to plot a bar chart to some the reasons i used

    Code:
    graph bar (percent), over(revreason) ytitle ("Percentage (%)")
    Stata returns the error:
    graph bar (percent), over(revreason) ytitle ("Percentage (%)")
    unmatched quote
    r(198);


    Where exactly is my unmatched quote?

    Click image for larger version

Name:	Screenshot 2024-05-16 at 09.14.41.png
Views:	1
Size:	192.2 KB
ID:	1753514

  • #2
    I'd try taking out the space after ytitle

    If that doesn't work, please show us the results of

    Code:
    dataex revreason

    Comment


    • #3
      thanks, how can I change the colours for the bar charts as they're all blue. and normally with white jet I get really nice colours

      I've created this graph but all the bars are navy blue

      Code:
      set scheme white_jet
      graph bar (percent), over(revreason) ytitle("Percentage (%)")
      Click image for larger version

Name:	Screenshot 2024-05-16 at 09.26.00.png
Views:	1
Size:	440.3 KB
ID:	1753519

      Comment


      • #4
        Give me a data example and I will try some code.

        It's more likely that you'll get an answer from anyone if you give a data example.

        Comment


        • #5
          here it is

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float revreason
          1
          2
          .
          .
          .
          .
          6
          .
          .
          .
          .
          .
          .
          .
          .
          3
          4
          7
          .
          .
          end
          label values revreason q
          label def q 1 "Infection", modify
          label def q 2 "Pain", modify
          label def q 3 "Aseptic loosening", modify
          label def q 4 "Fracture", modify
          label def q 6 "Dislocation", modify
          label def q 7 "Ossification", modify

          Comment


          • #6
            Thanks for the data example. That did most of the work I didn't want to do.

            That said, I searched for the graph scheme you name and couldn't find it quickly. Please note our long-standing request that you explain the provenance of community-contributed code you use.

            I have suggestions on almost everything else.

            I take it that there is a standard medical reason for the ordering of those categories.

            In any circles I know you would get flak for showing percents without explaining frequencies, especially as most problems mentioned appear to concern only 1 patient.

            I have not done anything about the massive number of 0 values shown in #3 but not otherwise explained. Perhaps that just means "No problems" but I won't guess beyond that or at the frequency.

            Horizontal layout is often better.

            For some of those reasons, changing to twoway bar gives you ultimately more flexibility.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float revreason freq
            1  1
            2  1
            3  3
            4  1
            6  5
            7  1
            end
            label values revreason q
            label def q 1 "Infection", modify
            label def q 2 "Pain", modify
            label def q 3 "Aseptic loosening", modify
            label def q 4 "Fracture", modify
            label def q 6 "Dislocation", modify
            label def q 7 "Ossification", modify
            
            su freq, meanonly
            
            gen pc = 100 * freq / r(sum)
            
            gen revreason2 = _n
            
            label value revreason2 q2
            label def q2 1 "Infection", modify
            label def q2 2 "Pain", modify
            label def q2 3 "Aseptic loosening", modify
            label def q2 4 "Fracture", modify
            label def q2 5 "Dislocation", modify
            label def q2 6 "Ossification", modify
            
            gen toshow = strofreal(freq) + " (" + strofreal(pc, "%3.2f") + "%)"
            
            twoway bar freq revreason2, base(0) barw(0.5) ysc(reverse) yla(1/6, valuelabel tlc(none)) xtitle("") xsc(r(0 5.5)) xla(none) horizontal mla(toshow) mlabpos(3) mlabc(black) ytitle(Better text here) color(stc2)
            Click image for larger version

Name:	ossification.png
Views:	1
Size:	37.8 KB
ID:	1753542

            Last edited by Nick Cox; 16 May 2024, 04:35.

            Comment


            • #7
              can I ask pls just using this code

              how did you manage to get the variable -freq- when using:


              Code:
              su freq, meanonly
              gen pc = 100 * freq / r(sum)

              Comment


              • #8
                The variable freq was what I typed in while revising Tara's data example. There were two steps to my logic. First, the data example in #5 was very helpful in listing value labels, but there weren't many data points that could be used. Second, I went back to #1 and copied the frequencies from the table.

                Tara has the full dataset and I don't, so she would need to approach calculations something like this:

                Code:
                bysort revreason : gen freq = _N if revreason < . & _n == 1
                and then the rest of the code should work. (However, the posts are inconsistent: there are values of 0 in the graph in #3 that didn't show in the table in #1.)

                If you use graph bar, most of this should be done for you, but my suggestion is that you need twoway bar here, if only because showing both frequencies and percents -- which is strongly advisable -- needs a customised marker label.
                Last edited by Nick Cox; 16 May 2024, 11:52.

                Comment

                Working...
                X