Announcement

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

  • Line graph based on frequency

    Hello Stata users,

    I would be grateful for any assistance.

    I have data like this:

    Code:
     
    shock shock_year
    Drought 2023
    Drought 2023
    Drought 2022
    Too much rain 2023
    Too much rain 2023
    Too much rain 2023
    Too much rain 2019
    Too much rain 2019
    Frosts 2020
    Floods 2022
    Floods 2023
    Very cold winter 2019
    Very cold winter 2022
    Frosts 2019
    Frosts 2019
    Frosts 2019
    Frosts 2020
    Frosts 2020
    Very cold winter 2020
    Very cold winter 2021
    Very cold winter 2023
    Floods 2020
    Floods 2020


    How can I draw a graph using this data on Stata that looks like this:
    Click image for larger version

Name:	image_33303.png
Views:	1
Size:	14.5 KB
ID:	1737239



    Your guidance on this is highly appreciated.

    Best regards,
    Dastan
    Last edited by Dastan Aseinov; 15 Dec 2023, 03:44. Reason: duplication of attachment

  • #2
    The Y (vertical) axis indicates the number of observations

    Comment


    • #3
      Code:
      clear
      input str16 shock    int shock_year
      "Drought"    2023
      "Drought"    2023
      "Drought"    2022
      "Too much rain"    2023
      "Too much rain"    2023
      "Too much rain"    2023
      "Too much rain"    2019
      "Too much rain"    2019
      "Frosts"    2020
      "Floods"    2022
      "Floods"    2023
      "Very cold winter"    2019
      "Very cold winter"    2022
      "Frosts"    2019
      "Frosts"    2019
      "Frosts"    2019
      "Frosts"    2020
      "Frosts"    2020
      "Very cold winter"    2020
      "Very cold winter"    2021
      "Very cold winter"    2023
      "Floods"    2020
      "Floods"    2020
      end
      
      contract shock shock_year
      
      fillin shock shock_year
      replace _freq = 0 if _fillin
      
      separate _freq, by(shock) veryshortlabel
      twoway connected _freq? shock_year
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Many thanks Maarten, I hope it help me. I will try the code, Best Regards, Dastan

        Comment


        • #5
          Here is a twist on @Maarten Buis's nice solution. I use tabplot and myaxis from the Stata Journal. Here

          tabplot is used to give a two-way bar chart. Notice how we can dispense with an awkward legend because we can use axis labels directly.

          For some other examples see https://www.statalist.org/forums/for...updated-on-ssc

          myaxis is used to get an ordering -- anything interesting or useful is better than a dopey default of alphabetical order.

          For more see https://www.statalist.org/forums/for...e-or-graph-use

          As said, there is a fuller write-up in each case in the Stata Journal.

          Code:
          clear
          input str16 shock    int shock_year
          "Drought"    2023
          "Drought"    2023
          "Drought"    2022
          "Too much rain"    2023
          "Too much rain"    2023
          "Too much rain"    2023
          "Too much rain"    2019
          "Too much rain"    2019
          "Frosts"    2020
          "Floods"    2022
          "Floods"    2023
          "Very cold winter"    2019
          "Very cold winter"    2022
          "Frosts"    2019
          "Frosts"    2019
          "Frosts"    2019
          "Frosts"    2020
          "Frosts"    2020
          "Very cold winter"    2020
          "Very cold winter"    2021
          "Very cold winter"    2023
          "Floods"    2020
          "Floods"    2020
          end
          
          contract shock shock_year
          
          myaxis Shock=shock, sort(total _freq) descending 
          
          tabplot Shock shock_year [fw=_freq], showval barw(0.9) separate(Shock) xtitle("") ytitle("") subtitle("")
          Click image for larger version

Name:	hazards.png
Views:	1
Size:	37.7 KB
ID:	1737334

          Comment


          • #6
            Hello Nick Cox! Thank you very much. Both your solution and Marten's solution are what I was looking for.

            Comment

            Working...
            X