首页 > 解决方案 > 使用 ggplot 在一个图中为具有不同标签的分组数据绘制两条线图

问题描述

我有一个数据框,它是使用groupby如下所示的分组结果(这只是我的数据示例):

structure(list(Issue_Year = c(1387, 1387, 1387, 1387, 1387, 1387, 
1387, 1387, 1387, 1388, 1388, 1388, 1388, 1388, 1388, 1388, 1388, 
1388, 1388, 1388, 1388, 1388, 1388), Insurance_Duration_Group = c(1, 
2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 
7), Policy_Status = c("Surrended", "Issuance", "Surrended", "Issuance", 
"Surrended", "Issuance", "Surrended", "Issuance", "Surrended", 
"Issuance", "Surrended", "Issuance", "Surrended", "Issuance", 
"Surrended", "Issuance", "Surrended", "Issuance", "Surrended", 
"Issuance", "Surrended", "Issuance", "Surrended"), ave_prem_annual = c(3241037.19885714, 
1700934.5795, 3150363.055, 2498964.24354545, 2618196.0915625, 
3121667.17790909, 5119958.578, 2506542.056, 2803738.318, 3691789.5957381, 
5171018.22543771, 3739251.27351327, 4941986.76581609, 3092901.61504865, 
3484270.7802, 2768585.95108475, 2790833.45352381, 2872372.61865854, 
3442560.37137931, 3395992.09203125, 5354771.74675, 2682242.9905, 
5283489.09633333)), row.names = c(NA, -23L), groups = structure(list(
    Issue_Year = c(1387, 1387, 1387, 1387, 1387, 1388, 1388, 
    1388, 1388, 1388, 1388, 1388), Insurance_Duration_Group = c(1, 
    2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7), .rows = structure(list(
        1L, 2:3, 4:5, 6:7, 8:9, 10:11, 12:13, 14:15, 16:17, 18:19, 
        20:21, 22:23), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

  Issue_Year Insurance_Duration_Group Policy_Status ave_prem_annual
        <dbl>                    <dbl> <chr>                   <dbl>
 1       1387                        1 Surrended            3241037.
 2       1387                        2 Issuance             1700935.
 3       1387                        2 Surrended            3150363.
 4       1387                        3 Issuance             2498964.
 5       ....                      ............              .....

我想要做的是显示每个保险持续时间组的ave_prem_annual作为 Issue_Year 的函数,用于同一图中的已放弃发行 组(一个用线,另一个用虚线)。我只为Surrended组执行此操作,如下所示:

temp1 <- subset(temp, temp$Policy_Status=="Surrended")
ggplot(temp1, aes(x=Issue_Year, y=ave_prem_annual, group=Insurance_Duration_Group)) +
  geom_line(aes(color=as.factor(Insurance_Duration_Group)))+
  geom_point(aes(color=as.factor(Insurance_Duration_Group)))+
  scale_color_discrete(name = "Insurance_Duration_Group")+
  scale_x_continuous(breaks = temp$Issue_Year,labels = temp$Issue_Year)+
  theme(legend.position="right")+
  ggtitle("")+
  theme(plot.title = element_text(hjust = 0.5, size=10) )

结果如下所示:

ggplot的结果

这个结果很好;但是,我想在同一个图上显示相同类型的图表带有虚线)以及同一图表(带有虚线,但每个被保险期限组的颜色相同,因此例如第 6 组在两个图表中都应该是紫色但一个用简单的线,另一个用虚线)。我怎样才能做到这一点?

标签: rggplot2

解决方案


请参阅下面的代码。以下是我对您的代码所做的更改:

  • 使用完整数据temp而不是子集temp1
  • group=Insurance_Duration_Group从中删除ggplot(aes(...))
  • 添加lty=Policy_Statusgeom_line(aes(...))
ggplot(temp, aes(x=Issue_Year, y=ave_prem_annual)) +
    geom_line(aes(color=as.factor(Insurance_Duration_Group), lty=Policy_Status))+
    geom_point(aes(color=as.factor(Insurance_Duration_Group)))+
    scale_color_discrete(name = "Insurance_Duration_Group")+
    scale_x_continuous(breaks = temp$Issue_Year,labels = temp$Issue_Year)+
    theme(legend.position="right")+
    ggtitle("")+
    theme(plot.title = element_text(hjust = 0.5, size=10) )

保险集团与 Surrended 和 Issuance 密谋


推荐阅读