简体   繁体   English

R-如果满足条件,则在Plotly中添加跟踪

[英]R - Add trace in Plotly if condition is met

I am creating a scatter plot graph and I would like the points connected by a trace if a condition is met. 我正在创建一个散点图,如果满足条件,我希望通过迹线连接点。 My data is separated into sequences, if an X and Y coordinate is part of the same sequence, I would like there to be a trace. 我的数据分为多个序列,如果X和Y坐标是同一序列的一部分,我希望有一个轨迹。 My sample data and code snippet is below. 我的示例数据和代码段如下。

Sample Data: 样本数据:

X   Y   Seq
1   3    1
2   5    1
1   4    1
3   1    2
4   5    2
6   3    3
3   4    3

In this example I would like points (1, 3), (2, 5), (1, 4) traced, points (3, 1), (4, 5) traced, and points (6, 3), (3, 4) traced. 在此示例中,我希望跟踪点(1、3),(2、5),(1、4),跟踪点(3、1),(4、5)和点(6、3),(3 ,4)追踪。 There should be a break in the trace if a new sequence starts. 如果有新的序列开始,则跟踪中应该有一个中断。

Code: 码:

 plot_ly (data, x = data$X , y = data$Y,
          type = "scatter", 
          mode="markers")%>%
          add_trace(data$Seq==shift(data$Seq, type="lag"), mode="lines")

Here is an image of the plot that my actual data is giving me. 这是我的实际数据给我的情节图。 You can see the points are being plotted but there is no break. 您可以看到正在绘制点,但没有中断。 跟踪

The problem lies in your use of add_trace . 问题在于您使用add_trace You're passing what I assume is a subset of your data to the first argument of add_trace when this argument expects an existing plot/trace. 当此参数需要现有的绘图/轨迹时,您add_trace我假设的数据子集传递给add_trace的第一个参数。 The problem is, since you're piping in with %>% the function is inheriting the original data and ignoring your subset. 问题是,由于要使用%>%进行传递,因此该函数将继承原始数据,而忽略您的子集。

Note that the below will give the same plot even though my variable NO has nothing to do with the plot: 请注意,即使我的变量NO与该图无关,以下内容也会给出相同的图:

X=c(1,2,1,3,4,6,3)
Y=c(3,5,4,1,5,3,4)
seq=c(1,1,1,2,2,3,3)
dataX <- data.frame(X,Y,seq)

NO <- "this won't work"
plot_ly plot_ly (dataX, x = dataX$X , y = dataX$Y,
         type = "scatter", 
         mode="markers") %>%
          add_trace(NO, mode="lines")

在此处输入图片说明

You can fix this with inherit=F , but then it won't work because add_trace is trying to add something to the plot NO which isn't a plot (and your subset wouldn't work either) 你可以解决这个inherit=F ,但随后它不会工作,因为add_trace试图添加一些情节NO这是不是一个阴谋(和你的子集将不会工作)

plot_ly (dataX, x = dataX$X , y = dataX$Y,
         type = "scatter", 
         mode="markers") %>%
  add_trace(NO, mode="lines", inherit=FALSE)

## No trace type specified: 

When you add traces you want to be explicit in the x= and y= . 添加跟踪时,您希望在x=y=明确显示。 Then you can allow it to automatically inherit the previous plot/trace, or specify one. 然后,您可以允许它自动继承上一个图/迹线,或指定一个。 As for what you're trying to do, you could build it up with a loop: 至于您要执行的操作,可以使用循环来构建它:

#make the plot
p <- plot_ly (dataX, x = dataX$X , y = dataX$Y,
         type = "scatter", 
         mode="markers")

#build it up
for(i in levels(factor(dataX$seq))){
  #subset data
  dataFilt <- dataX[dataX$seq==i,]
  #add it
  p <- add_trace(p, x=dataFilt$X, y=dataFilt$Y,mode="lines",color ='yellow')
}
p

在此处输入图片说明

This makes a new series each time so it's a bit of a work around. 每次都会制作一个新系列,因此需要一些工作。 You can hide the legend and it looks correct: 您可以隐藏图例,它看起来正确:

p %>%
  layout(showlegend = FALSE)

在此处输入图片说明

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

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