Announcement

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

  • Reshaping a data set for a time-series line graph

    Dear Stata users, Hi;

    I hope I could get some help with this. I have this data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str34 entity int year float gdppercapita
    "Australia" 1820  826
    "Australia" 1821  834
    "Australia" 1822  858
    "Australia" 1823  886
    "Australia" 1824  936
    "Australia" 1825  982
    "Australia" 1826  999
    "Australia" 1827 1030
    "Australia" 1828 1057
    "Australia" 1829 1114
    "Australia" 1830 1352
    "Australia" 1831 1398
    "Australia" 1832 1428
    "Australia" 1833 1435
    "Australia" 1834 1508
    "Australia" 1835 1825
    "Australia" 1836 1793
    "Australia" 1837 1881
    "Australia" 1838 1857
    "Australia" 1839 1733
    "Australia" 1840 2190
    "Australia" 1841 1868
    "Australia" 1842 1704
    "Australia" 1843 1975
    "Australia" 1844 2246
    "Australia" 1845 2310
    "Australia" 1846 2554
    "Australia" 1847 2954
    "Australia" 1848 3381
    "Australia" 1849 3339
    "Australia" 1850 3148
    "Australia" 1851 3736
    "Australia" 1852 4382
    "Australia" 1853 4798
    "Australia" 1854 4202
    "Australia" 1855 3988
    "Australia" 1856 4866
    "Australia" 1857 4294
    "Australia" 1858 3661
    "Australia" 1859 4728
    "Australia" 1860 4613
    "Australia" 1861 4544
    "Australia" 1862 4368
    "Australia" 1863 4352
    "Australia" 1864 4616
    "Australia" 1865 4403
    "Australia" 1866 4532
    "Australia" 1867 4956
    "Australia" 1868 5031
    "Australia" 1869 4932
    "Australia" 1870 5217
    "Australia" 1871 5259
    "Australia" 1872 5663
    "Australia" 1873 6095
    "Australia" 1874 6113
    "Australia" 1875 6596
    "Australia" 1876 6387
    "Australia" 1877 6433
    "Australia" 1878 6817
    "Australia" 1879 6703
    "Australia" 1880 6830
    "Australia" 1881 7101
    "Australia" 1882 6476
    "Australia" 1883 7133
    "Australia" 1884 6861
    "Australia" 1885 7049
    "Australia" 1886 6900
    "Australia" 1887 7383
    "Australia" 1888 7179
    "Australia" 1889 7567
    "Australia" 1890 7106
    "Australia" 1891 7438
    "Australia" 1892 6368
    "Australia" 1893 5910
    "Australia" 1894 6003
    "Australia" 1895 5558
    "Australia" 1896 5874
    "Australia" 1897 5451
    "Australia" 1898 6202
    "Australia" 1899 6121
    "Australia" 1900 6397
    "Australia" 1901 6119
    "Australia" 1902 6094
    "Australia" 1903 6497
    "Australia" 1904 6846
    "Australia" 1905 6825
    "Australia" 1906 7184
    "Australia" 1907 7358
    "Australia" 1908 7481
    "Australia" 1909 7940
    "Australia" 1910 8305
    "Australia" 1911 8136
    "Australia" 1912 8126
    "Australia" 1913 8220
    "Australia" 1914 8013
    "Australia" 1915 7806
    "Australia" 1916 7775
    "Australia" 1917 7637
    "Australia" 1918 7336
    "Australia" 1919 7517
    end
    The first variable is called "entity", it is about the name of some countries and regions, the second variable is the year, the third variable is about the measure of the gdp per capita for each region/country and for each year.
    As you can see, my data has the format of an (i,j) space, in which is is the "entity" variable and j is the year variable. I would love to reshape this dataset in a way that I could draw a graph which shows the evolution of the gdp per capita for each region/country by time (year), I would love to have the "year" variable in the x axis.

    Any help please? I will be greatful.

  • #2
    egen cid = group(entity)

    xtset cid year

    xtline gdppercapita, ov


    also search lgraph and fabplot

    Comment


    • #3
      The most important detail here is the number of countries (and regions) you want to show. Whether it's 2, 5, 10, 20, ..., 200 has a great influence on what kinds of graph will be at all useful.

      But you don't need to reshape at all. This long layout you have is already the layout you need. george Ford's point is that if you wish to use xtline or any other graph command that requires an xtset, then you need to xtset first.

      Comment


      • #4
        Nick Cox What I want to have is a tsline which shows the evolution of the indicator "gdppercapita" for each "entity", the problem that the variable "entity is repeated for each "year", but I want to reshape the data set in a way to have the "gdppercapita" value for each "year" (x axis) and for each "entity" (y variable).

        Comment


        • #5
          If entity means country here, I think I've already answered the question. You don't need to reshape.

          If it means something else, then you've lost me (and perhaps others) and I need a data example that shows what is different from #1.

          You didn't answer my first question in #3.

          Comment


          • #6
            Nick Cox Pehaps I'am not explaining myself in a good way, sorry about that.

            Getting back to your question in #3, I don't need to show the "entity" as a number on the graph, but as the name of the entity itself.

            George Ford 's code seems to do the trick, and I thank him for the help, it is just with that code, I get the "entity" as a number, yet I want to have the names of the regions in the "entity" variable, not by numbers. I would be reatful if you could help me with that.

            Comment


            • #7
              search labmask

              labmask cid, values(entity)

              Comment

              Working...
              X