Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I plot this data as a line graph?

I have a wide dataframe that looks like this:

> dput(Birthrate)
structure(list(`Country Name` = "United States", `Country Code` = "USA", 
    `Indicator Name` = "Fertility rate, total (births per woman)", 
    `Indicator Code` = "SP.DYN.TFRT.IN", `1960` = 3.654, `1961` = 3.62, 
    `1962` = 3.461, `1963` = 3.319, `1964` = 3.19, `1965` = 2.913, 
    `1966` = 2.721, `1967` = 2.558, `1968` = 2.464, `1969` = 2.456, 
    `1970` = 2.48, `1971` = 2.266, `1972` = 2.01, `1973` = 1.879, 
    `1974` = 1.835, `1975` = 1.774, `1976` = 1.738, `1977` = 1.79, 
    `1978` = 1.76, `1979` = 1.808, `1980` = 1.8395, `1981` = 1.812, 

I want to plot birth rates (y-axis) by year like this, like this
, however the format of my data is wide. Is there an easy way where I can plot the birthrate like in the image?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This works:

br <- unlist(BirthRate[-(1:4)])
plot(as.numeric(names(br)), br)

… but it would probably be worth going back upstream a little bit and figuring out how to convert your data to long format … the tidyverse dplyr::pivot_longer() function would probably be what you need.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading