如何在R中的一条单线的不同范围内设置不同的颜色?

时间:2018-01-14 01:58:14

标签: r dataframe matplotlib plot ggplot2

我现在正面临着如何在R中制作移动平均交叉图的问题。我根据我的价格数据将ma5和ma20添加为两个移动平均线图。 这是我的示例代码..

library("TTR")
library(ggplot2)
price<- c(3.23,  3.29,  3.29 , 3.21,  3.19,  3.18,  3.11,  3.21,  3.25,
3.40,  3.39,  3.28,  3.31 , 3.32,  3.21,  3.19,  3.16,  3.20,
3.26,  3.30,  3.42,  3.44,  3.40,  3.41,  3.59,  3.83,  3.70,
3.86,  3.95,  3.89,  3.94,  3.78,  3.69,  3.74,  3.67,  3.69,
3.69,  3.61,  3.64,  3.83,  3.88,  3.98,  3.98,  3.86,  3.87,
3.93,  4.05,  3.97,  3.90,  3.93,  4.00,  3.85,  3.81,  4.20,
4.17,  4.05,  3.95,  3.96,  3.97,  3.96,  3.88,  3.85,  3.79,
3.83,  3.68,  3.72,  3.73,  3.81,  3.80,  3.81,  3.75,  3.87,
3.90,  3.89,  3.86,  3.81,  3.86,  3.78,  3.83,  3.87,  3.91,
4.05,  4.07,  4.02,  4.01,  4.00,  4.13,  4.07,  4.11,  4.26,
4.33,  4.32,  4.39,  4.30,  4.39,  4.68,  4.69,  4.70,  4.60,
4.71,  4.81,  4.73,  4.78,  4.64,  4.64,  4.64,  4.61,  4.44)

date<- c("2004-01-23", "2004-01-26", "2004-01-27", "2004-01-28",
"2004-02-02", "2004-02-03", "2004-02-04", "2004-02-05",
"2004-02-06", "2004-02-11", "2004-02-12", "2004-02-13",
"2004-02-17", "2004-02-18", "2004-02-19", "2004-02-20",
"2004-02-23", "2004-02-24", "2004-02-25", "2004-02-26",
"2004-02-27", "2004-03-01", "2004-03-02", "2004-03-03",
"2004-03-04", "2004-03-05", "2004-03-08", "2004-03-09",
"2004-03-10", "2004-03-11", "2004-03-12", "2004-03-15",
"2004-03-16", "2004-03-17", "2004-03-18", "2004-03-19",
"2004-03-22", "2004-03-23", "2004-03-24", "2004-03-25",
"2004-03-26", "2004-03-29", "2004-03-30", "2004-03-31",
"2004-04-01", "2004-04-02", "2004-04-05", "2004-04-06",
"2004-04-07", "2004-04-08", "2004-04-12", "2004-04-13",
"2004-04-14", "2004-04-15", "2004-04-16", "2004-04-19",
"2004-04-20", "2004-04-21", "2004-04-22", "2004-04-23",
"2004-04-26", "2004-04-27", "2004-04-28", "2004-04-29",
"2004-04-30", "2004-05-03", "2004-05-04", "2004-05-05",
"2004-05-06", "2004-05-07", "2004-05-10", "2004-05-11",
"2004-05-12", "2004-05-13", "2004-05-14", "2004-05-17",
"2004-05-18", "2004-05-19", "2004-05-20", "2004-05-21",
"2004-05-24", "2004-05-25", "2004-05-26", "2004-05-27",
"2004-05-28", "2004-06-01", "2004-06-02", "2004-06-03",
"2004-06-04", "2004-06-07", "2004-06-08", "2004-06-09",
"2004-06-10", "2004-06-14", "2004-06-15", "2004-06-16",
"2004-06-17", "2004-06-18", "2004-06-21", "2004-06-22",
"2004-06-23", "2004-06-24", "2004-06-25", "2004-06-28",
"2004-06-29", "2004-06-30", "2004-07-01", "2004-07-02")

price5<- SMA(price,n=5)
price20<- SMA(price,n=20)
pricedf<- data.frame(date,price5,price20,price)

ggplot(pricedf,aes(date))+geom_line(group=1,aes(y=price5,colour="ma5"))+geom_line(group=1,aes(y=price20,colour="ma20"))+xlab("Date")+ylab("Price")

enter image description here 这个地块上有几对交叉。我想要的是当ma5高于ma20标记为'price'(我的pricedf)图中的一个特征时的绿线。另一方面,当ma5在m​​a20下标记为'price'图上的红线时。 示例图看起来像这张图, enter image description here 我在考虑将price5减去price20并比较值是否大于0.但是如何在另一个不同颜色的图上绘制它们?

1 个答案:

答案 0 :(得分:2)

以下是我如何解决它。

library("TTR")
library(ggplot2)
price<- c(3.23,  3.29,  3.29 , 3.21,  3.19,  3.18,  3.11,  3.21,  3.25,
      3.40,  3.39,  3.28,  3.31 , 3.32,  3.21,  3.19,  3.16,  3.20,
      3.26,  3.30,  3.42,  3.44,  3.40,  3.41,  3.59,  3.83,  3.70,
      3.86,  3.95,  3.89,  3.94,  3.78,  3.69,  3.74,  3.67,  3.69,
      3.69,  3.61,  3.64,  3.83,  3.88,  3.98,  3.98,  3.86,  3.87,
      3.93,  4.05,  3.97,  3.90,  3.93,  4.00,  3.85,  3.81,  4.20,
      4.17,  4.05,  3.95,  3.96,  3.97,  3.96,  3.88,  3.85,  3.79,
      3.83,  3.68,  3.72,  3.73,  3.81,  3.80,  3.81,  3.75,  3.87,
      3.90,  3.89,  3.86,  3.81,  3.86,  3.78,  3.83,  3.87,  3.91,
      4.05,  4.07,  4.02,  4.01,  4.00,  4.13,  4.07,  4.11,  4.26,
      4.33,  4.32,  4.39,  4.30,  4.39,  4.68,  4.69,  4.70,  4.60,
      4.71,  4.81,  4.73,  4.78,  4.64,  4.64,  4.64,  4.61,  4.44)

date<- c("2004-01-23", "2004-01-26", "2004-01-27", "2004-01-28",
     "2004-02-02", "2004-02-03", "2004-02-04", "2004-02-05",
     "2004-02-06", "2004-02-11", "2004-02-12", "2004-02-13",
     "2004-02-17", "2004-02-18", "2004-02-19", "2004-02-20",
     "2004-02-23", "2004-02-24", "2004-02-25", "2004-02-26",
     "2004-02-27", "2004-03-01", "2004-03-02", "2004-03-03",
     "2004-03-04", "2004-03-05", "2004-03-08", "2004-03-09",
     "2004-03-10", "2004-03-11", "2004-03-12", "2004-03-15",
     "2004-03-16", "2004-03-17", "2004-03-18", "2004-03-19",
     "2004-03-22", "2004-03-23", "2004-03-24", "2004-03-25",
     "2004-03-26", "2004-03-29", "2004-03-30", "2004-03-31",
     "2004-04-01", "2004-04-02", "2004-04-05", "2004-04-06",
     "2004-04-07", "2004-04-08", "2004-04-12", "2004-04-13",
     "2004-04-14", "2004-04-15", "2004-04-16", "2004-04-19",
     "2004-04-20", "2004-04-21", "2004-04-22", "2004-04-23",
     "2004-04-26", "2004-04-27", "2004-04-28", "2004-04-29",
     "2004-04-30", "2004-05-03", "2004-05-04", "2004-05-05",
     "2004-05-06", "2004-05-07", "2004-05-10", "2004-05-11",
     "2004-05-12", "2004-05-13", "2004-05-14", "2004-05-17",
     "2004-05-18", "2004-05-19", "2004-05-20", "2004-05-21",
     "2004-05-24", "2004-05-25", "2004-05-26", "2004-05-27",
     "2004-05-28", "2004-06-01", "2004-06-02", "2004-06-03",
     "2004-06-04", "2004-06-07", "2004-06-08", "2004-06-09",
     "2004-06-10", "2004-06-14", "2004-06-15", "2004-06-16",
     "2004-06-17", "2004-06-18", "2004-06-21", "2004-06-22",
     "2004-06-23", "2004-06-24", "2004-06-25", "2004-06-28",
     "2004-06-29", "2004-06-30", "2004-07-01", "2004-07-02")

price5<- SMA(price,n=5)
price20<- SMA(price,n=20)
pricedf<- data.frame(date,price5,price20,price)
coldf <- ifelse(price5 - price20 > 0, 'green', 'red')
coldf[is.na(coldf)] <- 'green'
coldf

ggplot(pricedf) +
  geom_line( aes(x = date, y=price, group = 1, color = coldf)) +
  xlab("Date") +
  ylab("Price")

创造了这个 graph, 我使用ifelse语句来查找price5大于20的价格。问题是这会创建NA,我用绿色填充。如果你希望它以绿色到红色的方式,我不是百分之百。您只需更改

即可
coldf <- ifelse(price5 - price20 > 0, 'green', 'red')

coldf <- ifelse(price5 - price20 > 0, 'red', 'green')

看起来像graph2