根据数值填充曲线下面积 [英] Filling area under curve based on value

查看:132
本文介绍了根据数值填充曲线下面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图用ggplot2做一个区域图,其中x轴上的正面区域是一种颜色,而负区域是另一种颜色。给定这些数据设置,我想要一个区域图来在轴的每一边加上不同的颜色。



我可以看到一种将数据集分成两个子集的方法,一个是正的所有负值都为零,一个为负值,所有正值都为零,然后将这些值分别绘制在同一轴上,但似乎会有更像ggplot的方式来执行此操作。

解决方案发布在应用于我们的数据集时,会产生与零交叉相关的伪像: https://i.stack.imgur.com/lZQfr.pngalt =Ribbon RLE plot>



由以下代码生成( do

 #设置分组函数
rle.grp< - function(x ){
xx< - rle(x)
xx $ values = seq_along(xx $ values)
inverse.rle(xx)}

# (a,b,b,b,c)(a,b,b,b,c) y),ymin = pmin(0,y),
fill = factor(sign(y),levels = c(-1,0,1),labels = c(' - ','0',' +')))
+ scale_fill_brewer(name ='sign',palette ='RdBu')

请参阅@baptiste和Kohske建议的最终答案。

会说这个是最好的答案。它基于Kohske的这篇文章。它在零交叉处向数据集添加新的xy对,并生成如下图:

pre $ #创建一些假数据, (1,2,1,2,2,2)长度(yvals)),y = yvals)

rx < - do.call(rbind,
sapply(1:(nrow(d)-1)),function ){
f < - lm(x〜y,d [i:(i + 1),])
if(f $ qr $ rank <2)return(NULL)
r < - 预测(f,newdata = data.frame(y = 0))
如果(d [i,] $ x return(data.frame(x = r,y = 0))
else return(NULL)
}))
d2 < - rbind(d,rx)
ggplot(d2,aes(x,y))+ geom_area(data = subset(d2,y≤0),fill =pink)
+ geom_area(data = subset(d2,y> = 0),fill =lightblue)+ geom_point()

生成以下输出:


We are trying to make an area plot with ggplot2 where the positive areas above the x-axis are one color and the negative areas are another.

Given this data set, I would like an area graph to shaded different colors on each side of the axis.

I can see a way to divide the dataset into two subsets, one positive where all negative values are zero, and one negative with all positive values of zero, and then plot these separately on the same axis, but it seems like there would be a more ggplot-like way to do it.

The solution posted at this question does not give accurate results (see below).

Example data shown accurately as a bar plot

Generated by this code:

# create some fake data with zero-crossings
yvals=c(2,2,-1,2,2,2,0,-1,-2,2,-2)
test = data.frame(x=seq(1,length(yvals)),y=yvals)

# generate the bar plot
ggplot(data=test,aes(x=x,y=y)) 
    + geom_bar(data=test[test$y>0,],aes(y=y), fill="blue",stat="identity", width=.5) 
    + geom_bar(data=test[test$y<0,],aes(y=y), fill="red",stat="identity", width=.5)

RLE Approach is not General

The RLE approach proposed on the other question produces artifacts related to zero-crossings when applied to our data set:

Generated by the following code (do not use):

# set up grouping function
rle.grp <- function(x) {
   xx <- rle(x)
   xx$values = seq_along(xx$values)
   inverse.rle(xx) }

# generate ribbon plot
ggplot(test, aes(x=x,y=y,group = factor(rle.grp(sign(y))))) + 
    geom_ribbon(aes(ymax = pmax(0,y),ymin = pmin(0,y),
   fill = factor(sign(y), levels = c(-1,0,1), labels = c('-','0','+')))) 
   + scale_fill_brewer(name = 'sign', palette = 'RdBu')

See ultimate answer below as suggested by @baptiste and Kohske.

解决方案

Per @baptiste's comment (since deleted) I would say this is the best answer. It is based on this post by Kohske. It adds new x-y pairs to the dataset at zero crossings, and generates the plot below:

# create some fake data with zero-crossings
yvals = c(2,2,-1,2,2,2,0,-1,-2,2,-2)
d = data.frame(x=seq(1,length(yvals)),y=yvals)

rx <- do.call("rbind",
   sapply(1:(nrow(d)-1), function(i){
   f <- lm(x~y, d[i:(i+1),])
   if (f$qr$rank < 2) return(NULL)
   r <- predict(f, newdata=data.frame(y=0))
   if(d[i,]$x < r & r < d[i+1,]$x)
      return(data.frame(x=r,y=0))
    else return(NULL)
 }))
 d2 <- rbind(d,rx)
 ggplot(d2,aes(x,y)) + geom_area(data=subset(d2, y<=0), fill="pink") 
     + geom_area(data=subset(d2, y>=0), fill="lightblue") + geom_point()

Generates the following output:

这篇关于根据数值填充曲线下面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆