如何在时间序列中途更改 ggplot2 中的线属性?
How to change line properties in ggplot2 halfway in a time series?
从 economics{ggplot2}
数据集
中获取两个时间序列的以下简单图
require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
group_by(indicator, Y2K) %>%
ggplot(aes(date, percentage, group = indicator, colour = indicator)) + geom_line(size=1)
我想将 21 世纪所有点的 linetype
从 "solid" 更改为 "dashed"(可能还有行 size
),即Y2K
等于 TRUE
的那些观察结果。
我做了一个 group_by(indicator, Y2K)
,但在 ggplot
命令中,我似乎无法在多个级别上使用 group =
,所以线属性现在只有 indicator
不同。
问题:如何实现这种分割线的外观?
更新:我的首选解决方案是对@sahoang 的解决方案进行了细微调整:
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
ggplot(aes(date, percentage, colour = indicator)) +
geom_line(size=1, aes(linetype = year(date) >= 2000)) +
scale_linetype(guide = F)
这消除了@Roland评论的group_by
,filter
步骤确保时间序列将在Y2K点连接(如果数据是基于年份的,否则可能会出现视觉上的不连续性)。
比@Roland 的建议更简单:
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
group_by(indicator, Y2K) -> econ
ggplot(econ, aes(date, percentage, group = indicator, colour = indicator)) +
geom_line(data = filter(econ, !Y2K), size=1, linetype = "solid") +
geom_line(data = filter(econ, Y2K), size=1, linetype = "dashed")
P.S。改变绘图宽度以消除尖峰伪影(红线)。
require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
ggplot(aes(date, percentage, colour = indicator)) +
geom_line(size=1, aes(linetype = Y2K))
从 economics{ggplot2}
数据集
require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
group_by(indicator, Y2K) %>%
ggplot(aes(date, percentage, group = indicator, colour = indicator)) + geom_line(size=1)
我想将 21 世纪所有点的 linetype
从 "solid" 更改为 "dashed"(可能还有行 size
),即Y2K
等于 TRUE
的那些观察结果。
我做了一个 group_by(indicator, Y2K)
,但在 ggplot
命令中,我似乎无法在多个级别上使用 group =
,所以线属性现在只有 indicator
不同。
问题:如何实现这种分割线的外观?
更新:我的首选解决方案是对@sahoang 的解决方案进行了细微调整:
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
ggplot(aes(date, percentage, colour = indicator)) +
geom_line(size=1, aes(linetype = year(date) >= 2000)) +
scale_linetype(guide = F)
这消除了@Roland评论的group_by
,filter
步骤确保时间序列将在Y2K点连接(如果数据是基于年份的,否则可能会出现视觉上的不连续性)。
比@Roland 的建议更简单:
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
group_by(indicator, Y2K) -> econ
ggplot(econ, aes(date, percentage, group = indicator, colour = indicator)) +
geom_line(data = filter(econ, !Y2K), size=1, linetype = "solid") +
geom_line(data = filter(econ, Y2K), size=1, linetype = "dashed")
P.S。改变绘图宽度以消除尖峰伪影(红线)。
require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
ggplot(aes(date, percentage, colour = indicator)) +
geom_line(size=1, aes(linetype = Y2K))