简体   繁体   English

ggplot2 上是否有等效于 points()

[英]Is there an equivalent to points() on ggplot2

I'm working with stock prices and trying to plot the price difference.我正在处理股票价格并尝试 plot 价格差异。 I created one using autoplot.zoo() , my question is, how can I manage to change the point shapes to triangles when they are above the upper threshold and to circles when they are below the lower threshold.我使用autoplot.zoo()创建了一个,我的问题是,当它们高于上限阈值时,如何将点形状更改为三角形,而当它们低于下限阈值时,如何将它们更改为圆形。 I understand that when using the basic plot() function you can do these by calling the points() function, wondering how I can do this but with ggplot2 .我知道在使用基本plot() function 时,您可以通过调用points() function 来做到这一点,想知道如何使用ggplot2来做到这一点。

Here is the code for the plot:这是 plot 的代码:

p<-autoplot.zoo(data, geom = "line")+
        geom_hline(yintercept = threshold, color="red")+
        geom_hline(yintercept = -threshold, color="red")+
        ggtitle("AAPL vs. SPY out of sample")
p+geom_point()

情节图片

We can't fully replicate without your data, but here's an attempt with some sample generated data that should be similar enough that you can adapt for your purposes.没有您的数据,我们无法完全复制,但这里尝试使用一些样本生成的数据,这些数据应该足够相似,您可以根据自己的目的进行调整。

# Sample data
data = data.frame(date = c(2001:2020),
                  spread = runif(20, -10,10))

# Upper and lower threshold
thresh <- 4

You can create an additional variable that determines the shape, based on the relationship in the data itself, and pass that as an argument into ggplot.您可以根据数据本身的关系创建一个确定形状的附加变量,并将其作为参数传递给 ggplot。

# Create conditional data
data$outlier[data$spread > thresh] <- "Above"
data$outlier[data$spread < -thresh] <- "Below"
data$outlier[is.na(data$outlier)] <- "In Range"

library(ggplot2)
ggplot(data, aes(x = date, y = spread, shape = outlier, group = 1)) +
  geom_line() +
  geom_point() +
  geom_hline(yintercept = c(thresh, -thresh), color = "red") +
  scale_shape_manual(values = c(17,16,15))

在此处输入图像描述

# If you want points just above and below# Sample data
data = data.frame(date = c(2001:2020),
                  spread = runif(20, -10,10))
thresh <- 4
data$outlier[data$spread > thresh] <- "Above"
data$outlier[data$spread < -thresh] <- "Below"
ggplot(data, aes(x = date, y = spread, shape = outlier, group = 1)) +
  geom_line() +
  geom_point() +
  geom_hline(yintercept = c(thresh, -thresh), color = "red") +
  scale_shape_manual(values = c(17,16))

在此处输入图像描述

Alternatively, you can just add the points above and below the threshold as individual layers with manually specified shapes, like this.或者,您可以将高于和低于阈值的点添加为具有手动指定形状的单独图层,如下所示。 The pch argument points to shape type. pch参数指向形状类型。

# Another way of doing this

data = data.frame(date = c(2001:2020),
                  spread = runif(20, -10,10))

# Upper and lower threshold
thresh <- 4

ggplot(data, aes(x = date, y = spread, group = 1)) +
  geom_line() +
  geom_point(data = data[data$spread>thresh,], pch = 17) +
  geom_point(data = data[data$spread< (-thresh),], pch = 16) + 
  geom_hline(yintercept = c(thresh, -thresh), color = "red") +
  scale_shape_manual(values = c(17,16))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM