4

I have the data frame One_APD:

One_APD = structure(list(Amplification = c(108.91, 120.765, 134.875, 151.877, 172.812, 199.324), Voltage = c(351.955, 353.954, 355.956, 357.955, 359.947, 361.948), pred = c(1.54580502813059, 1.56713437847747, 1.58992216028315, 1.61410007849728, 1.63960908075698, 1.66665619275778)), .Names = c("Amplification", "Voltage", "pred"), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

And it looks like this:

> One_APD
  Amplification Voltage     pred
1       108.910 351.955 1.545805
2       120.765 353.954 1.567134
3       134.875 355.956 1.589922
4       151.877 357.955 1.614100
5       172.812 359.947 1.639609
6       199.324 361.948 1.666656

and plotted it as following:

ggplot(One_APD, aes(x = Voltage, y = log(log(Amplification)))) +
  geom_point(size=3, colour="blue") +
  geom_line(aes(y = pred), size=2, alpha=0.3, colour="red")

which looks like:

ggplot

I would like to add a legend which names the blue points and the red curve. As far as I see all legend commands aim at fill or colour in the aes of ggplot. But I don't provide fill or colour because it makes no sense to assign colour or fill.. How can I add corresponding legends to the data in the plot?

edit: Solution is in Construct a manual legend for a complicated plot

Ben
  • 1,432
  • 4
  • 20
  • 43
  • Please provide the data in a parsable way to [provide a minimal working example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You can do this using `dput`. – ziggystar Feb 19 '18 at 11:43
  • @ziggystar Thanks, I added it (is it correct this way?). – Ben Feb 19 '18 at 11:48
  • That's much better. Also you could have removed all columns that do not contribute to the problem, to remove the clutter. Some remark: I think `threshold` and `SN` should be `logical`, or at least `factor`. – ziggystar Feb 19 '18 at 11:51
  • I kept it because obviously, R keeps the data in its heads though they are removed as above: I removed the columns but they still appear.. not in the data frame but in the structure (?) – Ben Feb 19 '18 at 11:59
  • 1
    Hey, I edited the question for you. Now you have put the old code back in. :) – ziggystar Feb 19 '18 at 12:00
  • Now that we've spent that much work on the question... It's a duplicate, and you can find the answer there. – ziggystar Feb 19 '18 at 12:02
  • ..oh, ok. Will have a look at it. Thanks a lot! – Ben Feb 19 '18 at 12:03
  • I'm afraid the answer given there is not sufficient for me resp. not sufficient enough to move on. – Ben Feb 19 '18 at 12:10
  • 1
    you can solve your problem here is think https://stackoverflow.com/questions/17148679/construct-a-manual-legend-for-a-complicated-plot – Antonios Feb 19 '18 at 12:15
  • @Antonis Yes, this worked! Thank you very much! – Ben Feb 19 '18 at 12:26
  • @Antonis Can you please post it as an answer so I can accept it? – Ben Feb 21 '18 at 11:13
  • Many thanks @Ben, please find answer below. – Antonios Feb 21 '18 at 12:39

1 Answers1

2

I think the following graph answers your question and matches your requirements

colors=c("Amplification"="blue","pred"="red")
ggplot(data=One_APD,aes(x=Voltage)) + 
  geom_point(aes(y=log(log(Amplification)), colour="Amplification"),size=3) +
  geom_line(aes(y=pred,colour="pred"), size=2, alpha=0.3) +scale_colour_manual(name="Variable",values=colors) +
  ylab("log(log(Amplification)") + xlab("Voltage") 

enter image description here

Antonios
  • 1,919
  • 1
  • 11
  • 18