[英]ggplot2 plotting time series with multiple lm
I am trying to fit a geom_smooth to multiple phases in a time series where the plot has two variables, but I only want one of the variables smoothed or a way to have a separate geom_smooth()
for each variable factor. 我正在尝试将geom_smooth拟合到时间图中具有两个变量的多个阶段,但我只希望对其中一个变量进行平滑处理,或者只希望对每个变量因子使用单独的
geom_smooth()
。
The example data frame ( df
) is at the bottom of this thread - so run first for the data. 示例数据帧(
df
)在此线程的底部-因此首先运行该数据。 Note it is in long format for better ggplot2
manipulation. 请注意,它采用长格式,可以更好地控制
ggplot2
。
So far I have: 到目前为止,我有:
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)
The above code plots the geom_smooth()
between all the values not only, for example, the 'large' variable values - how would I adjust my geom_smooth()
code to not consider the values where the variable is 'small' or produce two geom_smooth()
based on the variable factor? 上面的代码不仅在所有值之间绘制了
geom_smooth()
值,例如,在'large'变量值之间-我如何调整我的geom_smooth()
代码以不考虑变量为'small'的值或产生两个geom_smooth()
基于可变因素?
Data: 数据:
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")
Answered question as per @missuse28 comments 根据@ missuse28评论回答问题
z <- subset(df, variable == "large")
plot1 <- plot1 + geom_smooth(data=z, method = "lm", aes(group=type), se =F)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.