0

I currently have a code to make a scatterplot as shown below, I made two objects of gene names that are being downregulated and upregulated, Then I added the two objects to points (points = c(genes.to.labelCtrl, genes.to.labelCis)) so that only the labels in these two objects show up on the scatterplot. But I would like the genes.to.labelCtrl gene names to be blue in color and genes.to.labelCis gene names to be red in color. But I can't add more than one color to the LabelPoints, otherwise the code throws me an error.

p1 <- ggplot(object, aes(CTRL, CIS)) + geom_point() + ggtitle("Macrophage CTRL vs CIS") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 1,alpha = 0.6) +   
  geom_smooth(method=lm, se=TRUE, color="brown", linetype="solid", size=1) 

 p1 <- LabelPoints(plot = p1, points = c(genes.to.labelCtrl, genes.to.labelCis), color="blue", repel = TRUE, xnudge=0, ynudge=0)

 plot_grid(p1)

Do you know how to overcome this issue?

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
Cathy
  • 23
  • 3
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Sep 29 '20 at 05:48
  • Is `LabelPoints()` from `Seurat` package? – Paul Sep 29 '20 at 05:55
  • @MrFlick Apologies for not providing a reproducible example. I think I was able to figure it out. – Cathy Sep 29 '20 at 18:48
  • @Paul I believe LabelPoints() is from ggplot2 package and not Seurat package. – Cathy Sep 29 '20 at 18:48
  • @Cathy I was not able to find it in `ggplot2` package. Is it this one: https://www.rdocumentation.org/packages/Seurat/versions/3.1.4/topics/LabelPoints – Paul Sep 30 '20 at 06:07

2 Answers2

1

probably it's not a good solution and won't adress your problem. But as a reproducible example was missing (as MrFlick already mentioned) and as my knowledge on genes is basically NULL, I had to be creative :D

I assumed that you have two label variables in your object dataframe and that you only want some of your CTRL/CIS values to be labeled. If that's the case I would pre calculate the label variables (based on a set of conditions you define ... here it was just a sample). In the next step I would plot both labels with geom_text and define a unique color (I am not sure what exactely LabelPoints is doing, because I had no possibility to test it ... so I sticked to your description)

I hope you can use this as a starting point (otherwise just throw it away ^^)

object <- dplyr::tibble(ID=1:100,
                        CTRL=rnorm(100),
                        CIS=rnorm(100),
                        label1=sample(c("","A"),size=100,replace=T,prob=c(.9,.1)),
                        label2=sample(c("","B"),size=100,replace=T,prob=c(.9,.1)))


ggplot2::ggplot(object, 
                      ggplot2::aes(x=CTRL, y=CIS)) +
  ggplot2::geom_point() + 
  ggplot2::geom_text(ggplot2::aes(label=label1),color="red") +
  ggplot2::geom_text(ggplot2::aes(label=label2),color="blue") +
  ggplot2::ggtitle("Macrophage CTRL vs CIS") + 
  ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) + 
  ggplot2::geom_point(size = 1,
                      alpha = 0.6) +   
  ggplot2::geom_smooth(method=lm,
                       se=TRUE,
                       color="brown",
                       linetype="solid",
                       size=1) 
sambold
  • 807
  • 5
  • 15
0

@sambold Thank you so very much for your detailed response despite no reproducible example. Next time I have a question, I will make sure to upload an example as well.

I think I was able to figure out a simple solution. In my initial code, I added my two gene name data frames as a vector in the “points” slot as below.

p1 <- LabelPoints(plot = p1, points = c(genes.to.labelCtrl, genes.to.labelCis), color="blue", repel = TRUE, xnudge=0, ynudge=0)

However, if I separate the gene name data frame into two R codes, then I am able to color code gene names from each gene name data frame as shown below.

p1 <- LabelPoints(plot = p1, points = genes.to.labelCtrl, color="blue", repel = TRUE, xnudge=0, ynudge=0)
p1 <- LabelPoints(plot = p1, points = genes.to.labelCis, color="red", repel = TRUE, xnudge=0, ynudge=0)
Paul
  • 2,850
  • 1
  • 12
  • 37
Cathy
  • 23
  • 3