用统一的颜色表面替换颜色点 [英] Replacing points of color by a uniform colored surface

查看:209
本文介绍了用统一的颜色表面替换颜色点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



取而代之的是上面的蓝色和橙色点,我希望背景以蓝色和橙色着色。边界可以是直线,也可以是一些黄线或者更容易实现的东西(我认为一些流畅的线条会更加花哨)!这听起来像是一个难题。只要看起来不错,我就欢迎我提出的解决方案的变体!



你能帮我解决吗?谢谢。

解决方案

你可以试试这个,这个想法是为每个组找出分离点这两个地区,然后采取这两个点的中间,并得到一个黄线作为边界:

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

#所有的点都是区域发生变化的地方
#这里它是c变量从0切换到1的地方,因为dt是a和c的顺序
#找到第一个并取得该点,并在

前获得该点和
get_group_change< -function(x){
idx< -min(which(x [,c (a)%>%1))
x [c(idx-1,idx),]
}

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

#获取边界点中间的点
get_middle< -function(x){exp(mean(log(x)))}

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

#对于b
边界<-data.frame(a = 2:100,b = exp(预测(黄土(log(b)〜a,middle_points),2:100)),c = 2)


#绘制区域,中点也绘制为
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 = NA)+
geom_ribbon(数据=边界,aes(ymin = b,ymax = max(dt $ b)),alpha = 0.1,fill =green,color = NA)

我得到这样的结果:



或者用直线表示边界:

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



不会如果积分没有离散的 b ...


Here is my data and my current plot

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

Instead of the above blue and orange points, I would like the background to be colored in blue and orange. The boundary can either be straight lines or some LOESS line or whatever is easier to implement (some smooth line would be more fancy I think)! It sounds like a difficult problem to me. I welcome variants of the solution I asked as long as it looks good!

Can you help me with that? Thank you.

解决方案

You could try this, the idea is to find the points for each group that would be at the separation of the two regions, then take the middle of these two points and get a LOESS line as boundary:

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)

I get something like this:

Or with straight lines for the boundary:

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)

Wouldn't be possible if the points did not have a discrete b...

这篇关于用统一的颜色表面替换颜色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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