在R中的xy曲线图内阴影 [英] shading within xy curve plot in R
本文介绍了在R中的xy曲线图内阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似以下的资料:
I have data similar to following:
X <- 1:20
B <- c(1,4,6,3,1, 4, 5,8,8,6,3,2,1, 1,5,7,8,6,4,2)
C <- B + 4
myd <- data.frame (X, B, C)
I想要在曲线内以不同的颜色进行遮蔽。请注意边界颜色填充x
I want to shade with different color within the curve. Please note the boundries color filling in x
region 1 = 1 to 6
region 2 = 6 to 16
region 3 = 16 to 20
推荐答案
这是一个使用ggplot2的解决方案。
Here is a solution with ggplot2.
library(ggplot2)
library(reshape2)
d <- melt(myd, id.vars="X")
d <- rbind(
transform( d[ 0 <= d$X & d$X <= 6, ], interval=1 ),
transform( d[ 6 <= d$X & d$X <= 16, ], interval=2 ),
transform( d[ 16 <= d$X & d$X <= 20, ], interval=3 )
)
# Set the order in which we fill the areas:
# first the large ones ("C"), then the small ones ("B");
# Otherwise, the small ones are invisible.
d$variable <- LETTERS[3-as.numeric(d$variable)] # Alphabetic order
ggplot( d, aes(X,value,fill=paste(variable,interval)) ) +
# Set the alpha to a value close to 1, to see if the order is wrong
geom_area(position="identity", alpha=.9) +
opts(legend.position = "none")
这篇关于在R中的xy曲线图内阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文