4

I'm trying to develop a stacked area plot with a combination of colors and patterns. I know R is a bit limited when it comes to pattern fills. I tried looking up, but could only find patternplot which is limited to bar and pie charts. Any help is much appreciated!

Details on the data: There are four phases given by P1, P2, P3 and P4. Each phase is followed by a clearance (C). The y-axis is the probability (y-axis) of each phase and its corresponding clearances across a time (0 to 5 seconds) on the x-axis. The phases will be in solid color, whereas the clearance will be a 45 degree hash pattern. The color of the clearance pattern follows the corresponding phase. For example, in below fig, P1 is solid gray, so the clearances P1_C and will hashed with gray color and so on.

Data and current progress:

mydat= data.frame("1"=c(1,0,0,0,0,0,0,0), "2"=c(0,0.9,0.1,0,0,0,0,0),
"3"=c(0,0,1,0,0,0,0,0), "4"=c(0,0,0,0.6,0.25,0.15,0,0),
"5"=c(0,0,0,0,0,0.25,0.75,0), "6"=c(0.1,0,0,0,0,0,0,0.9))
rownames(mydat)=c("P1","P1_C","P2","P2_C","P3","P3_C","P4","P4_C")
colnames(mydat)=seq(0,5,1)
mydat=t(mydat)
mydat=mydat[,c(8,7,6,5,4,3,2,1)]
melted = melt(mydat, 1)
colnames(melted)=c("time","Phase","Probability")

ggplot(melted, aes(x = time, y = Probability, fill=Phase)) +
 geom_area()+
 scale_fill_manual(values=c("NA","deepskyblue","NA","darkblue","NA","lightgreen","NA","gray"))+
 scale_x_continuous("Time",limits=c(0,5), breaks = c(seq(0,5,1)),expand = c(0,0)) +
 scale_y_continuous("Probability", breaks = c(seq(0,1,0.1)), expand = c(0,0)) +
 coord_cartesian(ylim = c(0,1))+
 theme_bw()

Currently, I have the clearance set to NA, but eventually I'd like the final plot to be as shown below. Thanks!

Required Plot

nick
  • 1,090
  • 1
  • 11
  • 24
bhwak
  • 41
  • 1
  • 4

0 Answers0