用统一的彩色表面替换颜色点

Replacing points of color by a uniform colored surface

这是我的数据和我当前的情节

require(ggplot2)
a = rep(c(2,5,10,15,20,30,40,50,75,100), each=7)
b = rep(c(0.001,0.005,0.01,0.05,0.5,5,50), 10)
c = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE)
dt = data.frame(a=a,b=b,c=c)
ggplot(dt, aes(x=a, y=b, color=c)) + geom_point() + scale_y_log10()

我希望背景为蓝色和橙色,而不是上面的蓝色和橙色点。边界可以是直线或一些 LOESS 线或更容易实现的任何东西(我认为一些平滑的线会更花哨)!这对我来说听起来像是一个难题。我欢迎我提出的解决方案的变体,只要它看起来不错!

你能帮我吗?谢谢。

你可以试试这个,这个想法是为每个组找到将在两个区域之间的点,然后取这两个点的中间并得到一条黄土线作为边界:

library(dplyr)
#make column c numeric and order the dataframe 
dt$c<-dt$c*1
dt<-dt[order(a,c),]

#get all the points that are where the change of "region" happens 
#here it is where the c variable switches from 0 to 1, since dt is ordered
#by a and c, you can just find the first 1 and take that point and the one 
#before

get_group_change<-function(x){
  idx<-min(which(x[,"c"]==1))
  x[c(idx-1,idx),]
}

boundary_points<-dt %>% group_by(a) %>% do(get_group_change(.))

#get the point in the middle of the boundary points
get_middle<-function(x){exp(mean(log(x)))}

middle_points<-boundary_points %>% group_by(a) %>% summarise_each(funs(get_middle),a,b)
middle_points$c<-2

#make a boundary data frame with a LOESS prediction for b
boundary<-data.frame(a=2:100,b=exp(predict(loess(log(b)~a,middle_points),2:100)),c=2)


#plot the regions, the middle_points are also plotted 
ggplot(rbind(dt,middle_points), aes(x=a, y=b, color=as.factor(c))) + geom_point() + scale_y_log10()+
  geom_ribbon(data=boundary,aes(ymin=min(dt$b),ymax=b),alpha=0.1,fill="red",colour=NA)+
  geom_ribbon(data=boundary,aes(ymin=b,ymax=max(dt$b)),alpha=0.1,fill="green",colour=NA)

我得到这样的结果:

或者用直线作为边界:

ggplot(rbind(dt,middle_points), aes(x=a, y=b, color=as.factor(c))) + geom_point() + scale_y_log10()+
  geom_ribbon(data=middle_points,aes(ymin=min(dt$b),ymax=b),alpha=0.1,fill="red",colour=NA)+
  geom_ribbon(data=middle_points,aes(ymin=b,ymax=max(dt$b)),alpha=0.1,fill="green",colour=NA)

如果点没有离散 b...

则不可能