英文:
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<-data %>% pivot_wider(names_from =Source)
.
ggplot(pivoted, aes(x=group,fill=condition)) + geom_col(aes(y=Simulated), position=position_dodge()) +
labs(x="Group", y="Value") + 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 == "Simulated"),
position = position_dodge()) +
geom_point(
data = filter(data, Source == "Experimental"),
aes(group = condition),
position = position_dodge(width = 1)
)
Using this data
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论