Announcement

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

  • Add line on a graph bar mean

    Dear collegues,

    I'm currently working on a graph that represents the average perception of consumers regarding two types of products : Aquaculture and wild products.

    On this graph, i want to compare their opinion regarding several products attributes : Expensive, Taste, Hard to cook ...

    My data are organised as follows :
    Click image for larger version

Name:	graph.JPG
Views:	1
Size:	29.0 KB
ID:	1743829


    "A" correspond to the different attributes tested in our survey; "MeanPê~e" to the average score obtained for wild products; "MeanAqua" to the average score obtained for farmed products ; "Meangl~l" the average score obtained for both.

    I them use the following command to create my graph :

    Code:
    graph hbar (mean) MeanPêche (mean) MeanAqua, by(A)
    I obtain this graph :

    Click image for larger version

Name:	graph.JPG
Views:	1
Size:	74.1 KB
ID:	1743830


    My question is : How can I add a line to each sub-graph for the average score obtained (Meangl~l) for each of the attributes tested?

    I've tried a simple command like the following, but it doesn't work :

    Code:
    graph hbar (mean) MeanPêche (mean) MeanAqua, by(A) yline(Meanglobal)
    I think the solution lies in the two-way chart, but I haven't mastered this tool very well yet. So I wanted to know if someone could help me solve this problem? If you want more information, I'm always available!

    Thanks for your help!

    Regards,

    JF

  • #2
    Not sure you can do it combined, but this works if you do it independently.

    Code:
    clear
    sysuse auto, clear
    
    graph hbar (mean) headroom (mean) gear_ratio , by(foreign)
    summ headroom
    local hmark = r(mean)
    summ gear_ratio
    local gmark = r(mean)
    
    graph hbar (mean) headroom , over(foreign) yline(`hmark') name(headroom)
    graph hbar (mean) gear_ratio , over(foreign) yline(`gmark') name(gear_ratio)

    Comment


    • #3
      A different reaction is that your means are based on different if (presumably) overlapping samples. A fastidious analysis might restrict comparisons to observations for which all variables are present.

      Comment


      • #4
        Dear George Ford Nick Cox. I hope this is the proper place to post my question, although my question is a little different from Jean Dewals in #1. I graph a bar with two over() options, and in the second over()s variable there're three values, thus I get three panels or regions defined by the second over variable (I don't know whether these terminologies are proper). I want to add three ylines on my bar plot, and one line for one panel. That is to say, add one line (y=73.06) at Eur & C.Asia panel, one line (y=71.21) at N.A. panel, and one line (y=70.3) at S.A. panel. As you can see, when I specify yline() option, Stata add ylines across all the three panels. Is there any way to solve my problem? Thank you very much.

        Code:
        sysuse lifeexp
        xtile gnppc5=gnppc, n(5)
        separate lexp, by(region)
        summarize lexp1, meanonly
        local lmark1= r(mean)
        summarize lexp2, meanonly
        local lmark2= r(mean)
        summarize lexp3, meanonly
        local lmark3= r(mean)
        graph bar lexp1 lexp2 lexp3, over(gnppc5, sort(lexp)) over(region) nofill yline(`lmark1' `lmark2' `lmark3')
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	160.9 KB
ID:	1771515

        Comment


        • #5
          By the way, I don't want to use graph twoway bar as an alternative.

          Comment


          • #6
            I think you're going to need to move to twoway to get what you want.

            If your real data groups are also unequal in size, you may appreciate some of the trickery in https://journals.sagepub.com/doi/pdf...6867X241297949

            Here bars to show means that start at zero are one way to throw away or to obscure much of the information. Here is an alternative.

            Yet further: there are many duplicates here, and some other display would be preferable.

            Whether regional means across countries should be population-weighted is another detail that may be irrelevant for your own data.

            Code:
            sysuse lifeexp, clear 
            
            xtile gnppc5=gnppc, n(5)
            separate lexp, by(region)
            
            egen mean = mean(lexp), by(region)
            
            bysort region (gnppc) : gen x = cond(_n == 1, 0.55, cond(_n == _N, 5.45, .))
            
            
            twoway scatter lexp1 lexp2 lexp3 gnppc5, xla(1/5) by(region, row(1) note("") legend(off))|| line mean x, lc(black) lw(medthick) ytitle("`: var label lexp'") name(Chen, replace)
            Click image for larger version

Name:	Chen.png
Views:	1
Size:	48.9 KB
ID:	1771518

            Comment


            • #7
              By the way, I don't want to use graph twoway bar as an alternative.
              Noted, but short of drawing in the lines yourself, I think that you're not going to advance that way.

              Comment


              • #8
                Thank you very much Nick Cox. I think bar is more beautiful than twoway bar for the data in question. That is why I reject twoway bar this time. Well, it seems that I have to accept a compromise. And a further question: I don't understand why x takes values of 0.55, 5.45, and .. Could you explain in brief?

                Comment


                • #9
                  If you draw bars and also the lines from 1 to 5 those lines will start and end in the middle of each bar, which looks odd to me. As I first wrote code for bars with width 0.9, I needed to add half of that at each end.

                  I didn't give you code for the bars because I don't think it's a good idea, but I suggest that the extra length doesn't hurt with other displays.

                  Comment


                  • #10
                    Thank you Nick.

                    Comment

                    Working...
                    X