条形图和点图,带有重复数据。

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

Bar and point plot with repeat data

问题

这是您要翻译的内容:

  1. 我有以下形式的数据:
  2. data =
  3. | |条件 |数值 |来源 |
  4. |------|---------|-----------|------------|
  5. | A |健康 |1.0000000 |模拟 |
  6. | B |健康 |1.0000000 |模拟 |
  7. | C |健康 |1.0000000 |模拟 |
  8. | A |疾病 |0.4589925 |模拟 |
  9. | B |疾病 |1.4905422 |模拟 |
  10. | C |疾病 |1.3422035 |模拟 |
  11. | A |健康 |1.0000000 |实验 |
  12. | B |健康 |1.0000000 |实验 |
  13. | C |健康 |1.0000000 |实验 |
  14. | A |疾病 |0.5158371 |实验 |
  15. | A |疾病 |0.7559055 |实验 |
  16. | B |疾病 |1.4153005 |实验 |
  17. | B |疾病 |1.4000000 |实验 |
  18. | C |疾病 |1.3300971 |实验 |
  19. | C |疾病 |1.0000000 |实验 |
  20. 我想制作一个堆叠图。沿X轴是组(3个),沿Y轴是值。每个组应该有两个条(一个用于健康,一个用于疾病)。此外,我尝试在上面叠加一个点图,显示我的实验值。
  21. 当每个条件只有一个实验值时,我成功地使用以下方法做到了:
  22. `pivoted <- data %>%
  23. pivot_wider(names_from = Source)`
  24. `ggplot(pivoted, aes(x = group, fill = condition)) +
  25. geom_col(aes(y = Simulated), position = position_dodge()) +
  26. labs(x = "组", y = "数值") +
  27. geom_point(aes(y = Experimental), position = position_dodge(width = 0.9), size = 3)`
  28. 然而,现在有重复的情况,这种方法不起作用。
  29. 非常感谢您的帮助!

请注意,上述内容已经被翻译成中文,只包括代码部分的英文内容。

英文:

So I have data of the form:

data =

group condition value Source
A Health 1.0000000 Simulated
B Health 1.0000000 Simulated
C Health 1.0000000 Simulated
A Disease 0.4589925 Simulated
B Disease 1.4905422 Simulated
C Disease 1.3422035 Simulated
A Health 1.0000000 Experimental
B Health 1.0000000 Experimental
C Health 1.0000000 Experimental
A Disease 0.5158371 Experimental
A Disease 0.7559055 Experimental
B Disease 1.4153005 Experimental
B Disease 1.4000000 Experimental
C Disease 1.3300971 Experimental
C Disease 1.0000000 Experimental

I wish to make a stacked plot. Along the X axis is Group (3) and along the Y axis is the value. Each group should have two bars (one for health, and one for disease). On top of this, I am trying to overlay a point plot with my experimental values.

I have managed to do this when there is only one Experimental value for each condition using:

pivoted&lt;-data %&gt;% pivot_wider(names_from =Source).

ggplot(pivoted, aes(x=group,fill=condition)) + geom_col(aes(y=Simulated), position=position_dodge()) +
labs(x=&quot;Group&quot;, y=&quot;Value&quot;) + geom_point(aes(y=Experimental), position = position_dodge(width=0.9), size=3)

However, now there are repeats this method is not working.

Thank you in advance for any help!

答案1

得分: 0

在这种情况下,我建议对您的数据进行子集选择,而不是进行数据透视:

  1. ggplot(data, aes(x = group, y = value, fill = condition)) +
  2. geom_col(data = filter(data, Source == "Simulated"),
  3. position = position_dodge()) +
  4. geom_point(
  5. data = filter(data, Source == "Experimental"),
  6. aes(group = condition),
  7. position = position_dodge(width = 1)
  8. )

使用此数据:

  1. data = read.table(text = 'group condition value Source
  2. A Health 1.0000000 Simulated
  3. B Health 1.0000000 Simulated
  4. C Health 1.0000000 Simulated
  5. A Disease 0.4589925 Simulated
  6. B Disease 1.4905422 Simulated
  7. C Disease 1.3422035 Simulated
  8. A Health 1.0000000 Experimental
  9. B Health 1.0000000 Experimental
  10. C Health 1.0000000 Experimental
  11. A Disease 0.5158371 Experimental
  12. A Disease 0.7559055 Experimental
  13. B Disease 1.4153005 Experimental
  14. B Disease 1.4000000 Experimental
  15. C Disease 1.3300971 Experimental
  16. C Disease 1.0000000 Experimental', header = T)
英文:

In this case I would suggest subsetting your data rather than pivoting:

  1. ggplot(data, aes(x = group, y = value, fill = condition)) +
  2. geom_col(data = filter(data, Source == &quot;Simulated&quot;),
  3. position = position_dodge()) +
  4. geom_point(
  5. data = filter(data, Source == &quot;Experimental&quot;),
  6. aes(group = condition),
  7. position = position_dodge(width = 1)
  8. )

条形图和点图,带有重复数据。


Using this data

  1. data = read.table(text = &#39;group condition value Source
  2. A Health 1.0000000 Simulated
  3. B Health 1.0000000 Simulated
  4. C Health 1.0000000 Simulated
  5. A Disease 0.4589925 Simulated
  6. B Disease 1.4905422 Simulated
  7. C Disease 1.3422035 Simulated
  8. A Health 1.0000000 Experimental
  9. B Health 1.0000000 Experimental
  10. C Health 1.0000000 Experimental
  11. A Disease 0.5158371 Experimental
  12. A Disease 0.7559055 Experimental
  13. B Disease 1.4153005 Experimental
  14. B Disease 1.4000000 Experimental
  15. C Disease 1.3300971 Experimental
  16. C Disease 1.0000000 Experimental&#39;, header = T)

huangapple
  • 本文由 发表于 2023年6月22日 20:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531881.html
匿名

发表评论

匿名网友

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

确定