如何在我的 ggplot2 图中更改星号的大小?

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

How to change the size of stars in my plot by ggplot2?

问题

我想根据padj值在我的图表上添加星号,但星号的大小太小了。我知道我是手动添加的,但我不知道如何在我的图表上使用更大的星号。

这是df

  1. dput(df)
  2. structure(list(CancerType = c("BRCA", "COAD", "LUAD", "LUSC",
  3. "STAD", "THCA", "UCEC", "ESCA", "KIRC", "PRAD", "LIHC", "GBM",
  4. "BLCA"), log2FC = c(-0.145366946, -0.276093973, 0.056591306,
  5. 0.150671917, 0.159516768, 0.040044405, -0.050525831, -0.09909923,
  6. 0.125670677, -0.131356051, 0.090115912, -0.170939552, -0.150633173
  7. ), padj = c(0.002552355, 0.001254905, 0.515358544, 0.119809051,
  8. 0.117073154, 0.407311464, 0.595674904, 0.643894805, 0.029380174,
  9. 0.014769961, 0.251602611, 0.470002204, 0.228163654)), class = "data.frame", row.names = c(7L,
  10. 23L, 39L, 55L, 71L, 87L, 103L, 119L, 135L, 151L, 167L, 183L,
  11. 199L))

而我的脚本如下:

  1. nudge <- 0.02
  2. x %>%
  3. mutate(
  4. sig_label = case_when(
  5. padj < 0.001 ~ "***",
  6. padj < 0.01 ~ "**",
  7. padj <= 0.05 ~ "*",
  8. padj > 0.05 ~ ""
  9. ),
  10. label_position = ifelse(log2FC > 0, log2FC + nudge, log2FC - (nudge+0.01))
  11. ) %>%
  12. ggplot(aes(x = CancerType, y = log2FC)) +
  13. geom_bar(position = "stack", stat = "identity", fill = '#38357A') +
  14. theme_bw() +
  15. theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1, colour = 'black')) +
  16. geom_text(aes(label = sig_label, y = label_position))

感谢任何帮助。

英文:

I want to add stars to my plots based on padj values, but the of stars are too small. I know that I added them manually but I don't know how to have bigger stars on my plots.

Here is df

  1. dput(df)
  2. structure(list(CancerType = c(&quot;BRCA&quot;, &quot;COAD&quot;, &quot;LUAD&quot;, &quot;LUSC&quot;,
  3. &quot;STAD&quot;, &quot;THCA&quot;, &quot;UCEC&quot;, &quot;ESCA&quot;, &quot;KIRC&quot;, &quot;PRAD&quot;, &quot;LIHC&quot;, &quot;GBM&quot;,
  4. &quot;BLCA&quot;), log2FC = c(-0.145366946, -0.276093973, 0.056591306,
  5. 0.150671917, 0.159516768, 0.040044405, -0.050525831, -0.09909923,
  6. 0.125670677, -0.131356051, 0.090115912, -0.170939552, -0.150633173
  7. ), padj = c(0.002552355, 0.001254905, 0.515358544, 0.119809051,
  8. 0.117073154, 0.407311464, 0.595674904, 0.643894805, 0.029380174,
  9. 0.014769961, 0.251602611, 0.470002204, 0.228163654)), class = &quot;data.frame&quot;, row.names = c(7L,
  10. 23L, 39L, 55L, 71L, 87L, 103L, 119L, 135L, 151L, 167L, 183L,
  11. 199L))

And my script is here

  1. nudge &lt;- 0.02
  2. x |&gt;
  3. mutate(
  4. sig_label = case_when(
  5. padj &lt; 0.001 ~ &quot;***&quot;,
  6. padj &lt; 0.01 ~ &quot;**&quot;,
  7. padj &lt;= 0.05 ~ &quot;*&quot;,
  8. padj &gt; 0.05 ~ &quot;&quot;
  9. ),
  10. label_position = ifelse(log2FC &gt; 0, log2FC + nudge, log2FC - (nudge+0.01))
  11. ) |&gt; ggplot(aes(x = CancerType,y = log2FC))+
  12. geom_bar(position=&quot;stack&quot;, stat=&quot;identity&quot;, fill=&#39;#38357A&#39;)+
  13. theme_bw()+
  14. theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1, colour = &#39;black&#39;))+
  15. geom_text(aes(label = sig_label, y = label_position))

Thanks for any help.

答案1

得分: 1

  1. 你可以通过 `geom_text` `size` 参数来设置标签的大小:
  2. ```r
  3. library(dplyr)
  4. library(ggplot2)
  5. nudge <- 0.02
  6. x %>%
  7. mutate(
  8. sig_label = case_when(
  9. padj < 0.001 ~ "***",
  10. padj < 0.01 ~ "**",
  11. padj <= 0.05 ~ "*",
  12. padj > 0.05 ~ ""
  13. ),
  14. label_position = ifelse(log2FC > 0, log2FC + nudge, log2FC - (nudge + 0.01))
  15. ) %>%
  16. ggplot(aes(x = CancerType, y = log2FC)) +
  17. geom_bar(position = "stack", stat = "identity", fill = "#38357A") +
  18. theme_bw() +
  19. theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1, colour = "black")) +
  20. geom_text(aes(label = sig_label, y = label_position), size = 16)

[![enter image description here][1]][1]

  1. <details>
  2. <summary>英文:</summary>
  3. You could set the size for your labels via the `size` argument of `geom_text`:

library(dplyr)
library(ggplot2)

nudge <- 0.02

x |>
mutate(
sig_label = case_when(
padj < 0.001 ~ "",
padj < 0.01 ~ "
",
padj <= 0.05 ~ "
",
padj > 0.05 ~ ""
),
label_position = ifelse(log2FC > 0, log2FC + nudge, log2FC - (nudge + 0.01))
) |>
ggplot(aes(x = CancerType, y = log2FC)) +
geom_bar(position = "stack", stat = "identity", fill = "#38357A") +
theme_bw() +
theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1, colour = "black")) +
geom_text(aes(label = sig_label, y = label_position), size = 16)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/tN0ec.png
  3. </details>

huangapple
  • 本文由 发表于 2023年1月9日 04:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051045.html
匿名

发表评论

匿名网友

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

确定