Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在highchart上的散点图中绘制第二条回归线_R_Plot_Highcharts_R Highcharter - Fatal编程技术网

无法在highchart上的散点图中绘制第二条回归线

无法在highchart上的散点图中绘制第二条回归线,r,plot,highcharts,r-highcharter,R,Plot,Highcharts,R Highcharter,我有一组有两类的数据。我想在r中画一个更高的散点图,每个类别都有自己的颜色和独立的回归线。我使用for循环来实现不同的颜色,并计算回归线的两点。问题是,我只能在散点图上画一条回归线。我试图在循环中画一条线,或者使用%>%添加一条线。它们都不起作用。我也不能给这个图表添加标题。 这是我的密码 data<-data.frame(gene=rnorm(20),metab=rnorm(20),type=sample(0:1,1000,rep=TRUE))[1:20,]] b<-glm(dat

我有一组有两类的数据。我想在r中画一个更高的散点图,每个类别都有自己的颜色和独立的回归线。我使用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)

data我试图运行您提供的代码时出错。第一行以
]
结尾,这会阻止代码运行或丢失其他内容

如果我去掉这个,那么在
系数[,5]
上,我得到了一个关于索引越界的错误。通过更改为
[,4]
修复了此问题

在代码的最后两行中,您使用的是
hc\u add\u 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

非常感谢!它完美地解决了我的问题!