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

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

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

问题

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

这是df

dput(df)

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

而我的脚本如下:

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))

感谢任何帮助。

英文:

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

dput(df)

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

And my script is here

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

Thanks for any help.

答案1

得分: 1

你可以通过 `geom_text` 的 `size` 参数来设置标签的大小:

```r
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)

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


<details>
<summary>英文:</summary>

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)


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


  [1]: https://i.stack.imgur.com/tN0ec.png

</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:

确定