0

My data is displayed in the picture below. I'm using ggplot2 to draw a dotplot:

  ggplot(dr, aes(x = tSNE1, y = tSNE2, color = HLA_DR)) +
    geom_point(size = 0.1) +
    theme_bw() +
    scale_color_gradientn("TCRgd",
                          colours = 
                          colorRampPalette(rev(
                          brewer.pal(n = 11, name = "RdYlBu")))(50))+
    theme(panel.background = element_rect(fill = "transparent",colour = NA), 
          panel.grid.minor = element_blank(), 
          panel.grid.major = element_blank(),
          plot.background = element_rect(fill = "transparent",colour = NA))

How can I plot all the other columns(like CD14 ,CD16...) at the same time like the facet function.

enter image description here

Eric Brandt
  • 7,886
  • 3
  • 18
  • 35
  • Welcome to Stack Overflow! Please provide a [reproducible example in r](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The link I provided, will tell you how. Moreover, please take the [tour](https://stackoverflow.com/tour) and visit [how to ask](https://stackoverflow.com/help/how-to-ask). In particular, do not post picture of your code/data. In this case, use `dput(head(your_dataframe,10))` to provide an example dataset. Cheers. – M-- Jan 21 '19 at 14:49
  • The `facet_wrap` and `facet_grid` functions work best when you want to _facet_ your data based on a categorical field. The columns/fields that you reference in your question, _CD14_, _CD16_, etc.. would not allow you to facet. – OTStats Jan 21 '19 at 15:02
  • @OTStats I disagree: they can be properly thought of as facets of the same data set. You just need to restructure the dataframe into long format. This specific example is a fairly common use-case of faceting in my field (genetics). – Konrad Rudolph Jan 21 '19 at 15:16
  • @KonradRudolph Ah, sure enough. Well worked answer below. Thanks for the perspective, Konrad. – OTStats Jan 21 '19 at 16:44
  • Even if you got your answer, please consider explaining your problem clearly and going through my first comment as it is important to have valuable questions for other users stumping by this thread. – M-- Jan 21 '19 at 17:55

1 Answers1

0

In order to perform faceting you need to reshape your data such that one column contains the faceting factor, and another contains the facet values. This is easy enough using e.g. the {tidyr} package:

dr_long = dr %>% gather(Receptor, Expression, -tSNE1, -tSNE2)

This gives you a table that looks like this:

    tSNE1 tSNE2 Receptor Expression
    <dbl> <dbl> <chr>         <dbl>
 1 0.103  0.466 HLA_DR       -0.475
 2 0.799  0.322 HLA_DR        1.63
 3 0.262  0.591 HLA_DR       -0.100
 4 0.562  0.756 HLA_DR       -1.11
 5 0.566  0.898 HLA_DR        1.22
 6 0.149  0.742 HLA_DR        0.109
…

Now you can plot it:

ggplot(dr_long) +
    aes(tSNE1, tSNE2, color = Expression) +
    geom_point() +
    facet_wrap(~ Receptor)

Since you’re plotting tSNE data, you probably also want to add coord_fixed() to your plotting command.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214