在R中的chart.TimeSeries中从图例中删除图例 [英] Getting legend out of the plot in chart.TimeSeries in R

查看:52
本文介绍了在R中的chart.TimeSeries中从图例中删除图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

data<- structure(c(103.7, 103.2, 103.1, 105.4, 102.1, 103.5, 103.1, 
            102.6, 102.2, 104.6, -2.1, -1.4, -2.6, 1.9, -0.7, 1.4, -0.6, 
            -1.3, -1.4, 0, -25.3, -25.3, -25.3, -24.9, -24.7, -24.5, -21.4, 
            -20.9, -20.4, -19.8, 25, 20.7, 25.5, 19.8, 12.8, 13.6, 15.5, 
            23, 12.8, 16.3, -16.1, -20.1, -16.9, -17.8, -18.6, -19.5, -16.4, 
            -18.9, -16.1, -15.1), .Dim = c(10L, 5L), .Dimnames = list(NULL, 
             c("Edinburgh", "Glasgow", "Aberdeen", "St Andrews", 
             "Highlands")), .Tsp = c(1985, 1985.75, 12), class = c("mts", 
             "ts", "matrix"))

我发现最合适的数据绘制方法是使用 PerformanceAnalytics 包中的 chart.TimeSeries 函数.我是这样使用的:

I figured out that the most suitable way to plot my data is to use the chart.TimeSeries function from PerformanceAnalytics package. I did that using :

chart.TimeSeries(data, legend.loc = "right")

然后我得到了

我的问题是如何将图例移到右侧图的右边.我什至尝试使用 ggplot 重现相同的图表,但失败了.

My Question is how to get the legend outside the plot to the right. I even tried to reproduce the same chart using ggplot but failed.

推荐答案

尝试一下:

chart.TimeSeries(data, legend.loc = NULL)

然后添加一个自定义图例:

Then add a custom legend:

legend(20,10, c("Edinburgh","Glasgow", "Aberdeen", "St Andrews", "Highlands"),
lty=c(1,1), lwd=c(2.5,2.5), col=c("black", "red", "green", "deepskyblue", "blue"))

这篇关于在R中的chart.TimeSeries中从图例中删除图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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