在箱线图中添加回归线

时间:2019-12-19 23:18:20

标签: r boxplot

我使用下面的代码在箱线图后面添加了回归线。

boxplot(yield~Year, data=dfreg.raw,
        ylab = 'Yield (bushels/acre)',
        col = 'orange')
yield.year <- lm(yield~Year, data = dfreg.raw)
abline(reg = yield.year)

但是,回归线未显示。我得到的情节在下面enter image description here

我的数据如下所示。这是一个面板数据,可能最终会导致回归线问题。

> head(dfreg.raw)
# A tibble: 6 x 15
  index  Year yield State.Code  harv frez_j  dd_j cupc_j sm7_j fitted_j max_spring_j sp_spring_j
  <dbl> <dbl> <dbl>      <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>    <dbl>        <dbl>       <dbl>
1 16001  1984 105           16  7200   330. 2438.   7.32  53.4     49.1         19.7       0.863
2 16001  1985  96.8         16  8200   413. 2407.   5.71  52.5     48.4         23.9      -0.391
3 16001  1986  94.9         16  7400   476. 2638.   8.34  52.5     48.4         23.4      -0.122
4 16001  1987 106.          16  9700   154. 2838.   5.44  54.4     49.9         25.6      -0.485
5 16001  1988  89.6         16  7600   184. 2944.   3.28  54.5     50.0         23.9       0.115
6 16001  1989  96.4         16  7300   383. 2766.   5.91  52.6     48.4         23.5      -1.02 
# … with 3 more variables: pc_spring_j <dbl>, lt <dbl>, qt <dbl>


有人对此有任何想法吗?

1 个答案:

答案 0 :(得分:2)

x值是1:max(x变量的级别),因此abline不起作用。您可以在下面尝试类似的方法。

首先模拟一个数据集:

Vue.component('customer-result', {
    props: {
      results: {
          type: Array,
          required: false,
      }
    },
    // language=Vue
    template: `
    <ul>
      <li v-for="result in results" :key="result.id" class="customer-result">
          <input type="radio" 
                 :value="result.pk" 
                 name="selected-customer" 
                 v-model="customerRadio"
                 @change="changeCustomer"
          />
          <span class="customer-name">{{ result.name }}</span>
          <span class="customer-address_1" v-if="result.address_1">{{ result.address_1}}</span>
          <span class="customer-address_2" v-if="result.address_2">{{ result.address_2}}</span>
          <span class="customer-address_3" v-if="result.address_3">{{ result.address_3}}</span>
          <span class="customer-city" v-if="result.city">{{ result.city}}, </span>
          <span class="customer-state" v-if="result.state">{{ result.state}}</span>
          <span class="customer-country" v-if="result.country">{{ result.country}}</span>
          <span class="customer-email" v-if="result.email">{{ result.email}}</span>
          <span class="customer-phone" v-if="result.phone">{{ result.phone}}</span>
      </li>
    </ul>
    `,
    data(){
        return {
            customerRadio: null
        }
    },
    methods: {
        changeCustomer(){
            console.log('component change customer');
            this.$emit('change-customer');
        }
    }
});

var app = new Vue({
    delimiters: ['[[', ']]'], // django
    el: '#customer-lookup',
    methods: {
        search(e){
            if( this.customerLookupInput === "" ){
                this.results = null;
            } else {
                searchCustomers(this.customerLookupInput, this); // ajax call that's working
            }
        },
        updateResults(results){
            this.results = results; // update from ajax that's working
        },
        changeCustomer(){
            console.log('root change customer')
        },
        changeCustomer2(){
            console.log('root2 change customer');
        }
    },
    data() {
        return {
            customerLookupInput: null,
            results: null,
            customerRadio: null
        }
    }
});

然后情节:

dfreg.raw= data.frame(
yield=rpois(100,lambda=rep(seq(60,100,by=10),each=20)),
Year=rep(1995:1999,each=20)
)

获取年份的唯一上升向量,并进行预测

boxplot(yield~Year, data=dfreg.raw,
        ylab = 'Yield (bushels/acre)',
        col = 'orange')
yield.year <- lm(yield~Year, data = dfreg.raw)

enter image description here