Announcement

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

  • Line graph

    Hi Stata users,

    I have 4 time periods ( round 1, round 2, round 3, and round 4) and a food insecurity score (Yes or No). I wanted to make a line graph for the food insecurity score using

    graph twoway line Food_secure Chronic_food_insecruity round

    But it does not give the proper line graph with time on the x-axis and food insecurity scores on the Y-axis. What could be my mistake?

  • #2
    That command means that you are plotting two variables against round. That is not what you describe otherwise. What's wrong with the graph?

    Comment


    • #3
      Thank you, Nick. The command can of course be edited as

      graph twoway line Food_secure round.

      However, even with this command, the graph is messed up with no pattern.

      Comment


      • #4
        Either you need a sort option or you have panel or longitudinal data and need something different. Without a graph or data example we’re guessing here.

        Comment


        • #5
          Yes, Nick, I do have panel data of 3 rounds and 4 countries. Below is an example bar graph I produced. I either want to replace it with a line graph or put a trend line on the bar graphs.
          Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	46.7 KB
ID:	1612008

          Comment


          • #6
            You are not giving us an example of your dataset. That makes it much harder for us to help you. I will assume that your data looks something like the example below. If that is not the case then you and I wasted a lot of time unnecessarily. Even if I guessed right, I still had to unnecessarily spent time to create that example data. So in future use dataex to give us an example for us to work with. How to do that is described in the statalist FAQ (black bar near the top of this page).


            Code:
            // create example data
            clear
            input byte(country round pid secure mod mild chron)
            1 3  1 1 0 0 0
            1 3  2 0 0 1 0
            1 3  3 0 0 1 0
            1 3  4 0 1 0 0
            1 3  5 0 0 1 0
            1 3  6 0 0 1 0
            1 3  7 0 0 1 0
            1 3  8 0 1 0 0
            1 3  9 1 0 0 0
            1 3 10 0 1 0 0
            1 3 11 1 0 0 0
            1 3 12 0 0 1 0
            1 3 13 0 0 1 0
            1 3 14 1 0 0 0
            1 3 15 0 0 1 0
            1 3 16 0 0 1 0
            1 3 17 0 1 0 0
            1 3 18 0 0 1 0
            1 3 19 0 0 1 0
            1 3 20 1 0 0 0
            1 4  1 0 0 1 0
            1 4  2 0 0 1 0
            1 4  3 0 0 1 0
            1 4  4 0 0 1 0
            1 4  5 0 0 1 0
            1 4  6 0 0 1 0
            1 4  7 0 0 0 1
            1 4  8 1 0 0 0
            1 4  9 0 0 1 0
            1 4 10 0 1 0 0
            1 4 11 0 0 1 0
            1 4 12 1 0 0 0
            1 4 13 0 0 1 0
            1 4 14 1 0 0 0
            1 4 15 0 0 1 0
            1 4 16 0 1 0 0
            1 4 17 1 0 0 0
            1 4 18 0 1 0 0
            1 4 19 0 0 0 1
            1 4 20 0 1 0 0
            1 5  1 0 1 0 0
            1 5  2 0 0 1 0
            1 5  3 0 0 1 0
            1 5  4 0 1 0 0
            1 5  5 0 0 1 0
            1 5  6 0 0 0 1
            1 5  7 0 0 1 0
            1 5  8 0 0 1 0
            1 5  9 1 0 0 0
            1 5 10 0 0 1 0
            1 5 11 0 0 1 0
            1 5 12 0 0 1 0
            1 5 13 0 0 1 0
            1 5 14 0 0 1 0
            1 5 15 0 0 1 0
            1 5 16 0 1 0 0
            1 5 17 0 0 1 0
            1 5 18 0 1 0 0
            1 5 19 0 0 1 0
            1 5 20 0 0 0 1
            2 3  1 0 0 1 0
            2 3  2 0 0 1 0
            2 3  3 0 0 0 1
            2 3  4 0 1 0 0
            2 3  5 0 1 0 0
            2 3  6 1 0 0 0
            2 3  7 0 0 1 0
            2 3  8 1 0 0 0
            2 3  9 0 1 0 0
            2 3 10 0 0 1 0
            2 3 11 0 0 1 0
            2 3 12 0 1 0 0
            2 3 13 1 0 0 0
            2 3 14 0 1 0 0
            2 3 15 0 1 0 0
            2 3 16 0 0 0 1
            2 3 17 0 0 1 0
            2 3 18 0 0 1 0
            2 3 19 0 1 0 0
            2 3 20 0 1 0 0
            2 4  1 0 1 0 0
            2 4  2 0 1 0 0
            2 4  3 0 1 0 0
            2 4  4 0 0 1 0
            2 4  5 0 1 0 0
            2 4  6 0 1 0 0
            2 4  7 0 0 1 0
            2 4  8 0 0 1 0
            2 4  9 0 0 1 0
            2 4 10 0 0 1 0
            2 4 11 0 0 1 0
            2 4 12 0 0 0 1
            2 4 13 0 0 1 0
            2 4 14 1 0 0 0
            2 4 15 0 0 1 0
            2 4 16 0 0 0 1
            2 4 17 0 0 1 0
            2 4 18 0 0 0 1
            2 4 19 0 0 0 1
            2 4 20 0 1 0 0
            2 5  1 1 0 0 0
            2 5  2 0 0 1 0
            2 5  3 1 0 0 0
            2 5  4 0 0 1 0
            2 5  5 1 0 0 0
            2 5  6 0 1 0 0
            2 5  7 0 0 1 0
            2 5  8 1 0 0 0
            2 5  9 0 1 0 0
            2 5 10 1 0 0 0
            2 5 11 1 0 0 0
            2 5 12 1 0 0 0
            2 5 13 1 0 0 0
            2 5 14 1 0 0 0
            2 5 15 1 0 0 0
            2 5 16 1 0 0 0
            2 5 17 1 0 0 0
            2 5 18 1 0 0 0
            2 5 19 0 1 0 0
            2 5 20 1 0 0 0
            3 3  1 1 0 0 0
            3 3  2 1 0 0 0
            3 3  3 0 0 1 0
            3 3  4 0 1 0 0
            3 3  5 0 0 1 0
            3 3  6 1 0 0 0
            3 3  7 0 1 0 0
            3 3  8 1 0 0 0
            3 3  9 1 0 0 0
            3 3 10 0 0 1 0
            3 3 11 1 0 0 0
            3 3 12 1 0 0 0
            3 3 13 0 1 0 0
            3 3 14 0 0 1 0
            3 3 15 0 1 0 0
            3 3 16 0 1 0 0
            3 3 17 0 0 1 0
            3 3 18 1 0 0 0
            3 3 19 1 0 0 0
            3 3 20 0 0 1 0
            3 4  1 1 0 0 0
            3 4  2 0 1 0 0
            3 4  3 0 0 1 0
            3 4  4 0 0 1 0
            3 4  5 1 0 0 0
            3 4  6 1 0 0 0
            3 4  7 1 0 0 0
            3 4  8 1 0 0 0
            3 4  9 0 0 1 0
            3 4 10 1 0 0 0
            3 4 11 1 0 0 0
            3 4 12 0 0 1 0
            3 4 13 1 0 0 0
            3 4 14 1 0 0 0
            3 4 15 0 0 1 0
            3 4 16 1 0 0 0
            3 4 17 1 0 0 0
            3 4 18 0 1 0 0
            3 4 19 0 0 0 1
            3 4 20 0 1 0 0
            3 5  1 0 1 0 0
            3 5  2 1 0 0 0
            3 5  3 0 0 0 1
            3 5  4 0 0 1 0
            3 5  5 0 1 0 0
            3 5  6 0 0 1 0
            3 5  7 0 0 1 0
            3 5  8 0 1 0 0
            3 5  9 0 1 0 0
            3 5 10 0 0 0 1
            3 5 11 1 0 0 0
            3 5 12 0 1 0 0
            3 5 13 0 1 0 0
            3 5 14 0 0 0 1
            3 5 15 1 0 0 0
            3 5 16 1 0 0 0
            3 5 17 1 0 0 0
            3 5 18 0 0 0 1
            3 5 19 1 0 0 0
            3 5 20 0 0 0 1
            4 3  1 0 0 1 0
            4 3  2 0 0 1 0
            4 3  3 0 1 0 0
            4 3  4 0 0 1 0
            4 3  5 0 1 0 0
            4 3  6 1 0 0 0
            4 3  7 0 0 1 0
            4 3  8 1 0 0 0
            4 3  9 0 1 0 0
            4 3 10 1 0 0 0
            4 3 11 1 0 0 0
            4 3 12 1 0 0 0
            4 3 13 1 0 0 0
            4 3 14 1 0 0 0
            4 3 15 1 0 0 0
            4 3 16 0 0 1 0
            4 3 17 0 0 1 0
            4 3 18 0 1 0 0
            4 3 19 0 1 0 0
            4 3 20 1 0 0 0
            4 4  1 0 0 1 0
            4 4  2 0 1 0 0
            4 4  3 1 0 0 0
            4 4  4 1 0 0 0
            4 4  5 1 0 0 0
            4 4  6 0 0 1 0
            4 4  7 1 0 0 0
            4 4  8 0 1 0 0
            4 4  9 0 0 1 0
            4 4 10 1 0 0 0
            4 4 11 0 1 0 0
            4 4 12 1 0 0 0
            4 4 13 0 1 0 0
            4 4 14 0 1 0 0
            4 4 15 0 0 1 0
            4 4 16 0 1 0 0
            4 4 17 0 0 1 0
            4 4 18 1 0 0 0
            4 4 19 1 0 0 0
            4 4 20 0 1 0 0
            4 5  1 1 0 0 0
            4 5  2 0 1 0 0
            4 5  3 0 0 1 0
            4 5  4 0 0 1 0
            4 5  5 1 0 0 0
            4 5  6 1 0 0 0
            4 5  7 0 1 0 0
            4 5  8 1 0 0 0
            4 5  9 1 0 0 0
            4 5 10 1 0 0 0
            4 5 11 0 0 1 0
            4 5 12 1 0 0 0
            4 5 13 0 1 0 0
            4 5 14 0 1 0 0
            4 5 15 0 1 0 0
            4 5 16 0 0 1 0
            4 5 17 1 0 0 0
            4 5 18 0 1 0 0
            4 5 19 1 0 0 0
            4 5 20 1 0 0 0
            end
            label values country country
            label def country 1 "Ethopia", modify
            label def country 2 "Vietnam", modify
            label def country 3 "Peru", modify
            label def country 4 "India", modify
            
            label var secure "food secure"
            label var mod "moderate food insecure"
            label var mild "mild food insecure"
            label var chron "chronic food insecure"
            // finish creating example dataset
            
            // turn the data in a dataset of proportions
            collapse (mean) secure mod mild chron, by(country round)
            // optionally look at the result
            list , sepby(country)
            
            // turn proportions into percentages
            replace secure = secure * 100
            replace mod = mod * 100
            replace mild = mild * 100
            replace chron = chron * 100
            
            // fix variable labels
            label var secure "food secure"
            label var mod "moderate food insecure"
            label var mild "mild food insecure"
            label var chron "chronic food insecure"
            
            // create graph
            twoway connected secure mod mild chron round, ///
               by(country, note("") compact) xlab(3/5) ytitle("percent") ///
               xscale(range(2.9 5.1))
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	111.2 KB
ID:	1612038


            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              I riffed on @Maarten Buis's helpful fake data example using floatplot (SSC, intended but not submitted at the time of writing). Here's the kit and caboodle, apart from the code for floatplot.


              Code:
               // create example data
              clear
              input byte(country round pid secure mod mild chron)
              1 3  1 1 0 0 0
              1 3  2 0 0 1 0
              1 3  3 0 0 1 0
              1 3  4 0 1 0 0
              1 3  5 0 0 1 0
              1 3  6 0 0 1 0
              1 3  7 0 0 1 0
              1 3  8 0 1 0 0
              1 3  9 1 0 0 0
              1 3 10 0 1 0 0
              1 3 11 1 0 0 0
              1 3 12 0 0 1 0
              1 3 13 0 0 1 0
              1 3 14 1 0 0 0
              1 3 15 0 0 1 0
              1 3 16 0 0 1 0
              1 3 17 0 1 0 0
              1 3 18 0 0 1 0
              1 3 19 0 0 1 0
              1 3 20 1 0 0 0
              1 4  1 0 0 1 0
              1 4  2 0 0 1 0
              1 4  3 0 0 1 0
              1 4  4 0 0 1 0
              1 4  5 0 0 1 0
              1 4  6 0 0 1 0
              1 4  7 0 0 0 1
              1 4  8 1 0 0 0
              1 4  9 0 0 1 0
              1 4 10 0 1 0 0
              1 4 11 0 0 1 0
              1 4 12 1 0 0 0
              1 4 13 0 0 1 0
              1 4 14 1 0 0 0
              1 4 15 0 0 1 0
              1 4 16 0 1 0 0
              1 4 17 1 0 0 0
              1 4 18 0 1 0 0
              1 4 19 0 0 0 1
              1 4 20 0 1 0 0
              1 5  1 0 1 0 0
              1 5  2 0 0 1 0
              1 5  3 0 0 1 0
              1 5  4 0 1 0 0
              1 5  5 0 0 1 0
              1 5  6 0 0 0 1
              1 5  7 0 0 1 0
              1 5  8 0 0 1 0
              1 5  9 1 0 0 0
              1 5 10 0 0 1 0
              1 5 11 0 0 1 0
              1 5 12 0 0 1 0
              1 5 13 0 0 1 0
              1 5 14 0 0 1 0
              1 5 15 0 0 1 0
              1 5 16 0 1 0 0
              1 5 17 0 0 1 0
              1 5 18 0 1 0 0
              1 5 19 0 0 1 0
              1 5 20 0 0 0 1
              2 3  1 0 0 1 0
              2 3  2 0 0 1 0
              2 3  3 0 0 0 1
              2 3  4 0 1 0 0
              2 3  5 0 1 0 0
              2 3  6 1 0 0 0
              2 3  7 0 0 1 0
              2 3  8 1 0 0 0
              2 3  9 0 1 0 0
              2 3 10 0 0 1 0
              2 3 11 0 0 1 0
              2 3 12 0 1 0 0
              2 3 13 1 0 0 0
              2 3 14 0 1 0 0
              2 3 15 0 1 0 0
              2 3 16 0 0 0 1
              2 3 17 0 0 1 0
              2 3 18 0 0 1 0
              2 3 19 0 1 0 0
              2 3 20 0 1 0 0
              2 4  1 0 1 0 0
              2 4  2 0 1 0 0
              2 4  3 0 1 0 0
              2 4  4 0 0 1 0
              2 4  5 0 1 0 0
              2 4  6 0 1 0 0
              2 4  7 0 0 1 0
              2 4  8 0 0 1 0
              2 4  9 0 0 1 0
              2 4 10 0 0 1 0
              2 4 11 0 0 1 0
              2 4 12 0 0 0 1
              2 4 13 0 0 1 0
              2 4 14 1 0 0 0
              2 4 15 0 0 1 0
              2 4 16 0 0 0 1
              2 4 17 0 0 1 0
              2 4 18 0 0 0 1
              2 4 19 0 0 0 1
              2 4 20 0 1 0 0
              2 5  1 1 0 0 0
              2 5  2 0 0 1 0
              2 5  3 1 0 0 0
              2 5  4 0 0 1 0
              2 5  5 1 0 0 0
              2 5  6 0 1 0 0
              2 5  7 0 0 1 0
              2 5  8 1 0 0 0
              2 5  9 0 1 0 0
              2 5 10 1 0 0 0
              2 5 11 1 0 0 0
              2 5 12 1 0 0 0
              2 5 13 1 0 0 0
              2 5 14 1 0 0 0
              2 5 15 1 0 0 0
              2 5 16 1 0 0 0
              2 5 17 1 0 0 0
              2 5 18 1 0 0 0
              2 5 19 0 1 0 0
              2 5 20 1 0 0 0
              3 3  1 1 0 0 0
              3 3  2 1 0 0 0
              3 3  3 0 0 1 0
              3 3  4 0 1 0 0
              3 3  5 0 0 1 0
              3 3  6 1 0 0 0
              3 3  7 0 1 0 0
              3 3  8 1 0 0 0
              3 3  9 1 0 0 0
              3 3 10 0 0 1 0
              3 3 11 1 0 0 0
              3 3 12 1 0 0 0
              3 3 13 0 1 0 0
              3 3 14 0 0 1 0
              3 3 15 0 1 0 0
              3 3 16 0 1 0 0
              3 3 17 0 0 1 0
              3 3 18 1 0 0 0
              3 3 19 1 0 0 0
              3 3 20 0 0 1 0
              3 4  1 1 0 0 0
              3 4  2 0 1 0 0
              3 4  3 0 0 1 0
              3 4  4 0 0 1 0
              3 4  5 1 0 0 0
              3 4  6 1 0 0 0
              3 4  7 1 0 0 0
              3 4  8 1 0 0 0
              3 4  9 0 0 1 0
              3 4 10 1 0 0 0
              3 4 11 1 0 0 0
              3 4 12 0 0 1 0
              3 4 13 1 0 0 0
              3 4 14 1 0 0 0
              3 4 15 0 0 1 0
              3 4 16 1 0 0 0
              3 4 17 1 0 0 0
              3 4 18 0 1 0 0
              3 4 19 0 0 0 1
              3 4 20 0 1 0 0
              3 5  1 0 1 0 0
              3 5  2 1 0 0 0
              3 5  3 0 0 0 1
              3 5  4 0 0 1 0
              3 5  5 0 1 0 0
              3 5  6 0 0 1 0
              3 5  7 0 0 1 0
              3 5  8 0 1 0 0
              3 5  9 0 1 0 0
              3 5 10 0 0 0 1
              3 5 11 1 0 0 0
              3 5 12 0 1 0 0
              3 5 13 0 1 0 0
              3 5 14 0 0 0 1
              3 5 15 1 0 0 0
              3 5 16 1 0 0 0
              3 5 17 1 0 0 0
              3 5 18 0 0 0 1
              3 5 19 1 0 0 0
              3 5 20 0 0 0 1
              4 3  1 0 0 1 0
              4 3  2 0 0 1 0
              4 3  3 0 1 0 0
              4 3  4 0 0 1 0
              4 3  5 0 1 0 0
              4 3  6 1 0 0 0
              4 3  7 0 0 1 0
              4 3  8 1 0 0 0
              4 3  9 0 1 0 0
              4 3 10 1 0 0 0
              4 3 11 1 0 0 0
              4 3 12 1 0 0 0
              4 3 13 1 0 0 0
              4 3 14 1 0 0 0
              4 3 15 1 0 0 0
              4 3 16 0 0 1 0
              4 3 17 0 0 1 0
              4 3 18 0 1 0 0
              4 3 19 0 1 0 0
              4 3 20 1 0 0 0
              4 4  1 0 0 1 0
              4 4  2 0 1 0 0
              4 4  3 1 0 0 0
              4 4  4 1 0 0 0
              4 4  5 1 0 0 0
              4 4  6 0 0 1 0
              4 4  7 1 0 0 0
              4 4  8 0 1 0 0
              4 4  9 0 0 1 0
              4 4 10 1 0 0 0
              4 4 11 0 1 0 0
              4 4 12 1 0 0 0
              4 4 13 0 1 0 0
              4 4 14 0 1 0 0
              4 4 15 0 0 1 0
              4 4 16 0 1 0 0
              4 4 17 0 0 1 0
              4 4 18 1 0 0 0
              4 4 19 1 0 0 0
              4 4 20 0 1 0 0
              4 5  1 1 0 0 0
              4 5  2 0 1 0 0
              4 5  3 0 0 1 0
              4 5  4 0 0 1 0
              4 5  5 1 0 0 0
              4 5  6 1 0 0 0
              4 5  7 0 1 0 0
              4 5  8 1 0 0 0
              4 5  9 1 0 0 0
              4 5 10 1 0 0 0
              4 5 11 0 0 1 0
              4 5 12 1 0 0 0
              4 5 13 0 1 0 0
              4 5 14 0 1 0 0
              4 5 15 0 1 0 0
              4 5 16 0 0 1 0
              4 5 17 1 0 0 0
              4 5 18 0 1 0 0
              4 5 19 1 0 0 0
              4 5 20 1 0 0 0
              end
              label values country country
              label def country 1 "Ethopia", modify
              label def country 2 "Vietnam", modify
              label def country 3 "Peru", modify
              label def country 4 "India", modify
              
              gen score = secure + 2 * mild + 3 * mod + 4 * chron 
              label def score 1 none 2 mild 3 moderate 4 chronic 
              label val score score  
              label var score "food insecurity"
              
              set scheme s1color 
              floatplot score, over(round) by(country, note("percents")) fcolors(blue*0.4 red*0.2 red*0.6 red) lcolors(blue red red red) highnegative(2) name(secure1, replace)
              
              floatplot score, over(round) by(country, compact row(1) note("percents", pos(11)) legend(pos(3) col(1))) fcolors(blue*0.4 red*0.2 red*0.6 red) lcolors(blue red red red) highnegative(2) vertical  legend(symxsize(*.2) size(small)) offset(0.4) name(secure2, replace)

              Click image for larger version

Name:	secure1.png
Views:	1
Size:	26.8 KB
ID:	1612045
              Click image for larger version

Name:	secure2.png
Views:	1
Size:	29.4 KB
ID:	1612046


              Comment


              • #8
                This design will make stacked bar charts very popular. Nick Cox, I would create a new thread once it is available from SSC so that it is easier to find and reference.

                Comment


                • #9
                  Andrew Musau Indeed, I intend to announce the command in due course in a devoted thread.

                  Comment


                  • #10
                    Have been disconnected last week. Thank you very much, Nick, Andrew and Maarteen. I learnt a lot from this tread.

                    Comment

                    Working...
                    X