在条形图旁边添加列数数值

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

Add column values to a bar-plot next to plot names

问题

这个数据框:

structure(list(taxon = c("Bacteria 1", "Bacteria 2", 
"Bacteria 3", "Bacteria 4", "Bacteria 5", "Bacteria 6"), lfc_StatusChronic.ACST0. = c(0.88175957, 
0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253), 
se_StatusChronic.ACST0. = c(0.439259504, 0.449835605, 0.369268494, 
0.391714918, 0.27578621, 0.364036816), lfc_Time.fT1 = c(-0.021243562, 
0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181
), se_Time.fT1 = c(0.312681188, 0.330173331, 0.301559494, 
0.309355933, 0.293938402, 0.302957725), lfc_Time.fT2 = c(-1.092105655, 
-0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087
), se_Time.fT2 = c(0.326565043, 0.344533883, 0.31544836, 
0.323423323, 0.307225241, 0.317023725), lfc_Time.fT3 = c(-0.684898535, 
0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069
), se_Time.fT3 = c(0.324919486, 0.342422134, 0.314578177, 
0.322254678, 0.305999846, 0.316331693), lfc_Time.fT4 = c(-1.038613852, 
-0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665
), se_Time.fT4 = c(0.319416657, 0.336513636, 0.309526757, 
0.316959694, 0.300928605, 0.311343927), lfc_Time.fT5 = c(-0.714954683, 
0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519
), se_Time.fT5 = c(0.317230276, 0.334106044, 0.307553106, 
0.314893819, 0.298943665, 0.309379791), pvalue = c(0.0447, 
0.0484, 0.0322, 8e-04, 0.0093, 0.0372)), row.names = c(NA, 
6L), class = "data.frame")

你已经创建了这样的条形图:

taxonFC1 %>%
  rename(lfc_time.fT0 = lfc_StatusChronic.ACST0., 
         se_Time.fT0 = se_StatusChronic.ACST0.) %>%
  pivot_longer(-taxon, names_pattern="(.*)_\\[Tt\\]ime\\.f(.*)", 
               names_to = c(".value", "time")) %>% 
   ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = taxon)) +
   geom_bar(stat = "identity") +
   geom_errorbar(width=.4) + 
   theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   scale_fill_brewer(palette="Paired") + 
    xlab("") +
    ylab("LogFC") +
    facet_wrap(~taxon, ncol=3) + 
    ggtitle("9 taxas") +
    scale_x_discrete(labels = c("ACs & Chronic", "T1 Vs T0", "T2 Vs T0", "T3 Vs T0", "T4 Vs T0", "T5 Vs T0"))

你想要把数据框最后一列的 p 值放到细菌名称旁边(例如,Bacteria 1 (p-value = 0.0093)),但不放在图例中。如果可能的话,按显著性排序(p < 0.05)。

英文:

I have this dataframe:

structure(list(taxon = c(&quot;Bacteria 1&quot;, &quot;Bacteria 2&quot;, 
&quot;Bacteria 3&quot;, &quot;Bacteria 4&quot;, &quot;Bacteria 5&quot;, &quot;Bacteria 6&quot;), lfc_StatusChronic.ACST0. = c(0.88175957, 
0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253), 
    se_StatusChronic.ACST0. = c(0.439259504, 0.449835605, 0.369268494, 
    0.391714918, 0.27578621, 0.364036816), lfc_Time.fT1 = c(-0.021243562, 
    0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181
    ), se_Time.fT1 = c(0.312681188, 0.330173331, 0.301559494, 
    0.309355933, 0.293938402, 0.302957725), lfc_Time.fT2 = c(-1.092105655, 
    -0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087
    ), se_Time.fT2 = c(0.326565043, 0.344533883, 0.31544836, 
    0.323423323, 0.307225241, 0.317023725), lfc_Time.fT3 = c(-0.684898535, 
    0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069
    ), se_Time.fT3 = c(0.324919486, 0.342422134, 0.314578177, 
    0.322254678, 0.305999846, 0.316331693), lfc_Time.fT4 = c(-1.038613852, 
    -0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665
    ), se_Time.fT4 = c(0.319416657, 0.336513636, 0.309526757, 
    0.316959694, 0.300928605, 0.311343927), lfc_Time.fT5 = c(-0.714954683, 
    0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519
    ), se_Time.fT5 = c(0.317230276, 0.334106044, 0.307553106, 
    0.314893819, 0.298943665, 0.309379791), pvalue = c(0.0447, 
    0.0484, 0.0322, 8e-04, 0.0093, 0.0372)), row.names = c(NA, 
6L), class = &quot;data.frame&quot;)

and I have done a bar plot like this:

taxonFC1 %&gt;%
  rename(lfc_time.fT0 = lfc_StatusChronic.ACST0., 
         se_Time.fT0 = se_StatusChronic.ACST0.) %&gt;%
  pivot_longer(-taxon, names_pattern=&quot;(.*)_[Tt]ime\\.f(.*)&quot;, 
               names_to = c(&quot;.value&quot;, &quot;time&quot;)) %&gt;% 
   ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = taxon)) +
   geom_bar(stat = &quot;identity&quot;) +
   geom_errorbar(width=.4) + 
   theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   scale_fill_brewer(palette=&quot;Paired&quot;) + 
    xlab(&quot;&quot;) +
    ylab(&quot;LogFC&quot;) +
    facet_wrap(~taxon, ncol=3) + 
    ggtitle(&quot;9 taxas&quot;) +
    scale_x_discrete(labels = c(&quot;ACs &amp; Chronic&quot;, &quot;T1 Vs T0&quot;, &quot;T2 Vs T0&quot;, &quot;T3 Vs T0&quot;, &quot;T4 Vs T0&quot;, &quot;T5 Vs T0&quot;))

As you can see, the last column of the dataframe contains the p-values, I would like to put this values next to the bacteria name (e.g. Bacteria 1 (p-value = 0.0093)), but not in the legend.
And if it is possible to sort them from more significative (p<0.05).

I have tried using this:

taxonFC1$taxon &lt;- paste(taxonFC1$taxon, &quot;(p-value =&quot;, taxonFC1[,ncol(taxonFC1)], &quot;)&quot;, sep = &quot; &quot;)

but it shows also in the legend.

Any help is more than welcome
Thanks!!

答案1

得分: 1

调整绘图顺序,我们添加了因子水平;提取细菌名称,我们进行了一些字符串操作。

英文:

To change plot order we add factor levels, to extract bacteria names we do some character string manipulation.

taxonFC1$taxon &lt;- paste(taxonFC1$taxon, &quot;(p-value =&quot;, taxonFC1[,ncol(taxonFC1)], &quot;)&quot;, sep = &quot; &quot;)
legendcolors &lt;- RColorBrewer::brewer.pal(6,&#39;Paired&#39;)
taxonFC1 %&gt;% arrange(pvalue) %&gt;% 
  rename(lfc_time.fT0 = lfc_StatusChronic.ACST0., 
         se_Time.fT0 = se_StatusChronic.ACST0.) %&gt;%
  pivot_longer(-taxon, names_pattern=&quot;(.*)_[Tt]ime\\.f(.*)&quot;, 
               names_to = c(&quot;.value&quot;, &quot;time&quot;)) %&gt;% 
  na.omit() %&gt;% # Be careful this removes all rows with ANY NA&#39;s
  mutate(legend_labels=substr(taxon,1,unlist(gregexpr(&#39;p-value&#39;, taxon))[2]-2),#str_locate(taxon,&quot;p-value = &quot;)[1,1]-2),
         pval = substr(taxon,str_locate(taxon,&quot;p-value = &quot;)[1,2]+1,str_locate(taxon,&quot;p-value = &quot;)[1,2]+6),
         # testlabel=substr(taxon,1,str_locate(taxon,&#39; &#39;)[1,1]))
         porder=factor(taxon, levels = c(&quot;Bacteria 4 (p-value = 8e-04 )&quot;,&quot;Bacteria 5 (p-value = 0.0093 )&quot;,
                                                    &quot;Bacteria 3 (p-value = 0.0322 )&quot;,&quot;Bacteria 6 (p-value = 0.0372 )&quot;,
                                                    &quot;Bacteria 1 (p-value = 0.0447 )&quot;,&quot;Bacteria 2 (p-value = 0.0484 )&quot;))) %&gt;% 
  ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = legend_labels)) +
  geom_bar(stat = &quot;identity&quot;) +
  geom_errorbar(width=.4) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  # scale_fill_brewer(palette=&quot;Paired&quot;) + 
  xlab(&quot;&quot;) +
  ylab(&quot;LogFC&quot;) +
  facet_wrap(~porder, ncol=3, ) + 
  ggtitle(&quot;9 taxas&quot;) +
  scale_x_discrete() +
  scale_fill_manual(name=&quot;Bacteria Name&quot;,
                    values = legendcolors)

在条形图旁边添加列数数值

huangapple
  • 本文由 发表于 2023年3月15日 21:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745360.html
匿名

发表评论

匿名网友

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

确定