Fran Braga
Fran Braga

Reputation: 197

barplot with density curve

I am trying to generate a bar graph with a density curve that shows the bar trends, see the example in the figure 1 (I criated this example by power point).

However, in the script I wrote, I couldn't plot the curve. What appears were just lines in the middle of each bar, see the figure 2.

Would anyone here know how to improve my script?

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

Upvotes: 3

Views: 4037

Answers (1)

Mojoesque
Mojoesque

Reputation: 1318

Maybe something like this? Only the geom_density() call was changed. The curves are not smooth, but it's close to what you were looking for. To get them smooth you probably need continuous data (see the comment by 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))

Which should yield something similar to this (generated from different data):enter image description here

Upvotes: 1

Related Questions