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, , however the format of my data is wide. Is there an easy way where I can plot the birthrate like in the image?
CodePudding user response:
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.