How to create a grid with ggplot?

I read the examples given on this page, but for some reason could not find the right way to do this.

I have several such data:

Group Member Percentage [1,] "1" "A" "60" [2,] "1" "A" "20" [3,] "1" "A" "20" [4,] "1" "B" "80" [5,] "1" "B" "5" [6,] "1" "B" "5" [7,] "1" "B" "5" [8,] "2" "C" "50" [9,] "2" "C" "50" [10,] "2" "D" "25" [11,] "2" "D" "25" [12,] "2" "D" "25" [13,] "2" "D" "20" [14,] "2" "D" "5" 

and can be created using the following commands:

 a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2) b = c("A","A","A","B","B","B","B","C","C","D","D","D","D","D") c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5) dat = data.frame(Group=a, Member=b, Percentage=c) ggplot(dat, aes(x=Member, y=Percentage)) + geom_bar(stat="identity", position="dodge", fill="white", colour="black") 

The last line gives me the following graph:

enter image description here

What I'm really looking for is to combine each of the bars in the same group with one line and present the percentages as a fraction of the same bar (where each member from each group is depicted with one bar with each bar having percentages like their colors). So I used the following:

 ggplot(dat, aes(x=Member, y=Percentage)) + geom_bar(stat="identity", colour="white") 

and got this:

enter image description here

But now I canโ€™t get the colors right. I want something similar, as shown below, but I cannot figure out how to do this. Any suggestions on how to do this?

enter image description here

+8
r statistics ggplot2
source share
2 answers

Good, finally understood! Hooray! Here's the full code if anyone is interested:

 a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2) b = c("A","A","A","B","B","B","B","C","C","D","D","D","D","D") c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5) dat = data.frame(Group=a, Member=b, Percentage=c) ggplot(dat, aes(x=Member, y=Percentage, fill=Percentage)) + geom_bar(stat="identity", colour="white") 

enter image description here

and with what @joran suggested (Thanks a lot!) in the comments:

 ggplot(dat, aes(x=Member, y=Percentage, fill=Percentage)) + geom_bar(stat="identity", colour="white") 

enter image description here

+9
source share

You're close Try

 ggplot(dat, aes(x=Member, y=Percentage, fill = factor(Percentage))) + geom_bar(stat = "identity") 

What produces

enter image description here

+4
source share

All Articles