Yellow_truffle
Yellow_truffle

Reputation: 923

plotting two line plot in one graph for grouped data with different label using ggplot

I have a data frame which is a result of grouping using groupby as shown below (this is just a sample of my data):

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

What I want to do is to show ave_prem_annual for each Inssurance Duration Group as a function of Issue_Year both for Surrended and Issuance group in the same plot (one with line, the other one with dashed line). I've done this only for Surrended group as shown below:

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

The result is shown below:

result of ggplot

This result is good; however, I want to show the same type of graphs for Issuance group (with dashed line) as well on the same plot (with the dashed line but the same colour for each Insured duration group, so forexample group 6 should be purple in both graph but one with simple line and the other with dashed line). How can I do this?

Upvotes: 1

Views: 56

Answers (1)

LC-datascientist
LC-datascientist

Reputation: 2096

See the code below. Here are the changes that I made to your code:

  • Work with your full data temp instead of the subset temp1
  • Remove group=Insurance_Duration_Group from ggplot(aes(...))
  • Add lty=Policy_Status to geom_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) )

Insurance groups plot with Surrended and Issuance

Upvotes: 2

Related Questions