I'm trying to plot a stacked bar where the order of the levels is defined by a column and the color is defined by another one.
I read a csv file and the dataframe has the following columns:
- Method (character)
- Property (character)
- Order (integer)
- Success (double): Success is the axis "y" and Method is the axis "x".
Data
A subset of my dataframe is:
Method <- c("MF", "MF", "MF", "MF", "MF", "MF", "RF", "RF", "RF", "RF", "RF", "RF")
Property <- c("P1","P2","P3","P6","P5","P7","P1","P6","P2","P5","P4","P7")
Order <- c(1,2,3,4,5,6,1,2,3,4,5,6)
Success <- c(87.612,4.583,0.286,6.122,0.788,0.573,87.612,6.409,4.332,0.895,0.0,0.573)
REF01 <- data.frame(Method, Property, Order, Success)
Success is an incremental column following the Order column. For example, the Success for method RF for the first 3 Properties (P1, P6 and P2) is (87.612 + 6.409 + 4.332).
Desired Result
I need to stack Success for each Method based on the Order column but I would like to color based on the Property column. I draw manually in Excel what I would like to see for this small example.
The picture below shows that the blue one (P1) is the first property for both methods. The second property is different on each method. P2 (brown) is the second one for the MF and the third one for RF, while the P6 (yellow) is the second one for RF and the fourth for MF. So, the MFbar color follows: blue, brown, gray (a property that doesn't exist in RF, yellow and dark blue. The RF bar color follows: blue, yellow, brown, blue and green)
Is there a way to plot this? I appreciate your help.
I also forgot to write I'm using ggplot2
, but I know that the code is simple.
p <- ggplot(REF01, aes(x=Method, y=Success)) +
geom_bar(aes(fill = Property) , stat = "identity")