Announcement

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

  • Graph bar over date

    Hello,

    I would like to plot a bar graph over time, and am facing some difficulties plotting.

    Here is part of the data I am using:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int total_case float date_1
     0 22068
     0 22068
     0 22069
     1 22068
     2 22072
     0 22070
     0 22069
     1 22068
     0 22070
     2 22071
     0 22069
     0 22069
    12 22098
     0 22068
     0 22068
     0 22068
     2 22073
     2 22074
     0 22068
    35 22106
     0 22068
    45 22069
     0 22069
     2 22077
     0 22068
     6 22093
     0 22068
     0 22068
     0 22068
    19 22094
     0 22070
     0 22068
     1 22068
     0 22069
     0 22068
     0 22068
    14 22087
     2 22093
     0 22069
     0 22076
     0 22068
     0 22069
     0 22068
    14 22087
     0 22068
     1 22071
     0 22068
     1 22069
     1 22069
     0 22068
     1 22069
     0 22068
     1 22068
     0 22068
    end
    format %td date_1
    If I use the codes:
    Code:
    graph bar total_case, over(date_1)
    I have too many ticks in the x-axis (date_1). I was wondering if there is a way to have only a few ticks and the dates are in format "July 04", "August 01" etc.

    Thank you very much for your response!

  • #2
    There are repeated observations for several daily dates. graph bar by default gives you the mean. Is that what you want?

    Comment


    • #3
      Oh yes, I'm sorry to not mention it.
      I'll first get the mean. Then, how should I change the date format?
      Thank you Nick!

      Comment


      • #4
        Code:
        graph bar
        is not well suited to your problem. It's predisposed to think that you have a categorical variable and that every distinct value should be labelled. For more than about a week, that is unlikely to be what you want.

        Here is some technique:

        Code:
        egen mean = mean(total_case), by(date_1)
        
        su date, meanonly 
        local min = mofd(r(min))
        local max = mofd(r(max) + 1)
        
        local labels 
        forval t = `min'/`max' { 
            local tick = dofm(`t')
            local show : di %tddd_Mon `tick'
            local labels `labels' `tick' "`show'" 
        }
        
        twoway bar mean date , xla(`labels') xtitle("") ytitle(Mean cases) yla(, ang(h))

        Comment


        • #5
          Hi Nick,

          Thank you so much!! it works well.
          I think your method is very straightforward. Thank you again

          Comment


          • #6
            More discussion has been published (today) at https://journals.sagepub.com/doi/pdf...6867X211000032

            Comment


            • #7
              Thank you Nick! I will read the article. Thank you for sharing.

              Comment

              Working...
              X