I have a data frame as follows:
| age-group | a | b |
|---|---|---|
| 15-19 | 0.113152976 | 0.107349538 |
| 20-24 | 0.302890507 | 0.365912750 |
| 25-29 | 0.274024612 | 0.244849695 |
| 30-34 | 0.175444951 | 0.153582407 |
| 35-39 | 0.096576950 | 0.094900555 |
| 40-44 | 0.033532932 | 0.030508273 |
| 45-49 | 0.004377071 | 0.002896782 |
I need to plot them based on age groups. The final graph should be something like the ensuing graph:
CodePudding user response:
The easiest way to achieve this is to reshape your data into long format:
library(ggplot2)
library(tidyr)
ggplot(pivot_longer(SVD, cols = c("a", "b")),
aes(age.group, value, group = name, linetype = name))
geom_line()
labs(x = "Initial Age for Each Age Group",
y = "Proportional Share of Fertility",
linetype = "")
theme_classic()
theme(legend.position = "bottom")

Created on 2022-02-02 by the reprex package (v2.0.1)
Data taken from question
SVD <- structure(list(age.group = c("15-19", "20-24", "25-29", "30-34",
"35-39", "40-44", "45-49"), a = c(0.113152976, 0.302890507, 0.274024612,
0.175444951, 0.09657695, 0.033532932, 0.004377071), b = c(0.107349538,
0.36591275, 0.244849695, 0.153582407, 0.094900555, 0.030508273,
0.002896782)), class = "data.frame", row.names = c(NA, -7L))

