Announcement

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

  • x axis with graph bar

    Hi Statausers,

    I know it's a very basic question, but I couldn't find the right answer. How can I add one mark in the x-axis in the middle of the axis saying 50%?

    Code:
    graph bar  (count) first_obs if first_obs == 1, over(seminar_series_mod_num, lab(nolab) sort(1)) ylabel(0 25 50 75 100 ) bar(1, fcolor(gs11) lcolor(white) lwidth(0.1)) graphregion(color(white)) bgcolor(white) ytitle("Number of Presentations")
    In case it's easier, it can be also a vertical line in the middle of the chart (so at the 50%)

    Click image for larger version

Name:	borrar.png
Views:	1
Size:	146.4 KB
ID:	1732369

    Last edited by Jean Jacques; 01 Nov 2023, 16:52.

  • #2
    I don't know a way to do this. As the help explains graph bar isn't considered to have an x axis at all. Its horizontal axis is a categorical axis.

    For what you want, there are at least two ways forward. One is to calculate the counts directly and call up twoway bar.

    Another is to wonder whether spikeplot will help.

    Comment


    • #3
      Technically, you could use -relabel()- within -over()-, blanking out all bars except the middlemost bar. A loop over the bar numbers may make this easier.

      Comment


      • #4
        Thanks to both of you.

        I think i go it through this:
        Code:
        local call
        levels seminar_series_mod_num, local(levels)
        foreach l of local levels {
                local call `call' `l' " "
            }
        
        graph bar  (count) first_obs if first_obs == 1, over(seminar_series_mod_num, relabel(`call' ) sort(1)) ylabel(0 25 50 75 100 ) bar(1, fcolor(gs11) lcolor(white) lwidth(0.1)) graphregion(color(white)) bgcolor(white) ytitle("Number of Presentations")  b1title("Seminar Series")
        However I cannot drop the "92" in the axis, which i don't know how it comes from.
        Click image for larger version

Name:	borrar.png
Views:	1
Size:	239.2 KB
ID:	1732580


        Additionally with a code like this

        Code:
        gen uno = 1
        gen test = 50
        sort seminar_series_mod
        egen N_test = count(uno) if first_obs == 1, by (seminar_series_mod)
        twoway bar N_test seminar_series_mod_num if first_obs == 1 || line seminar_series_mod_num test
        I can sort the bars by height.
        Last edited by Jean Jacques; 03 Nov 2023, 09:57.

        Comment


        • #5
          Hi, anybody has any hint? I'm particularly curious about the second code I provided where I cannot sort the bars by height.

          Comment


          • #6
            The usual FAQ recommendation for a data example still applies here.

            Comment


            • #7
              Sorry, I didn't think it was necessary in this case as it's just plotting in columns the distribution of a variable in an ordered way and drawing a line showing the 50% (or 25% or 75%).

              Here is again the code

              Code:
              gen uno = 1
              gen test = 50
              sort seminar_series_mod
              egen N_test = count(uno) if first_obs == 1, by (seminar_series_mod)
              twoway bar N_test seminar_series_mod_num if first_obs == 1 || line seminar_series_mod_num test
              and here is the data. Clearly seminar_series_mod_num is an identifier. There're more than 100 different ones.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte seminar_series_mod_num
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              81
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              75
              end

              Comment


              • #8
                #4 looked like closure to me, but now #5 says that you cannot sort the bars by height, contrary to #4.

                The data example doesn't match your code and I am now more unclear what you want and can't get.

                Perhaps this will help, where myaxis is from the Stata Journal.

                Code:
                bysort seminar: gen freq = _N 
                
                myaxis xtouse = seminar, sort(mean freq) 
                
                su xtouse, meanonly 
                
                twoway bar freq xtouse, xla(1/`r(max)', valuelabel) barw(0.8) base(0)

                Comment


                • #9
                  Indeed it was poorly and very badly explained. My apologies! Here I go again.

                  I have presentations in a panel data structure in which each row is one minute of the presentation. That presentation has 2 key variables (seminar series and specific_semnar). I want a distribution in which each bar is a seminar series and the height of the bar is the number of different specific_seminar. That's why at some point in my code I used "if first_obs == 1". Because as it is a panel structure, I only care of one observation per specific_seminar.

                  Also the data provided was unsufficient. Here you have the 3 variables that i mentioned.

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input byte seminar_series_mod_num int video_name_num byte first_obs
                  0 2205 1
                  0 2205 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 1
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2206 0
                  0 2207 0
                  0 2207 1
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2207 0
                  0 2208 0
                  0 2208 0
                  0 2208 1
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  0 2208 0
                  end
                  The same pattern repeats but seminar_series_num takes number 1 and specific_seminar other numbers. first_obs takes 1 on the first observation of specific_seminar. Thanks and again sorry for the previous explanation.

                  Comment


                  • #10
                    I don't doubt that this is relatively simple really but sorry, I don't get it. For example, your data example has a variable video_name_num that isn't referred to anywhere else. I hope someone else can see that you want.

                    Comment


                    • #11

                      video_name_num is just what i called for simplicity specific_seminar

                      Comment


                      • #12
                        Your data example is not useful. Create one that reproduces the problem, even if you end up with a total of 5 bars.

                        Comment


                        • #13
                          Sorry, I feel it's a simple problem that clearly I'm not being able to communicate properly.

                          I took another chunk of data. The first variable, seminar_series_mod_num, is the seminar_series. As a seminar series it host different seminars (each seminar is in video_name). The variable first_obs is 1 on the first observation of each seminar_series.

                          I want to plot a bar chart in which on the x-axis is each seminar_series and on the y-axis its the count of seminars. And i want it in an ordered way, meaning that the first bar should have a value of 6 (because the seminar series that organized less seminars, organized 6) and the bar more to the right should have 117 as the seminar series with more seminars, organized 117 events.

                          In the dataset there's information for 2 seminar series (denoted with the identifies 0 and 1). The seminar series 0 organized 3 seminars (identified with 2, 3 and 4) and the seminar series 1 organized two seminars (identified with 1 and 2). I hope now it's more clear.

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input byte seminar_series_mod_num int video_name_num byte first_obs
                          0 2 1
                          0 2 0
                          0 2 0
                          0 3 1
                          0 3 0
                          0 3 0
                          0 3 0
                          0 3 0
                          0 3 0
                          0 3 0
                          0 3 0
                          0 4 1
                          0 4 0
                          0 4 0
                          0 4 0
                          0 4 0
                          1 1 1
                          1 1 0
                          1 1 0
                          1 1 0
                          1 1 0
                          1 2 1
                          1 2 0
                          1 2 0
                          end
                          ------------------ copy up to and including the previous line ------------------

                          Listed 100 out of 52629 observations
                          Use the count() option to list more
                          Last edited by Jean Jacques; 07 Nov 2023, 11:23.

                          Comment


                          • #14
                            As I said, give an adequate data example. I will add on to this for today. Here, I can use the -text()- option to achieve what you ask for in #1, but I am not sure whether your question remains the same over the subsequent posts.

                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input byte seminar_series_mod_num int video_name_num byte first_obs
                            0 2 1
                            0 2 0
                            0 2 0
                            0 3 1
                            0 3 0
                            0 3 0
                            0 3 0
                            0 3 0
                            0 3 0
                            0 3 0
                            0 3 0
                            0 4 1
                            0 4 0
                            0 4 0
                            0 4 0
                            0 4 0
                            1 1 1
                            1 1 0
                            1 1 0
                            1 1 0
                            1 1 0
                            1 2 1
                            1 2 0
                            1 2 0
                            2 3 0
                            2 4 1
                            2 4 0
                            2 5 0
                            2 80
                            3 1 0
                            3 2 1
                            3 3 0
                            3 4 0
                            3 5 0
                            4 1 1
                            4 1 0
                            4 2 0
                            end
                            
                            set scheme s1mono
                            contract seminar_series_mod_num video_name_num 
                            contract seminar_series_mod_num, freq(count)
                            sort count seminar_series
                            gen long overid=_n
                            gr bar count, over(overid, label(nolab)) text(-0.12 50  "50%") ytitle("")
                            Click image for larger version

Name:	Graph.png
Views:	1
Size:	23.6 KB
ID:	1733090

                            Comment


                            • #15
                              Yea, that's exactly what I want. Perhaps I would like, instead of just the mark at 50%, a vertical line (I tried to do it using twoway and I didn't work). It can also be using the grid but as there's no horizontal axis I also couldn't.

                              Comment

                              Working...
                              X