0

I have a data frame with 1149 rows and 6 columns. I made a plot with the columns protein number on the x-axis and iBAQ score on the y-axis. Now, I want to add labels to the scatterplot for a few proteins of interest, protein numbers: 3, 5, 10, 20, 43, 72, 151. My scatterplot code looks like this:

plot <- plot(Protein_number, iBAQ_score, xlab = "Proteins", ylab = "iBAQ score",
 main = 'Ccrosslinking on beads', abline(h=79961000, lwd=1, lty=2, col='green'),
 col=ifelse(Protein_number%in%Proteins_interest, 'red', 'black'),
 pch=ifelse(Protein_number%in%Proteins_interest, 19, 1),
 cex=ifelse(Protein_number%in%Proteins_interest, 1.2, 1),)

and it gives the following figure scatterplot

The proteins of interest are colored red in the picture. How can I add labels to my proteins of interest?

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Yy Law
  • 1
  • 1
    You can add labels to a base plot with `text()`. Check `?text` to find out about the different parameters. The principle is to provide 2 vectors `x`, and `y` for the position, with the respective `labels`. – Ray Jun 30 '21 at 21:06
  • You should move `abline(.)` *outside* of the `plot` call. While it *appears* to work, what is really happening is that it is inadvertently assigned to the `type=` argument of `plot`, and when the code attempts to determine what "type" of plot it should make, it evaluates that expression (which has the side-effect of adding the horiz line to a plot); when the expression returns `NULL`, plot then ignores `type` and it just so happens to work. If you ever actually *use* `type=`, or if you have other unnamed args in front of it, then that will no longer work. – r2evans Jun 30 '21 at 21:25
  • As far as adding labels, adding to @Ray's comment: you will likely want to subset your data (do you always store everything in vectors??) to the point of interest and then assign labels accordingly. I find that when plots get this "complex" (relatively speaking), the benefits and flexibility of `ggplot2` really shine, you might consider shifting to that plotting engine. I can't show you with your data (since we don't have it), but it would be straight-forward, and once you understand how ggplot layers things, it starts making a lot of sense. Good luck! – r2evans Jun 30 '21 at 21:28
  • Does this answer your question? [How can I label points in this scatterplot?](https://stackoverflow.com/questions/13229546/how-can-i-label-points-in-this-scatterplot) – Dylan_Gomes Jun 30 '21 at 21:29

0 Answers0