英文:
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("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")
and I have done a bar plot like this:
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"))
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 <- paste(taxonFC1$taxon, "(p-value =", taxonFC1[,ncol(taxonFC1)], ")", sep = " ")
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 <- paste(taxonFC1$taxon, "(p-value =", taxonFC1[,ncol(taxonFC1)], ")", sep = " ")
legendcolors <- RColorBrewer::brewer.pal(6,'Paired')
taxonFC1 %>% arrange(pvalue) %>%
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")) %>%
na.omit() %>% # Be careful this removes all rows with ANY NA's
mutate(legend_labels=substr(taxon,1,unlist(gregexpr('p-value', taxon))[2]-2),#str_locate(taxon,"p-value = ")[1,1]-2),
pval = substr(taxon,str_locate(taxon,"p-value = ")[1,2]+1,str_locate(taxon,"p-value = ")[1,2]+6),
# testlabel=substr(taxon,1,str_locate(taxon,' ')[1,1]))
porder=factor(taxon, levels = c("Bacteria 4 (p-value = 8e-04 )","Bacteria 5 (p-value = 0.0093 )",
"Bacteria 3 (p-value = 0.0322 )","Bacteria 6 (p-value = 0.0372 )",
"Bacteria 1 (p-value = 0.0447 )","Bacteria 2 (p-value = 0.0484 )"))) %>%
ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = legend_labels)) +
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(~porder, ncol=3, ) +
ggtitle("9 taxas") +
scale_x_discrete() +
scale_fill_manual(name="Bacteria Name",
values = legendcolors)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论