带密度曲线的条形图

时间:2018-08-22 15:19:04

标签: r ggplot2 density-plot

我正在尝试生成具有显示条形趋势的密度曲线的条形图,请参见图1中的示例(我以功率点为例对这个示例进行了注释)。

但是,在我编写的脚本中,我无法绘制曲线。出现的只是每个条中间的线,见图2。

这里有人会知道如何改善我的脚本吗?

Figure1

library(ggplot2)
ggplot(Dataset, aes(specie,M1_sava)) + 
  facet_wrap(~ momento, nrow=1) + #place the factors in separate facets
  geom_bar(stat="identity") + #make the bars
  coord_flip() + #flip the axes so the test names can be horizontal  
  theme_bw(base_size=10)+ #use a black-and0white theme with set font size
  geom_density()



specie  momento M1_sava
S1  M1  1,00
S2  M1  0,86
S3  M1  1,00
S4  M1  1,00
S5  M1  1,00
S6  M1  0,74
S7  M1  0,39
S8  M1  0,83
S9  M1  0,83
S10 M1  0,00
S11 M1  0,70
S12 M1  0,11
S13 M1  1,00
S14 M1  0,00
S15 M1  0,00
S16 M1  0,00
S17 M1  0,00
S18 M1  0,83
S19 M1  0,00
S20 M1  0,00
S21 M1  0,00
S22 M1  0,00
S23 M1  0,00
S24 M1  0,04
S25 M1  0,00
S26 M1  0,00
S1  M2  0,33
S2  M2  0,86
S3  M2  0,39
S4  M2  0,02
S5  M2  0,07
S6  M2  0,02
S7  M2  0,87
S8  M2  0,06
S9  M2  0,63
S10 M2  0,33
S11 M2  0,91
S12 M2  0,67
S13 M2  0,18
S14 M2  0,08
S15 M2  0,00
S16 M2  0,00
S17 M2  0,00
S18 M2  0,00
S19 M2  0,08
S20 M2  0,00
S21 M2  0,04
S22 M2  0,00
S23 M2  0,00
S24 M2  0,00
S25 M2  0,00
S26 M2  0,00
S1  M3  0,04
S2  M3  0,32
S3  M3  0,02
S4  M3  0,00
S5  M3  0,00
S6  M3  0,00
S7  M3  0,96
S8  M3  0,06
S9  M3  0,18
S10 M3  0,33
S11 M3  0,63
S12 M3  1,00
S13 M3  0,00
S14 M3  0,94
S15 M3  0,17
S16 M3  0,00
S17 M3  0,41
S18 M3  0,04
S19 M3  0,44
S20 M3  0,17
S21 M3  0,02
S22 M3  0,00
S23 M3  0,00
S24 M3  0,00
S25 M3  0,00
S26 M3  0,00
S1  M4  0,00
S2  M4  0,00
S3  M4  0,00
S4  M4  0,00
S5  M4  0,00
S6  M4  0,00
S7  M4  0,89
S8  M4  0,00
S9  M4  0,03
S10 M4  0,22
S11 M4  0,41
S12 M4  0,46
S13 M4  0,00
S14 M4  0,81
S15 M4  0,39
S16 M4  0,70
S17 M4  0,70
S18 M4  0,00
S19 M4  0,87
S20 M4  0,91
S21 M4  0,33
S22 M4  0,37
S23 M4  0,24
S24 M4  0,15
S25 M4  0,00
S26 M4  0,00

Figure2

1 个答案:

答案 0 :(得分:2)

也许是这样?仅geom_density()调用已更改。曲线不平滑,但与您要寻找的接近。为了使它们平滑,您可能需要连续的数据(请参见camille的注释)。

library(ggplot2)
    ggplot(Dataset, aes(specie,M1_sava)) + 
      facet_wrap(~ momento, nrow=1) + #place the factors in separate facets
      geom_bar(stat="identity") + #make the bars
      coord_flip() + #flip the axes so the test names can be horizontal  
      theme_bw(base_size=10)+ #use a black-and0white theme with set font size
      geom_density(stat = "identity", alpha = 0.3, aes(group = momento, fill = momento))

应该产生类似的结果(从不同的数据生成):enter image description here