0

Hi I am trying to draw diagonal lines on specific boxes which belongs to class2. I got this: Image1

My dataset:


samples genes   value   class
sample1 geneA   0.52    class2
sample1 geneB   1       class1
sample1 geneC   1       class1
sample2 geneD   1       class1
sample2 geneB   1       class1
sample2 geneH   0.4     class2
sample2 geneC   1       class1
sample3 geneE   0.44    class2
sample3 geneF   0.34    class2
sample3 geneB   1       class1
sample3 geneI   0.65    class2
sample3 geneC   1       class1
sample4 geneB   0.72    class2
sample4 geneC   0.41    class2
sample5 geneG   1       class1
sample5 geneB   1       class1
sample5 geneC   1       class1

Class1 is marked with yellow lines encircle the boxes. Can you please help me to figure out insert diagonal line on boxes belong to class2.

I want like this:

Image2

Thanks, kumarr

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Welcome to SO, Kumarr. Please hover over the R tag - it asks for a reproducible example, with which one can recreate your problem using copy,paste,run. See [this GIF](http://giphy.com/embed/3og0IEmawWtqcdLRug) and [this guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). You may want to use that in future postings. – lukeA Mar 21 '17 at 21:30

1 Answers1

0

Here's one way to do it:

p <- ggplot(df, aes(x=genes,y=samples,fill=class)) + geom_tile()
p + geom_segment(
  aes(x=xmin,xend=xmax,y=ymin,yend=ymax), 
  subset(ggplot_build(p)$data[[1]],fill=="#00BFC4"),
  inherit.aes=F
)

enter image description here

with

library(ggplot2)
df <- read.table(header=T, text="samples genes   value   class
sample1 geneA   0.52    class2
sample1 geneB   1       class1
sample1 geneC   1       class1
sample2 geneD   1       class1
sample2 geneB   1       class1
sample2 geneH   0.4     class2
sample2 geneC   1       class1
sample3 geneE   0.44    class2
sample3 geneF   0.34    class2
sample3 geneB   1       class1
sample3 geneI   0.65    class2
sample3 geneC   1       class1
sample4 geneB   0.72    class2
sample4 geneC   0.41    class2
sample5 geneG   1       class1
sample5 geneB   1       class1
sample5 geneC   1       class1")
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Hi lukeA, thanks for your quick answer. you are using "fill=class". But i want to use "fill=value" and in that case above code is not working. Can you please help me to figure out that. thanks!! – user3701788 Mar 21 '17 at 21:41
  • Maybe `p <- ggplot(df, aes(x=genes,y=samples,fill=value, group=class)) + geom_tile(); p + geom_segment( aes(x=xmin,xend=xmax,y=ymin,yend=ymax), subset(ggplot_build(p)$data[[1]],group==2), inherit.aes=F )`. You should edit your question and follow to comment. – lukeA Mar 21 '17 at 21:44
  • Many thanks, it's working now. But if you see my image1, i have put some gap between boxes by using my own code and when i am merging that with your code, it's showing error. here is my code: ``ggplot()+geom_tile(data=data,aes(samples,genes,fill=value),width=0.9,height=0.9)+geom_tile(data=data1,aes(samples,genes),size=1,fill=NA,width=0.9,height=0.9,color="darkgoldenrod1") + scale_fill_gradient2(low="white", high="midnightblue", na.value="black", name="Cancer Cell Fraction") + coord_flip() + scale_x_discrete(name="") + scale_y_discrete(name="")`` – user3701788 Mar 21 '17 at 21:52
  • `p <- ggplot(df, aes(x=genes,y=samples,fill=value, group=class)) + geom_tile(data=df,aes(samples,genes,fill=value),width=0.9,height=0.9)+ geom_tile(data=df,aes(samples,genes),size=1,fill=NA,width=0.9,height=0.9,color="darkgoldenrod1") + scale_fill_gradient2(low="white", high="midnightblue", na.value="black", name="Cancer Cell Fraction") + coord_flip() + scale_x_discrete(name="") + scale_y_discrete(name=""); p + geom_segment( aes(x=xmin,xend=xmax,y=ymin,yend=ymax), subset(ggplot_build(p)$data[[1]],group==2), inherit.aes=F ) ` – lukeA Mar 22 '17 at 08:09
  • Thanks lukeA, this is really awesome, thanks very much. I am afraid to one ask one more thing here. Actually, i also need boxes for combinations where no data is present (e.g. sample2-geneA; sample1-geneD etc) and i need to color these boxes as grey, just to discriminate from the others. Thanks very much!! – user3701788 Mar 23 '17 at 00:40
  • I know, i can do this by adding dummy data, but keen to know, if there is any other way to do this with this dataset. – user3701788 Mar 23 '17 at 01:16
  • Quick & dirty: plot those boxes on a full grid below the others. If you need help, post a new question as this has nothing to do with the original one. – lukeA Mar 23 '17 at 15:18