如何在 R 中绘制特定数据集?

How graph specific data set in R?

我有一个数据框如下:

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))

我需要根据年龄段绘制它们。最终图表应该类似于随后的图表:

实现此目的的最简单方法是将数据重塑为长格式:

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")

reprex package (v2.0.1)

创建于 2022-02-02

数据取自问题

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))