【问题标题】:cannot draw second regression line in scatter plot on highchart无法在高图上的散点图中绘制第二条回归线
【发布时间】:2017-12-15 21:43:57
【问题描述】:

我有一组包含两个类别的数据。我想在 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。

【问题讨论】:

    标签: r plot highcharts r-highcharter


    【解决方案1】:

    我在尝试运行您提供的代码时遇到错误。第一行以] 结尾,这会阻止代码运行或缺少其他内容。

    如果我删除它,那么在coefficients[,5] 上我会收到关于索引越界的错误。通过更改为[,4] 修复了该问题。

    在代码的最后两行中,您使用 hc_add_serieshc - 这会触发单个更改,打开图表并且不会保存到 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 专家,但我认为我在这里写的是一个好的开始。

    【讨论】:

    • 非常感谢!完美解决了我的问题!
    猜你喜欢
    • 2021-05-09
    • 2019-10-22
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多