1

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

shad
  • 13
  • 3
  • Welcome to StackOverflow. In order to ask a better question please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rui Barradas Nov 23 '18 at 14:22
  • 3
    Type `unique(Data$sex)` into the console. I'll bet you'll see 3 unique values (possible `NA` among them?). That's why `scale_fill_manual()` and `scale_color_manual()` are expecting 3 values. – jdobres Nov 23 '18 at 14:25
  • Try defining your data in a data frame `df <- data.frame(x1 = 1, y1 = 156, x2 = 1, y2 = 156)` then using it in `geom_segment` like this: `SP + geom_segment(aes(x = x1, xend = x2, y = y1, yend = y2,colour ="black"), data = df)` – Dan Nov 23 '18 at 14:29
  • @ Lyngbakr, thanks so much, I run your code but I got this error " sex objective did not find" – shad Nov 23 '18 at 14:33
  • As @RuiBarradas mentioned, we need a reproducible example of the problem (i.e., add data to you question) otherwise we're just guessing. @jdrobres makes an excellent point, though. Also, does your code run okay when you omit the `geom_segment` line? – Dan Nov 23 '18 at 14:38
  • @ Lyngbakr , yes, I donot have any problem running the SP part and I added a picture of the plot (SP). How can I add unique(Data$sex) into the code? – shad Nov 23 '18 at 14:40
  • @jdobres, thanks for the comment, How I can add unique functin into this code. You mean I need to run SP+unique(Data$sex) ? – shad Nov 23 '18 at 14:42
  • @shad You don't add it to the plot, just run it in the console to see the unique values of `sex` in your data frame `Data`. Did you copy and paste the code I provided? – Dan Nov 23 '18 at 14:43
  • @ Lyngbakr , when I run unique(Data$sex) the result is "Female" " Male" – shad Nov 23 '18 at 14:45
  • @shad That's good – it means that we can rule out @jdrobres' guess. But until you provide some data, even just a small subset (ideally using `dput`) so we can run the code ourselves and produce the error, it'll be tricky for us to help. – Dan Nov 23 '18 at 14:49
  • @shad Shot in the dark: try moving `aes(x=Age, y= Alcohol,color=sex,fill=sex)` from `ggplot` to `geom_point` and then use the code I provided. – Dan Nov 23 '18 at 14:53
  • @ Lyngbakr, I just added some data into the question – shad Nov 23 '18 at 15:06

1 Answers1

1

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.

Dan
  • 11,370
  • 4
  • 43
  • 68
  • great, thanks so much, the only problem is why "scale_fill_manual" changes to gray color and is not in blue and pink anymore? – shad Nov 23 '18 at 15:29
  • @shad I missed `fill` out of `geom_ribbon`. Try with the updated code. – Dan Nov 23 '18 at 15:36