Search code examples
rggplot2segment

Error when using geom_segment to add a line into a ggplot


I want to add a line into this plot by using geom_segment but when I run the following code I have this error" Insufficient values in manual scale. 3 needed but only 2 provided". I was wondering how I can solve this issue.

Age      Alcohol   sex    X0.025  X0.975   L_LCI  L_UCI  U_LCI  U_UCI
1.000    211     Female   191.26  416.98 176.530 200.98 360.19 483.78
1.025    281     Female   300.20 306.03 166.300 310.10 300.28 381.77
1.103    195     Female   168.20 706.03 150.300 200.10 330.28 790.77
1.144    350     Female   148.20 506.03 100.300 150.10 390.28 510.77
1.156    256     Female   396.20 416.03 300.300 400.10 390.28 481.77 
2.922    336     Male     396.20 416.03 300.300 400.10 390.28 481.77
3        110     Male     191.26  416.98 176.530 200.98 360.19 483.78
1        410     Male     396.20 416.03 300.300 400.10 390.28 481.77 
1.001    501     Male    155.90 350.93 143.210 168.59 330.27 371.60
5        701     Male    155.76 349.35 143.210 168.30 329.95 368.76
1.289    165     Male    184.34 388.74 160.910 199.76 331.66 445.82

SP=ggplot(Data, aes(x=Age, y= Alcohol,color=sex,fill=sex))+
  geom_point()+ 
  scale_shape_manual(values=c(1,1), name='sex', labels=c('Female','Male'))+ #0.25
  geom_line(aes(x = Age, y = X0.025),size=1) +
  geom_line(aes(x = Age, y = X0.975),size=1) +
  geom_ribbon(aes(x = Age, ymin = L_LCI,ymax=L_UCI),alpha = 0.2,linetype=0) +
  geom_ribbon(aes(x = Age, ymin = U_LCI,ymax= U_UCI),alpha =0.2,linetype=0) +

  labs(x = 'Age (Years)', y = '')+
  scale_x_continuous(breaks = round(seq(1, 19, by = 1),0)) +
  scale_y_continuous(expand = c(0, 0),breaks = round(seq(0, 600, by = 100),0))+
  scale_color_manual(breaks = c("Female", "Male"),
                     values=c("#EC6696", "#558ED5"))+ 

 scale_fill_manual(breaks = c("Female", "Male"),
                     values=c("#EC6696", "#558ED5"))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.title.x=element_text(size=15,face="bold"),  axis.title.y=element_text(size=15,face="bold"),axis.text.x=element_text(size=13),
        axis.text.y=element_text(size=13),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),plot.title = element_text(vjust=-15,hjust=0.02,
color="black", size=20,face="bold"))+

       ggtitle("Tile")+ coord_cartesian(xlim = c(1,19), ylim = c(0,700))+
       theme(legend.text=element_text(size=12, face = "bold"),legend.key.size = unit(1.5, 'lines'), legend.spacing.x = unit(0.1, 'cm'),legend.justification = c(0, 1.5),legend.position = c(0, 1),
legend.box.margin=margin(c(10,10,10,10)))+theme(legend.title = element_blank())+
guides(color = guide_legend(override.aes = list(size = 1.3)))

SP+ geom_segment(aes(x = 1, y = 156, xend = 1, yend = 156,colour ="black"))

enter image description here


Solution

  • Try this:

    SP <- ggplot(Data)+
      geom_point(aes(x=Age, y= Alcohol, color=sex, fill=sex))+ 
      scale_shape_manual(values=c(1,1), name='sex', labels=c('Female','Male'))+ #0.25
      geom_line(aes(x = Age, y = X0.025, color=sex),size=1) +
      geom_line(aes(x = Age, y = X0.975, color=sex),size=1) +
      geom_ribbon(aes(x = Age, ymin = L_LCI, ymax=L_UCI, color=sex,fill=sex),alpha = 0.2,linetype=0) +
      geom_ribbon(aes(x = Age, ymin = U_LCI, ymax= U_UCI, color=sex, fill=sex),alpha =0.2,linetype=0) +
      labs(x = 'Age (Years)', y = '')+
      scale_x_continuous(breaks = round(seq(1, 19, by = 1),0)) +
      scale_y_continuous(expand = c(0, 0),breaks = round(seq(0, 600, by = 100),0))+
      scale_color_manual(breaks = c("Female", "Male"),
                         values=c("#EC6696", "#558ED5"))+ 
      scale_fill_manual(breaks = c("Female", "Male"),
                        values=c("#EC6696", "#558ED5"))+
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            axis.title.x=element_text(size=15,face="bold"),  axis.title.y=element_text(size=15,face="bold"),axis.text.x=element_text(size=13),
            axis.text.y=element_text(size=13),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),plot.title = element_text(vjust=-15,hjust=0.02, color="black", size=20,face="bold"))+
      ggtitle("Tile")+ coord_cartesian(xlim = c(1,19), ylim = c(0,700))+
      theme(legend.text=element_text(size=12, face = "bold"),legend.key.size = unit(1.5, 'lines'), legend.spacing.x = unit(0.1, 'cm'),legend.justification = c(0, 1.5),legend.position = c(0, 1),
            legend.box.margin=margin(c(10,10,10,10)))+theme(legend.title = element_blank())+
      guides(color = guide_legend(override.aes = list(size = 1.3)))
    
    df <- data.frame(x1 = 1, y1 = 156, x2 = 1, y2 = 156)
    SP + geom_segment(aes(x = x1, xend = x2, y = y1, yend = y2), colour ="black", data = df)
    

    Note that the segment won't be apparent, as it starts and ends at the same point according to your coordinates.