多点传奇问题

时间:2018-02-22 21:03:59

标签: r ggplot2

我正在开发一个图表,显示股票价格何时超过某些简单的移动平均值。

我已成功设法创建情节,但我不知道如何让传奇分别显示点数。此刻点数相互重叠,一团糟。任何建议都会非常有帮助。感谢。

Stock plot using ggplot2 可重现的代码:

array([[ 200,  800,  400, 1600],
       [ 400, 1000,  800, 2000],
       [ 600, 1200, 1200, 2400]])

1 个答案:

答案 0 :(得分:1)

你可以创建一个新的df。试试这个:

temp1 <- testdat[testdat$crossover200==1,]
temp2 <- testdat[testdat$crossunder200==1,]
temp3 <- testdat[testdat$crossover100==1,]
temp4 <- testdat[testdat$crossunder100==1,]
temp1$Group <- "A"
temp2$Group <- "B"
temp3$Group <- "C"
temp4$Group <- "D"
temp <- rbind(temp1, temp2)
temp <- rbind(temp, temp3)
temp <- rbind(temp, temp4)

ggplot(testdat, aes(x = date,y = close)) + geom_line(size=0.1) + geom_line(aes(x=date,y=sma100, color="100 days SMA")) + geom_line(aes(x=date,y=sma200, color="200 days SMA")) +
  theme(plot.title = element_text(color="red", size=14, face="bold.italic"),legend.position="bottom",
        axis.text.x = element_text(color="black", 
                                   size=10, angle=45),
        axis.text.y = element_text(color="black", 
                                   size=10, angle=45)) + scale_x_date(date_breaks="2 weeks") + 
  scale_y_continuous(breaks = pretty(testdat$close, n = 50)) + ggtitle(label = ticker) + 
  geom_point(data = temp,aes(x = date,y = close,fill=Group, shape=Group))  + 
  scale_fill_manual(name = "test",
                      labels = c("crossover200", "crossunder200", "crossover100", "crossunder100"),
                      values = c("green", "red", "blue", "orange")) +   
  scale_shape_manual(name = "test",
                     labels = c("crossover200", "crossunder200", "crossover100", "crossunder100"),
                     values = c(24, 25, 24, 24))

enter image description here