如何在ggplot2中从R数据框的其他列添加条件注释。

huangapple go评论107阅读模式
英文:

How to add conditional annotation from other column of R dataframe in ggplot2

问题

I will translate the code part as requested:

  1. 我正在尝试从包含超过300k行的数据框中制作一个图表,其中Y的高值和低值将从另一列而不是XY中的数据进行注释。
  2. 我该怎么做..!!
  3. 数据框:
  4. chr start end.x StoZ.x Hscore end.y StoZ.y Tier Gene
  5. 1 chr1 1 10000 0.0000000 0 NA NA <NA> <NA>
  6. 2 chr1 10001 20000 3.6488202 2 NA NA <NA> <NA>
  7. 3 chr1 20001 30000 -0.8475483 0 NA NA <NA> <NA>
  8. 4 chr1 30001 40000 2.1279359 2 NA NA <NA> <NA>
  9. 5 chr1 40001 50000 1.4119515 0 NA NA <NA> <NA>
  10. ................
  11. ................
  12. ................
  13. 256 chr1 2550001 2560000 -2.363378 1 2560000 -2.363378 T1 TNFRSF14
  14. 257 chr1 2560001 2570000 -0.796173 0 2570000 -0.796173 T1 TNFRSF14
  15. ................
  16. ................
  17. ................
  18. 305 chr1 3040001 3050000 0.0564608 0 NA NA NA NA
  19. 306 chr1 3050001 3060000 1.4822029 0 NA NA NA NA
  20. 307 chr1 3060001 3070000 1.7718186 0 3070000 1.7718186 T1 PRDM16
  21. 308 chr1 3070001 3080000 1.5650776 0 3080000 1.5650776 T1 PRDM16
  22. ................
  23. ................
  24. ................

The translation of the code snippet is complete. If you have any more specific questions or need further assistance, please feel free to ask.

英文:

I'm trying to make a plot from a dataframe of over 300k rows where the high and low values of Y will be annotated from another column rather than the x and y.
How can I do that..!!

Dataframe :

  1. chr start end.x StoZ.x Hscore end.y StoZ.y Tier Gene
  2. 1 chr1 1 10000 0.0000000 0 NA NA &lt;NA&gt; &lt;NA&gt;
  3. 2 chr1 10001 20000 3.6488202 2 NA NA &lt;NA&gt; &lt;NA&gt;
  4. 3 chr1 20001 30000 -0.8475483 0 NA NA &lt;NA&gt; &lt;NA&gt;
  5. 4 chr1 30001 40000 2.1279359 2 NA NA &lt;NA&gt; &lt;NA&gt;
  6. 5 chr1 40001 50000 1.4119515 0 NA NA &lt;NA&gt; &lt;NA&gt;
  7. ................
  8. ................
  9. ................
  10. 256 chr1 2550001 2560000 -2.363378 1 2560000 -2.363378 T1 TNFRSF14
  11. 257 chr1 2560001 2570000 -0.796173 0 2570000 -0.796173 T1 TNFRSF14
  12. ................
  13. ................
  14. ................
  15. 305 chr1 3040001 3050000 0.0564608 0 NA NA NA NA
  16. 306 chr1 3050001 3060000 1.4822029 0 NA NA NA NA
  17. 307 chr1 3060001 3070000 1.7718186 0 3070000 1.7718186 T1 PRDM16
  18. 308 chr1 3070001 3080000 1.5650776 0 3080000 1.5650776 T1 PRDM16
  19. ................
  20. ................
  21. ................

如何在ggplot2中从R数据框的其他列添加条件注释。

Till now what I did,

  1. p2&lt;-ggplot(BreastCanT1T2mer, aes(start, StoZ.x)) +
  2. geom_jitter(aes(color = Hscore), pch = 23, size = 0.2) +
  3. scale_colour_manual(values = c(&quot;gray70&quot;,&quot;#525480&quot;,&quot;#9F5370&quot;,&quot;blue&quot;,&quot;red&quot;), labels=c(&#39;NEUT&#39;, &#39;LOSS&#39;, &#39;GAIN&#39;, &quot;DEL&quot;,&quot;AMP&quot; ))+
  4. theme(legend.position = c(0.5, .06), legend.direction = &quot;horizontal&quot;)+
  5. theme(legend.title = element_blank()) +
  6. geom_hline(yintercept=0, linetype=&quot;dashed&quot;, color = &quot;black&quot;)+
  7. guides(colour = guide_legend(override.aes = list(size=1.5)))+
  8. xlab(&quot;Chromosomes&quot;) + ylab(&quot;Stoffers-Z Score&quot;)+
  9. facet_wrap(~factor(chr, c(&quot;chr1&quot;, &quot;chr2&quot;, &quot;chr3&quot;, &quot;chr4&quot;, &quot;chr5&quot;, &quot;chr6&quot;, &quot;chr7&quot;, &quot;chr8&quot;, &quot;chr9&quot;, &quot;chr10&quot;, &quot;chr11&quot;, &quot;chr12&quot;, &quot;chr13&quot;, &quot;chr14&quot;,&quot;chr15&quot;, &quot;chr16&quot;,&quot;chr17&quot;,&quot;chr18&quot;,&quot;chr19&quot;,&quot;chr20&quot;,&quot;chr21&quot;,&quot;chr22&quot;, &quot;chrX&quot;, &quot;chrY&quot;)), nrow =1, strip.position = &quot;bottom&quot;) +
  10. theme(panel.spacing.x = unit(0.1, &quot;lines&quot;))+
  11. theme(axis.text.x=element_blank())+
  12. theme( legend.key = element_rect(fill = &quot;transparent&quot;, colour = NA))
  13. p2
  14. p2+stat_peaks(span=7,size= 1, ignore_threshold = 0.95, color=&quot;brown&quot;)+
  15. stat_peaks(geom=&quot;text&quot;, size= 1.5, span=7, ignore_threshold = 0.95, color=&quot;red&quot;, angle=90, hjust=-0.1, check_overlap = TRUE) +
  16. stat_valleys(span=7,size= 1, ignore_threshold = 0.95, color=&quot;skyblue&quot;)+
  17. stat_valleys(geom=&quot;text&quot;, size= 1.5, span=7, ignore_threshold = 0.95, color=&quot;black&quot;, angle=90, hjust=1.1, check_overlap = TRUE)

My expectation is that the Gene name will appear only once where the Gene is available (StoZ is probably highest and lowest) and where the StoZ value should be less than -10 or greater than +10.

**Improvement after @Elin's sugesstion

  1. p2+geom_text(aes(label=Gene))

Here, the Name of the Genes should appear only once and y conditioned on <-2 and >+2.

如何在ggplot2中从R数据框的其他列添加条件注释。

答案1

得分: 2

What I have so far is here, however, I believe the output may be better.

  1. p2 + geom_text_repel(aes(label = ifelse(StoZ.x < -20, unique(Gene), "")), size = 1.5, angle = 45) +
  2. geom_text_repel(aes(label = ifelse(StoZ.x > 20, unique(Gene), "")), size = 1.5, angle = 45)

Now I'm wondering if I can make geom_text color based on the Tier column.

So far, what I've done hasn't yielded the desired results. Because I have called scale_color_manual in p2, I can't call it here. So I'm attempting to use scale_fill_manual, but it's not working.

  1. p2 + geom_text_repel(aes(label = ifelse(StoZ.x < -10, unique(Gene), ""), hjust = 1, vjust = 0.5, size = 0.5, angle = 45), fill = as.factor(BreastCanT1T2mer$Tier)) +
  2. scale_fill_manual(values = c("brown", "navy"))
英文:

What I have so far is here, however, I believe the output may be better.

  1. p2+geom_text_repel(aes(label= ifelse(StoZ.x &lt; -20, unique(Gene), &quot;&quot;)), size=1.5, angle = 45) +
  2. geom_text_repel(aes(label= ifelse(StoZ.x &gt; 20, unique(Gene), &quot;&quot;)), size=1.5, angle = 45)

如何在ggplot2中从R数据框的其他列添加条件注释。

Now I'm wondering if I can make geom_text color based Tier column.

So far, what I've done hasn't yielded the desired results. Because I have called scale_color_manual in p2, I can't call it here. So I'm attempting to use scale_fill_manual. but it's not working.
Hoping for advices.

  1. p2+geom_text_repel(aes(label= ifelse(StoZ.x &lt; -10, unique(Gene), &quot;&quot;), hjust = 1, vjust=.5, size=0.5, angle = 45), fill = as.factor(BreastCanT1T2mer$Tier)) +
  2. scale_fill_manual(values = c(&quot;brown&quot;, &quot;navy&quot;))

huangapple
  • 本文由 发表于 2023年5月21日 09:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定