添加标准误差以修正分面条形图上的面板

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

adding standard errors to correct panels on faceted bar chart

问题

我已翻译代码部分,以下是翻译好的内容:

  1. 我有这个数据框:
  2. 这是一个数据框,其中每一行都是一个类别,列与时间序列(从T0T5)相对应。
  3. 我想为每个类别(taxon)的时间(T0-T5)制作一个条形图:
  4. melted_df <- reshape2::melt(taxonFC1, id.vars = "taxon", variable.name = "timepoint", value.name = "value")
  5. ggplot(melted_df, aes(x = timepoint, y = value, fill = taxon)) +
  6. geom_bar(stat = "identity") +
  7. facet_wrap(~ taxon, ncol = 3) +
  8. labs(title = "不同时间序列的条形图",
  9. x = "时间点",
  10. y = "数值",
  11. fill = "类别")
  12. 问题是是否可以为每个时间序列分配标准误差(se列)到其对数FC值(lfc列)。
  13. 更新:
  14. 我已经为T0做到了:
  15. ggplot(data = taxonFC1, aes(x = taxon, y = lfc_StatusChronic.ACST0., fill = taxon)) +
  16. geom_bar(stat = "identity") +
  17. theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  18. xlab("类别") +
  19. ylab("lfc_StatusChronic.ACST0.") +
  20. ggtitle("按类别绘制的lfc_StatusChronic.ACST0.条形图") +
  21. # 使用se_StatusChronic.ACST0.列添加误差线
  22. geom_errorbar(aes(ymin = lfc_StatusChronic.ACST0. - se_StatusChronic.ACST0.,
  23. ymax = lfc_StatusChronic.ACST0. + se_StatusChronic.ACST0.),
  24. width = 0.4)

希望这可以帮助你绘制所需的图表。

英文:

I have this dataframe:

  1. structure(list(taxon = c(&quot;Acidaminococcus&quot;, &quot;Butyricicoccus&quot;,
  2. &quot;Butyrivibrio&quot;, &quot;Collinsella&quot;, &quot;Coprococcus&quot;, &quot;Olsenella&quot;, &quot;Parabacteroides&quot;,
  3. &quot;Paraprevotella&quot;, &quot;Pasteurellaceae_unclassified&quot;), lfc_StatusChronic.ACST0. = c(0.88175957,
  4. 0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253,
  5. -0.833877215, -1.106098414, 0.932218695), se_StatusChronic.ACST0. = c(0.439259504,
  6. 0.449835605, 0.369268494, 0.391714918, 0.27578621, 0.364036816,
  7. 0.377314959, 0.485078744, 0.421283473), lfc_Time.fT1 = c(-0.021243562,
  8. 0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181,
  9. -0.439558941, -0.029316428, 0.682658747), se_Time.fT1 = c(0.312681188,
  10. 0.330173331, 0.301559494, 0.309355933, 0.293938402, 0.302957725,
  11. 0.339292487, 0.361459254, 0.385696553), lfc_Time.fT2 = c(-1.092105655,
  12. -0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087,
  13. -0.19277856, 0.148094198, 0.461233277), se_Time.fT2 = c(0.326565043,
  14. 0.344533883, 0.31544836, 0.323423323, 0.307225241, 0.317023725,
  15. 0.354270528, 0.377368442, 0.403530764), lfc_Time.fT3 = c(-0.684898535,
  16. 0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069,
  17. -0.428338824, 0.072377208, 0.682615791), se_Time.fT3 = c(0.324919486,
  18. 0.342422134, 0.314578177, 0.322254678, 0.305999846, 0.316331693,
  19. 0.352370636, 0.375283079, 0.402530027), lfc_Time.fT4 = c(-1.038613852,
  20. -0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665,
  21. 0.073495083, 0.295212326, 0.337145234), se_Time.fT4 = c(0.319416657,
  22. 0.336513636, 0.309526757, 0.316959694, 0.300928605, 0.311343927,
  23. 0.346365478, 0.36886735, 0.396117478), lfc_Time.fT5 = c(-0.714954683,
  24. 0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519,
  25. -0.055315775, 0.475970869, 0.160939365), se_Time.fT5 = c(0.317230276,
  26. 0.334106044, 0.307553106, 0.314893819, 0.298943665, 0.309379791,
  27. 0.343965965, 0.366296439, 0.393607858)), row.names = c(NA, -9L
  28. ), class = &quot;data.frame&quot;)

It is a dataframe where each row is a category, and the columns correspond with a time series (from T0 til T5).

I want to do a bar chart for each category (taxon) for their time (T0-T5):

  1. melted_df &lt;- reshape2::melt(taxonFC1, id.vars = &quot;taxon&quot;, variable.name = &quot;timepoint&quot;, value.name = &quot;value&quot;)
  2. ggplot(melted_df, aes(x = timepoint, y = value, fill = taxon)) +
  3. geom_bar(stat = &quot;identity&quot;) +
  4. facet_wrap(~ taxon, ncol = 3) +
  5. labs(title = &quot;Bar Chart for Different Time Series&quot;,
  6. x = &quot;Time Point&quot;,
  7. y = &quot;Value&quot;,
  8. fill = &quot;Category&quot;)

添加标准误差以修正分面条形图上的面板

The question is if it is possible to assign the standard error (se columns) to their logFC value (lfc columns) for each time series.

Update:

I did this, but only for T0:

  1. ggplot(data = taxonFC1, aes(x = taxon, y = lfc_StatusChronic.ACST0., fill = taxon)) +
  2. geom_bar(stat = &quot;identity&quot;) +
  3. theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  4. xlab(&quot;Category&quot;) +
  5. ylab(&quot;lfc_StatusChronic.ACST0.&quot;) +
  6. ggtitle(&quot;Bar Plot of lfc_StatusChronic.ACST0. by Category&quot;) +
  7. # Add error bars using se_StatusChronic.ACST0. column
  8. geom_errorbar(aes(ymin = lfc_StatusChronic.ACST0. - se_StatusChronic.ACST0.,
  9. ymax = lfc_StatusChronic.ACST0. + se_StatusChronic.ACST0.),
  10. width = 0.4)

Output expected (the image is from other data):

添加标准误差以修正分面条形图上的面板

答案1

得分: 1

这是你要找的吗?

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. dat &lt;- structure(list(taxon = c(&quot;Acidaminococcus&quot;, &quot;Butyricicoccus&quot;,
  5. &quot;Butyrivibrio&quot;, &quot;Collinsella&quot;, &quot;Coprococcus&quot;, &quot;Olsenella&quot;, &quot;Parabacteroides&quot;,
  6. &quot;Paraprevotella&quot;, &quot;Pasteurellaceae_unclassified&quot;), lfc_StatusChronic.ACST0. = c(0.88175957,
  7. 0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253,
  8. -0.833877215, -1.106098414, 0.932218695), se_StatusChronic.ACST0. = c(0.439259504,
  9. 0.449835605, 0.369268494, 0.391714918, 0.27578621, 0.364036816,
  10. 0.377314959, 0.485078744, 0.421283473), lfc_Time.fT1 = c(-0.021243562,
  11. 0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181,
  12. -0.439558941, -0.029316428, 0.682658747), se_Time.fT1 = c(0.312681188,
  13. 0.330173331, 0.301559494, 0.309355933, 0.293938402, 0.302957725,
  14. 0.339292487, 0.361459254, 0.385696553), lfc_Time.fT2 = c(-1.092105655,
  15. -0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087,
  16. -0.192778560, 0.148094198, 0.461233277), se_Time.fT2 = c(0.326565043,
  17. 0.344533883, 0.315448360, 0.323423323, 0.307225241, 0.317023725,
  18. 0.354270528, 0.377368442, 0.403530764), lfc_Time.fT3 = c(-0.684898535,
  19. 0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069,
  20. -0.428338824, 0.072377208, 0.682615791), se_Time.fT3 = c(0.324919486,
  21. 0.342422134, 0.314578177, 0.322254678, 0.305999846, 0.316331693,
  22. 0.352370636, 0.375283079, 0.402530027), lfc_Time.fT4 = c(-1.038613852,
  23. -0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665,
  24. 0.073495083, 0.295212326, 0.337145234), se_Time.fT4 = c(0.319416657,
  25. 0.336513636, 0.309526757, 0.316959694, 0.300928605, 0.311343927,
  26. 0.346365478, 0.368867350, 0.396117478), lfc_Time.fT5 = c(-0.714954683,
  27. 0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519,
  28. -0.055315775, 0.475970869, 0.160939365), se_Time.fT5 = c(0.317230276,
  29. 0.334106044, 0.307553106, 0.314893819, 0.298943665, 0.309379791,
  30. 0.343965965, 0.366296439, 0.393607858)), row.names = c(NA, -9L
  31. ), class = &quot;data.frame&quot;)
  1. dat %&gt;%
  2. rename(lfc_time.fT0 = lfc_StatusChronic.ACST0.,
  3. se_Time.fT0 = se_StatusChronic.ACST0.) %&gt;%
  4. pivot_longer(-taxon, names_pattern=&quot;(.*)_[Tt]ime\\.f(.*)&quot;,
  5. names_to = c(&quot;.value&quot;, &quot;time&quot;)) %&gt;%
  6. ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = taxon)) +
  7. geom_bar(stat = &quot;identity&quot;) +
  8. geom_errorbar(width=.4) +
  9. theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  10. scale_fill_brewer(palette=&quot;Set1&quot;) +
  11. xlab(&quot;Category&quot;) +
  12. ylab(&quot;lfc_StatusChronic.ACST0.&quot;) +
  13. facet_wrap(~taxon, ncol=1) +
  14. ggtitle(&quot;Bar Plot of lfc_StatusChronic.ACST0. by Category&quot;)

添加标准误差以修正分面条形图上的面板

如果是的话,关键是将T0变量重命名为与其他时间段变量具有相同格式,然后使用pivot_longer()将所有lfc测量放入单个列中,所有se测量放入单个列中。其余的操作是通过在时间变量上进行分面处理来完成的。 pivot_longer()文档中有一些保留多列的良好示例,特别是页面上的最后一个示例。

英文:

Is this what you're looking for?

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. dat &lt;- structure(list(taxon = c(&quot;Acidaminococcus&quot;, &quot;Butyricicoccus&quot;,
  5. &quot;Butyrivibrio&quot;, &quot;Collinsella&quot;, &quot;Coprococcus&quot;, &quot;Olsenella&quot;, &quot;Parabacteroides&quot;,
  6. &quot;Paraprevotella&quot;, &quot;Pasteurellaceae_unclassified&quot;), lfc_StatusChronic.ACST0. = c(0.88175957,
  7. 0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253,
  8. -0.833877215, -1.106098414, 0.932218695), se_StatusChronic.ACST0. = c(0.439259504,
  9. 0.449835605, 0.369268494, 0.391714918, 0.27578621, 0.364036816,
  10. 0.377314959, 0.485078744, 0.421283473), lfc_Time.fT1 = c(-0.021243562,
  11. 0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181,
  12. -0.439558941, -0.029316428, 0.682658747), se_Time.fT1 = c(0.312681188,
  13. 0.330173331, 0.301559494, 0.309355933, 0.293938402, 0.302957725,
  14. 0.339292487, 0.361459254, 0.385696553), lfc_Time.fT2 = c(-1.092105655,
  15. -0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087,
  16. -0.19277856, 0.148094198, 0.461233277), se_Time.fT2 = c(0.326565043,
  17. 0.344533883, 0.31544836, 0.323423323, 0.307225241, 0.317023725,
  18. 0.354270528, 0.377368442, 0.403530764), lfc_Time.fT3 = c(-0.684898535,
  19. 0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069,
  20. -0.428338824, 0.072377208, 0.682615791), se_Time.fT3 = c(0.324919486,
  21. 0.342422134, 0.314578177, 0.322254678, 0.305999846, 0.316331693,
  22. 0.352370636, 0.375283079, 0.402530027), lfc_Time.fT4 = c(-1.038613852,
  23. -0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665,
  24. 0.073495083, 0.295212326, 0.337145234), se_Time.fT4 = c(0.319416657,
  25. 0.336513636, 0.309526757, 0.316959694, 0.300928605, 0.311343927,
  26. 0.346365478, 0.36886735, 0.396117478), lfc_Time.fT5 = c(-0.714954683,
  27. 0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519,
  28. -0.055315775, 0.475970869, 0.160939365), se_Time.fT5 = c(0.317230276,
  29. 0.334106044, 0.307553106, 0.314893819, 0.298943665, 0.309379791,
  30. 0.343965965, 0.366296439, 0.393607858)), row.names = c(NA, -9L
  31. ), class = &quot;data.frame&quot;)
  1. dat %&gt;%
  2. rename(lfc_time.fT0 = lfc_StatusChronic.ACST0.,
  3. se_Time.fT0 = se_StatusChronic.ACST0.) %&gt;%
  4. pivot_longer(-taxon, names_pattern=&quot;(.*)_[Tt]ime\\.f(.*)&quot;,
  5. names_to = c(&quot;.value&quot;, &quot;time&quot;)) %&gt;%
  6. ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = taxon)) +
  7. geom_bar(stat = &quot;identity&quot;) +
  8. geom_errorbar(width=.4) +
  9. theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  10. scale_fill_brewer(palette=&quot;Set1&quot;) +
  11. xlab(&quot;Category&quot;) +
  12. ylab(&quot;lfc_StatusChronic.ACST0.&quot;) +
  13. facet_wrap(~taxon, ncol=1) +
  14. ggtitle(&quot;Bar Plot of lfc_StatusChronic.ACST0. by Category&quot;)```

添加标准误差以修正分面条形图上的面板

If so, the key is to rename the T0 variables to have the same format as the other time-period variables and then use pivot_longer() to put all the lfc measures in a single column and all the se measures in a single column. The rest is accomplished with faceting on the time variable. The pivot_longer() documentation has some good examples of retaining multiple columns, see in particular the last example on the page.

huangapple
  • 本文由 发表于 2023年2月14日 21:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448531.html
匿名

发表评论

匿名网友

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

确定