r - ggplot2 - 在两个geom_abline图层之间创建一个阴影区域 [英] r - ggplot2 - create a shaded region between two geom_abline layers
问题描述
有一个少数 posts 有关使用ggplot2中的阴影区域,但我不认为任何回答我的问题。我有两条斜线用于满足多种条件的线条,我想遮蔽它们之间的区域。以下是示例数据:
dat < - data.frame(cond1 = c(a,a, (b),b),
cond2 = c(c,d,c,d),
x = c(1,5),
y = c(1,5),
sl = c(1,1.2,0.9,1.1),
int = c(0,0.1,0.1,0),
slopeU = c(1.1 ,1.3,1.2,1.2),
slopeL = c(.9,1,0.7,1))
这里, sl
是一个独立拟合过程的平均斜率参数, slopeU
和 slopeL
表示每个条件下斜率估计值的上置信区域和下置信区域。拦截限制为相同。下面的代码使用一些分面绘制每个条件的最佳拟合线:
p < - ggplot(dat,aes(x = x,y = y,color = cond1))
p <-p + facet_grid(。〜cond2)
p <-p + geom_blank()
p <-p + geom_abline( aes(intercept = int,slope = sl,color = cond1),data = dat)
p
我想添加由 我认识到,通过一点操作,我可以创建一个数据框,为至少两个x值指定这些行的值,然后绘制相应的geom_ribbon或geom_polygon以创建阴影区域,但是我希望找到更优雅的解。或者是从斜坡上手动指定一些坐标并拦截唯一的方法?我将如何最好地创建所需的数据框(它将需要比原始框架更多的行来计算条件和x,y对的所有组合)。 但是,如果您充分利用 plyr 和 ggplot ,事情会变得非常光滑。既然你的斜率和截距都很好地存储在数据框中,我们可以使用 plyr 和一个自定义函数来完成所有工作: 这里额外的流畅的东西是我完全丢弃了生成的数据帧,只是返回一个 我覆盖 我会提醒你最后一行产生情节也引发了很多关于无效因素水平的警告,而我老实说不知道为什么。但情节看起来不错。 There are a few posts regarding the use of shaded areas in ggplot2, but I don't think any exactly answer my question. I have two slopes for lines across a number of conditions, and I would like to shade the region between them. Here is example data: Here, I would like to add the lines defined by I recognise that with a little manipulation I could create a data frame specifying values of these lines for at least two x values, then plot the corresponding geom_ribbon or geom_polygon to create the shaded region, however I would like to find a more elegant solution. Or is manually specifying some coordinates from the slopes and intercepts the only way? How would I best create the required data frame (which will need to have more rows than the original frame to account for all combinations of conditions and x,y pairs). Personally, I think that creating the data frames and using But if you take full advantage of plyr and ggplot things can get pretty slick. Since your slopes and intercepts are all nicely stored in a dataframe anyway, we can use plyr and a custom function to do all the work: The extra slick thing here is that I'm discarding the generated data frames completely and just returning a list of I overrode the I'll warn you that the last line there that generates the plots also throws a lot of warnings about invalid factor levels, and I'm honestly not sure why. But the plot looks ok. 这篇关于r - ggplot2 - 在两个geom_abline图层之间创建一个阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! intercept = int,slope = slopeU
和 intercept = int定义的行,slope = slopeL
绘制阴影并遮住它们之间的区域(例如,在相应的cond1颜色中
<$ p $ c $ dat> data.frame(cond1 = c(a,a,b,b),
cond2 = c(c, d,c,d),
x = c(1,5),
y = c(1,5),
sl = c(1,1.2,0.9 ,1.1),
int = c(0,0.1,0.1,0),
slopeU = c(1.1,1.3,1.2,1.2),
slopeL = c(.9,1 ,0.7,1))
genRibbon< - 函数(param,xrng){
#xrng是原始数据中最小/最大x vals的向量
r< - (xrng [1] - 0.05 * r,xrng [2] + 0.05 * r,length.out = 3)abs(diff(xrng))
#adj绘图区域扩展
x < - seq
#create data frame
res< - data.frame(cond1 = param $ cond1,
cond2 = param $ cond2,
x = x,
y = param $ int + param $ sl * x,
ymin = param $ int + param $ slopeL * x,
ymax = param $ int + param $ slopeU * x)
#抛开min / max x vals只是为了安全;需要它们
#才能得到相应的y值
res $ x [which.min(res $ x)]< - -Inf
res $ x [which.max(res $ x = y,ymin = ymin,ymax = ymax,
fill = cond1,color = NULL),
alpha = 0.5)
}
rib <-dlply(dat,。(cond1,cond2),genRibbon,xrng = c ,5))
geom_ribbon
对象的列表。然后,他们可以简单地添加到我们的情节:
p +肋骨+
指南(fill = guide_legend(覆盖。 aes = list(alpha = 0.1)))
/ code>审美在传说中,因为你第一次看不到图例中的对角线。
$ b
dat <- data.frame(cond1=c("a","a","b","b"),
cond2=c("c","d","c","d"),
x=c(1,5),
y=c(1,5),
sl=c(1,1.2,0.9,1.1),
int=c(0,0.1,0.1,0),
slopeU=c(1.1,1.3,1.2,1.2),
slopeL=c(.9,1,0.7,1))
sl
is the mean slope parameter from a separate fitting procedure, and slopeU
and slopeL
represent upper and lower confidence regions on the slope estimate in each condition. The intercepts are constrained to be the same. The following code plots the best fitting lines for each condition using some faceting:p <- ggplot(dat,aes(x=x,y=y,colour=cond1))
p <- p + facet_grid(. ~ cond2)
p <- p + geom_blank()
p <- p + geom_abline(aes(intercept=int,slope=sl,colour=cond1),data=dat)
p
intercept=int, slope=slopeU
and intercept=int, slope=slopeL
to the plot and shade the region between them (e.g. at alpha=.5
in the corresponding cond1 colour).geom_ribbon
is the elegant solution, but obviously opinions will differ on that score.dat <- data.frame(cond1=c("a","a","b","b"),
cond2=c("c","d","c","d"),
x=c(1,5),
y=c(1,5),
sl=c(1,1.2,0.9,1.1),
int=c(0,0.1,0.1,0),
slopeU=c(1.1,1.3,1.2,1.2),
slopeL=c(.9,1,0.7,1))
genRibbon <- function(param,xrng){
#xrng is a vector of min/max x vals in original data
r <- abs(diff(xrng))
#adj for plot region expansion
x <- seq(xrng[1] - 0.05*r,xrng[2] + 0.05*r,length.out = 3)
#create data frame
res <- data.frame(cond1 = param$cond1,
cond2 = param$cond2,
x = x,
y = param$int + param$sl * x,
ymin = param$int + param$slopeL * x,
ymax = param$int + param$slopeU * x)
#Toss the min/max x vals just to be safe; needed them
# only to get the corresponding y vals
res$x[which.min(res$x)] <- -Inf
res$x[which.max(res$x)] <- Inf
#Return the correspondinng geom_ribbon
geom_ribbon(data = res,aes(x = x,y=y, ymin = ymin,ymax = ymax,
fill = cond1,colour = NULL),
alpha = 0.5)
}
ribs <- dlply(dat,.(cond1,cond2),genRibbon,xrng = c(1,5))
geom_ribbon
objects. Then they can simply be added to our plot:p + ribs +
guides(fill = guide_legend(override.aes = list(alpha = 0.1)))
alpha
aesthetic in the legend because the first time around you couldn't see the diagonal lines in the legend.