0

我有一组包含两个类别的数据。我想在 r 中绘制一个 highchater 散点图,每个类别都有自己的颜色和独立的回归线。我使用了一个 for 循环来实现不同的颜色,并计算了我的回归线的两个点。问题是,我只能在散点图上画一条回归线。我试图在循环中画线或使用 %>% 添加一条线。他们都没有工作。我也不能在这个图表上添加标题。这是我的代码。

data<-data.frame(gene=rnorm(20),metab=rnorm(20),type=sample(0:1,1000,rep=TRUE))[1:20,]]
b<-glm(data$metab~data$gene+data$type+data$gene:data$type)
coefficients<-t(b$coefficients)
i<-coefficients[,1]
g<-coefficients[,2]
c2<-coefficients[,3]
g.c2<-coefficients[,5]

u<-matrix(c(1,0))
type1<-u[1,1]
type2<-u[2,1]

largest1<- max(data$gene[data$type==type1])
largest2<-max(data$gene[data$type==type2])
min1<-min(data$gene[data$type==type1])
min2<-min(data$gene[data$type==type2])

line1<-data.frame(x=c(largest1,min1),y=c(g*largest1+i,min1*g+i))
line2<-data.frame(x=c(largest2,min2),y=c(g*largest2+g.c2*largest2+i+c2,min2*g+i+c2))

hc<-highchart(width = 800, height = 700) 
#hc_title(text = "scatterplot",
             #style = list(color = '#2E1717',fontSize = '20px',
                          #fontWeight = 'bold')) %>%
for(type in u){
       hc<-hc%>% 
        hc_add_series_scatter(data$gene[data$type==type],data$metab[data$type==type],name=sprintf("type %s", type),
                              showInLegend = TRUE) 
        #if(type==type1){                
        #   hc_add_series(hc,data=line1,type='line',name='regression line1') 
        #}else if (type==type2){                
        #   hc_add_series(hc,data=line2,type='line',name='regression line2')
        #}
} 

hc 

hc_add_series(hc,data=line1,type='line',name='regression line1',enableMouseTracking=FALSE,marker=FALSE) %>%
hc_add_series(hc,data=line2,type='line',name='regression line2',enableMouseTracking=FALSE,marker=FALSE)

line1 和 line2 是包含两条回归线的两个点的 data.frame。

4

1 回答 1

0

尝试运行您提供的代码时出现错误。第一行以]阻止代码运行或缺少其他内容结尾。

如果我删除它,那么coefficients[,5]我会收到关于索引越界的错误。通过更改为[,4].

在您使用的代码的最后 2 行中hc_add_series-hc这会触发单个更改,打开图表并且不保存到hc.

最后我使用了下面的代码:

data<-data.frame(gene=rnorm(20),metab=rnorm(20),type=sample(0:1,1000,rep=TRUE))[1:20,]]
b<-glm(data$metab~data$gene+data$type+data$gene:data$type)
coefficients<-t(b$coefficients)
i<-coefficients[,1]
g<-coefficients[,2]
c2<-coefficients[,3]
g.c2<-coefficients[,4]

u<-matrix(c(1,0))
type1<-u[1,1]
type2<-u[2,1]

largest1<- max(data$gene[data$type==type1])
largest2<-max(data$gene[data$type==type2])
min1<-min(data$gene[data$type==type1])
min2<-min(data$gene[data$type==type2])

line1<-data.frame(x=c(largest1,min1),y=c(g*largest1+i,min1*g+i))
line2<-data.frame(x=c(largest2,min2),y=c(g*largest2+g.c2*largest2+i+c2,min2*g+i+c2))

hc <- highchart(width = 800, height = 700) %>%
  hc_title(text = "scatterplot", style = list(color = '#2E1717',fontSize = '20px', fontWeight = 'bold'))

for(type in u){
       hc <- hc %>% 
        hc_add_series_scatter(data$gene[data$type==type],data$metab[data$type==type],name=sprintf("type %s", type),showInLegend = TRUE) 
        #if(type==type1){                
        #   hc_add_series(hc,data=line1,type='line',name='regression line1') 
        #}else if (type==type2){                
        #   hc_add_series(hc,data=line2,type='line',name='regression line2')
        #}
} 

hc <- hc %>%
hc_add_series(data=line1,type='line',name='regression line1',enableMouseTracking=FALSE,marker=FALSE) %>%
hc_add_series(data=line2,type='line',name='regression line2',enableMouseTracking=FALSE,marker=FALSE)

hc

添加了两个回归系列,但数据错误 - 您使用了错误的数据格式。作为系列数据,您有一个带有 x 和 y 的对象,它们是数字数组。作为系列数据,您应该有一个数组,其数组看起来像[X,Y](X,Y 是数字)或一个对象数组,看起来像{x: X, y: X}(X,Y 是数字,x,y 是变量的名称)。这个问题在 JS 级别是可见的,应该在 R 中修复。我不是 R 专家,但我认为我在这里写的是一个好的开始。

于 2017-07-12T15:51:04.857 回答