ggplot2用多个lm绘制时间序列

时间:2018-01-25 21:51:59

标签: r plot ggplot2 lm

我试图在一个时间序列中将geom_smooth设置为多个阶段,其中绘图有两个变量,但我只希望其中一个变量平滑或者为每个变量因子分别设置geom_smooth()

示例数据框(df)位于此线程的底部 - 因此首先运行数据。请注意,它采用长格式,以便更好地进行ggplot2操作。

到目前为止,我有:

df$Date <- as.Date(df$Date)
df$value <-as.numeric(df$value)

plot1 <- ggplot(df, 
            aes(x=Date, y=value, color=variable, group = variable))

plot1 <- plot1 + geom_point(size=3) + geom_line(linetype = 6, lwd =1)               

#location of phases
plot1 <- plot1 + geom_vline(xintercept = as.numeric(as.Date("2010-01-
16")), linetype=4, lwd=1, colour="red") +
geom_vline(xintercept = as.numeric(as.Date("2010-02-01")), linetype=4, lwd=1, colour="red")

plot1 <- plot1 + geom_smooth(method = "lm",aes(group=type), se =F)

enter image description here

以上代码不仅在所有值之间绘制geom_smooth(),例如,&#39; large&#39;变量值 - 如何调整我的geom_smooth()代码,以便不考虑变量值小的值&#39;或根据变量因子产生两个geom_smooth()

数据:

df <- structure(list(Date = structure(c(14610, 14611, 14612, 14613, 
14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621, 14622, 
14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630, 14631, 
14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639, 14640, 
14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648, 14649, 
14650, 14651, 14652, 14653, 14654, 14655, 14610, 14611, 14612, 
14613, 14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621, 
14622, 14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630, 
14631, 14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639, 
14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648, 
14649, 14650, 14651, 14652, 14653, 14654, 14655), class = "Date"), 
type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("phaseI", "phaseII", 
"phaseIII"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("large", "small"), class = "factor"), value = c(0.2, 
0.353333333333333, 0.506666666666667, 0.66, 0.813333333333333, 
0.966666666666667, 1.12, 1.27333333333333, 1.42666666666667, 
1.58, 1.73333333333333, 1.88666666666667, 2.04, 2.19333333333333, 
2.34666666666667, 2.5, 0.8, 1.4, 1, 1, 1.2, 1.3, 0.8, 0.6, 
0.7, 1, 1.4, 1.1, 1, 1.2, 0.6, 0.7, 1, 1.4, 1.1, 1, 1.2, 
1.4, 1.5, 1.5, 1.8, 2, 1.3, 1.8, 1.9, 1.6, 0.43234083361458, 
0.57943098328542, 0.585560035612434, 0.573809110070579, 
0.23520764564164, 
0.616980984131806, 0.575250472826883, 0.106012413115241, 
0.929755941592157, 0.772053755586967, 0.350421967194416, 
0.72715537250042, 0.358676509861834, 0.138381857587956, 
0.338254146534018, 
0.800451389560476, 0.430101366783492, 0.909903622069396, 
0.467150738765486, 0.321958338515833, 0.192312039108947, 
0.79779056571424, 0.467848383216187, 0.206698473682627, 
0.643230387661606, 
0.563105152756907, 0.618167799594812, 0.243913730233908, 
0.564221533830278, 0.427351927570999, 0.664244866860099, 
0.578704655752517, 0.510512614645995, 0.66944659948349, 
0.961445776955225, 
0.554041534732096, 0.956937691662461, 0.838187742326409, 
0.666935266461223, 0.512665116298012, 0.434139845683239, 
0.827959679719061, 0.866472804406658, 0.465548504237086, 
0.117253547115251, 0.684186496818438)), row.names = c(NA, 
-92L), .Names = c("Date", "type", "variable", "value"), class = 
"data.frame")

1 个答案:

答案 0 :(得分:0)

根据@ missuse28评论回答问题

template <class TYPE>
class Array
{
public:
    Array();
    Array(int length, int beg_index = 0);
    Array(const Array<TYPE>& copy);
    ~Array();

    const Array<TYPE> & operator=(const Array<TYPE>& rhs);    
    TYPE & operator[](int index);
    const TYPE & operator[](int index) const;
    int  getLength() const { return m_length; }
    inline void setLength(int new_length);
    int  getStartIndex() const {return m_start_index; }
    void setStartIndex(const int new_start_index) {m_start_index = new_start_index;}

private:
    bool WithinBounds(const int loc_request) const;
    bool ValidLength(const int requested_length)const;

    TYPE * m_array = 0;
    int m_length;
    int m_start_index;
};
相关问题