在R中使用ggplot2进行堆积面积图的图案填充

时间:2018-08-16 17:43:26

标签: r ggplot2 plot

我正在尝试开发一种颜色和图案组合的堆叠区域图。我知道R在图案填充方面有些局限。我尝试查找,但只能找到patternplot,仅限于条形图和饼图。任何帮助深表感谢!

有关数据的详细信息: P1,P2,P3和P4给出了四个阶段。每个阶段后面都带有一个间隙(C)。 y轴是概率(y轴)  在x轴上的一个时间段(0到5秒)内,每个相位的相对角及其相对应的间隙。这些阶段将为纯色,而间隙将为45度哈希图案。间隙图案的颜色遵循相应的相位。例如,在下图中,P1为纯灰色,因此间隙P1_C和将以灰色散列,依此类推。

数据和当前进度:

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()

当前,我已将间隙设置为NA,但最终,我希望最终绘图如下所示。谢谢!

Required Plot

0 个答案:

没有答案