Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中的特定框上绘制对角线?_R_Ggplot2 - Fatal编程技术网

R 在ggplot2中的特定框上绘制对角线?

R 在ggplot2中的特定框上绘制对角线?,r,ggplot2,R,Ggplot2,嗨,我正试图在属于2类的特定方框上画对角线。 我明白了: 我的数据集: Class1用黄线标记在方框周围。你们能帮我算出在第二类盒子上插入对角线吗 我想要这样: 谢谢, kumarr有一种方法: 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_bui

嗨,我正试图在属于2类的特定方框上画对角线。 我明白了:

我的数据集:



Class1用黄线标记在方框周围。你们能帮我算出在第二类盒子上插入对角线吗

我想要这样:

谢谢,
kumarr有一种方法:

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
)

p欢迎来到SO,库马尔。请将鼠标悬停在R标记上-它要求提供一个可复制的示例,使用该示例可以使用“复制”、“粘贴”、“运行”重新创建问题。见和。你可能想在以后的帖子中使用它。嗨,卢卡,谢谢你的快速回答。您正在使用“fill=class”。但是我想使用“fill=value”,在这种情况下,上面的代码不起作用。你能帮我弄清楚吗。谢谢也许
p非常感谢,它正在工作。但如果你看到我的图片1,我用我自己的代码在方框之间留出了一些间隙,当我将其与你的代码合并时,它显示了错误。这是我的代码:
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=“癌细胞分数”)+coord+flip()+scale\x离散(name=”“)+scale_y_离散(name=”“)
p谢谢lukeA,这真是太棒了,非常感谢。我恐怕还有一件事要问。实际上,我还需要没有数据的组合框(例如,sample2 geneA;sample1 geneD等),我需要将这些框涂成灰色,以区别于其他框。非常感谢!!
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
)
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")