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

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

Bar and point plot with repeat data

问题

这是您要翻译的内容:

我有以下形式的数据:

data = 

||条件    |数值       |来源        |
|------|---------|-----------|------------|
| A    |健康     |1.0000000  |模拟        |
| B    |健康     |1.0000000  |模拟        |
| C    |健康     |1.0000000  |模拟        |
| A    |疾病     |0.4589925  |模拟        |
| B    |疾病     |1.4905422  |模拟        |
| C    |疾病     |1.3422035  |模拟        |
| A    |健康     |1.0000000  |实验        |
| B    |健康     |1.0000000  |实验        |
| C    |健康     |1.0000000  |实验        |
| A    |疾病     |0.5158371  |实验        |
| A    |疾病     |0.7559055  |实验        |
| B    |疾病     |1.4153005  |实验        |
| B    |疾病     |1.4000000  |实验        |
| C    |疾病     |1.3300971  |实验        |
| C    |疾病     |1.0000000  |实验        |

我想制作一个堆叠图。沿X轴是组(3个),沿Y轴是值。每个组应该有两个条(一个用于健康,一个用于疾病)。此外,我尝试在上面叠加一个点图,显示我的实验值。

当每个条件只有一个实验值时,我成功地使用以下方法做到了:

`pivoted <- data %>%
    pivot_wider(names_from = Source)`

`ggplot(pivoted, aes(x = group, fill = condition)) +
    geom_col(aes(y = Simulated), position = position_dodge()) +
    labs(x = "组", y = "数值") +
    geom_point(aes(y = Experimental), position = position_dodge(width = 0.9), size = 3)`

然而,现在有重复的情况,这种方法不起作用。

非常感谢您的帮助!

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

英文:

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

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

ggplot(data, aes(x = group, y = value, fill = condition)) +
  geom_col(data = filter(data, Source == "Simulated"),
           position = position_dodge()) +
  geom_point(
    data = filter(data, Source == "Experimental"),
    aes(group = condition),
    position = position_dodge(width = 1)
  )

使用此数据:

data = read.table(text = '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', header = T)
英文:

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

ggplot(data, aes(x = group, y = value, fill = condition)) +
  geom_col(data = filter(data, Source == &quot;Simulated&quot;),
           position = position_dodge()) +
  geom_point(
    data = filter(data, Source == &quot;Experimental&quot;),
    aes(group = condition),
    position = position_dodge(width = 1)
  )

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


Using this data

data = read.table(text = &#39;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&#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:

确定