使用自动绘图和ggplot更改每个时间序列的线型/宽度 [英] Changing line type / width for each time series with autoplot and ggplot

查看:111
本文介绍了使用自动绘图和ggplot更改每个时间序列的线型/宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含两个时间序列的图:

I am trying to create one plot to include two time-series:

library(zoo)
library(tseries)
library(ggplot2)
library(ggfortify)

ts1 <- ts(c(1,2,3,4))
ts2 <- ts(c(1.1, 2.1, 3.1, 4.1))
autoplot(ts.union(ts1, ts2), facets = FALSE) +
scale_color_manual(breaks = c("ts1", "ts2"), labels = c("Actual", "Forecasted"), values=c("black", "red"))

更改每个系列的图例标题和颜色后,我也想按系列修改线宽和类型,但是调用scale_linetype_manual似乎并没有一点改变.我应该如何进行?

After changing the legend titles and colors for each series I want to modify the line width and type, also by series, but calling scale_linetype_manual does not seem to change the plot by a bit. How should I proceed?

推荐答案

您需要在情节中添加各自的美感,然后才能进行更改.为此,您需要知道autoplot用于美学映射的变量的名称:请参阅色标的名称.

You need to add the respective aesthetics to your plot before you can change them. For this you need to know the name of the variable that autoplot uses for aesthetic mapping: see the name of the colour scale.

用于绘制的数据框如下所示

The data frame that is used for plotting looks like this

#  Index plot_group value
#1     1        ts1   1.0
#2     2        ts1   2.0
#3     3        ts1   3.0
#4     4        ts1   4.0
#5     1        ts2   1.1
#6     2        ts2   2.1
#7     3        ts2   3.1
#8     4        ts2   4.1

autoplot为您创建列plot_group,为了区分两个时间序列,它(仅)映射了颜色.因此,您可以调用scale_color_manual.

autoplot creates the column plot_group for you and in order to distinguish the two time series, it maps (only) the colour to it. Hence you could call scale_color_manual.

您可以使用此变量将其他美观形式映射到它,例如sizelinetype.

You can use this variable to map another aesthetics to it, for example size and linetype.

autoplot(ts.union(ts1, ts2), facets = FALSE) +
 scale_color_manual(labels = c("Actual", "Forecasted"),
                    values=c("black", "red")) +
 aes(linetype = plot_group,
     size = plot_group) +
 scale_linetype_manual(labels = c("Actual", "Forecasted"),
                       values = c(1, 2)) +
 scale_size_manual(labels = c("Actual", "Forecasted"),
                   values = c(1, 2))

数据

p <- autoplot(ts.union(ts1, ts2), facets = FALSE)
p$data

这篇关于使用自动绘图和ggplot更改每个时间序列的线型/宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆