【问题标题】:ggplot2 plotting time series with multiple lmggplot2用多个lm绘制时间序列
【发布时间】:2018-07-05 06:27:46
【问题描述】:

我正在尝试将 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)

上面的代码不仅在所有值之间绘制geom_smooth(),例如,“大”变量值 - 我将如何调整我的geom_smooth()代码以不考虑变量为“小”或根据变量因子产生两个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")

【问题讨论】:

  • 子集提供给geom smooth的数据集。
  • 感谢@missuse,这确实是一种接近它的方式!除了现在我正在尝试应用它并且我仍然得到相同的平滑度,所以我的代码不太正确:z
  • subset(df, variable == "large") 而不是subset(df, variable = large)
  • 太棒了 - 谢谢你完美的工作!

标签: r plot ggplot2 lm


【解决方案1】:

根据@missuse28 cmets 回答的问题

z <- subset(df, variable == "large") 
plot1 <- plot1 + geom_smooth(data=z, method = "lm", aes(group=type), se =F)

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2017-03-04
    • 2021-03-17
    • 1970-01-01
    • 2023-03-18
    • 2013-01-30
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多