R语言 包含重复数据的条形图和点图

hec6srdp  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(90)

所以我有这样的数据:
数据=
| 群|条件|价值|来源|
| - -----|- -----|- -----|- -----|
| 一个|健康|1.000000|模拟|
| B级|健康|1.000000|模拟|
| C类|健康|1.000000|模拟|
| 一个|疾病|0.4589925|模拟|
| B级|疾病|1.4905422|模拟|
| C类|疾病|1.3422035|模拟|
| 一个|健康|1.000000|实验|
| B级|健康|1.000000|实验|
| C类|健康|1.000000|实验|
| 一个|疾病|0.5158371|实验|
| 一个|疾病|0.7559055|实验|
| B级|疾病|1.4153005|实验|
| B级|疾病|1.400万|实验|
| C类|疾病|1.3300971|实验|
| C类|疾病|1.000000|实验|
我想做一个叠加的情节。沿X轴的是组(3),沿Y轴的是值。每组应该有两个条形图(一个用于健康,一个用于疾病)。除此之外,我还试图用我的实验值覆盖点图。
当每个条件只有一个Experimental值时,我已经成功地做到了这一点,使用:
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)
然而,现在有重复这种方法是行不通的。
提前感谢您的任何帮助!

pkmbmrz7

pkmbmrz71#

在这种情况下,我建议将数据子集化,而不是旋转:

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)

相关问题