1

I am trying to plot several things in a chart. Points colored by ID, the regression line, a modified regression line and an area where I do not want values to fall in.
I would like to have a legend with the names of the two lines. e.g. blue=Fitted model, red=Worst case scenario and the area, red= Suspect values.

This is the code that I used to create the graph:

ggplot(data, aes(x=log_dilution, y=ct)) +
  geom_point(aes(color=ID),show.legend = FALSE) +
  geom_smooth(aes(linetype ='Fitted model +95%CI'), method = 'lm',show.legend = TRUE) +  
  geom_segment(aes(x = 0, xend = 1.7, y = model1$coefficients[1], yend 
      =model1$coefficients[1] + (model1$coefficients[2]+(1.96*1.030535))*1.7),
      color='red', lwd=1, lty=2,show.legend = FALSE) + 
  theme(axis.text.x= element_text(size=14), axis.title= element_text(size=16), axis.text.y = element_text(size=14)) +
  ylim(15, 40) + 
  annotate('rect',xmin=0, xmax=1.7, ymin=35, ymax=40, alpha=0.2, fill="red") + 
  scale_size_manual( values = c(1.5, 1.5), labels = c("Fitted model +95%CI", "Worst case")) + 
  guides(color=FALSE)+ylab("Ct")+theme(legend.position = "bottom")

and this is the result so far.

enter image description here

Can anybody give me some directions on how I can plot a legend for the lines and the area (i am not interested in plotting the points)?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Carles V.
  • 11
  • 1
  • This might help https://stackoverflow.com/questions/49373461/removing-the-border-of-legend-symbol/49377292#49377292 – Tung Jul 14 '18 at 06:21
  • Welcome to SO. please provide sample data when you ask for help. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tjebo Jul 14 '18 at 20:59
  • And reduce your code to the minimum. E.g., `theme` and `ylim` and `scale_size_manual` are not necessary for your question – tjebo Jul 14 '18 at 21:01
  • See [if this question and answer](https://stackoverflow.com/questions/36695984/add-a-legend-to-a-ggplot2-scatter-plot-including-additional-lines) helps get you started. You'd likely use `fill` and `linetype` instead of `color`. – aosmith Jul 16 '18 at 21:27

0 Answers0