简体   繁体   English

如何可视化 R 中的多条轨迹?

[英]How to visualize multiple trajectories in R?

I am trying to visualize the trajectory of multiple participants in a virtual room using R.我正在尝试使用 R 可视化虚拟房间中多个参与者的轨迹。 I have a participant entering from the right (black square) and moving toward the left, where there is an exit door (red square).我有一个参与者从右侧进入(黑色方块)并向左移动,那里有一个出口门(红色方块)。 Sometimes there is an obstacle right in the middle of the room (circle), and the participant goes around it, moving either to the left of the obstacle (that would be the bottom half of the graph) or to the right of the obstacle (that would be the top half of the graph).有时在房间中间(圆圈)有一个障碍物,参与者绕过它,移动到障碍物的左侧(即图表的下半部分)或障碍物的右侧(那将是图表的上半部分)。 In the example below the participant moves to the left of the obstacle to avoid it.在下面的示例中,参与者移动到障碍物的左侧以避开它。

library(shape)
# black line continuous
pos_x <- c(5.04,4.68,4.39,4.09,3.73,3.37,3.07,2.77,2.47,2.11)
pos_z <- c(0.74,0.69,0.64,0.60,0.56,0.52,0.50,0.50,0.50,0.51)
df1 <- cbind.data.frame(pos_x,pos_z)
x.2 <- df1$pos_x
z.2 <- df1$pos_z
plot(x.2,z.2,type="l", xlim=range(x.2), ylim=c(-1,3.5), xlab="x", ylab="z", main = "jagged trajectory using LINES function")
filledrectangle(wx = 0.2, wy = 0.2,col = "black", mid = c(5.16, 1), angle = 0)
filledrectangle(wx = 0.2, wy = 0.2,col = "red", mid = c(2, 1), angle = 0)
plotcircle(mid = c(3.4, 1), r = 0.05)

一条轨迹的可视化

To visualize multiple participants' trajectories on the same graph (ie, multiple lines), I have used the function plot to set up the plot itself (and the first line) and then I have used the function lines to add other trajectories after that. To visualize multiple participants' trajectories on the same graph (ie, multiple lines), I have used the function plot to set up the plot itself (and the first line) and then I have used the function lines to add other trajectories after that.

This setup usually works pretty well, but I have an issue with the function lines.这种设置通常效果很好,但我对 function 线路有疑问。 When a participant crosses the room from the left side of the room to the right or vice versa (that is, goes from the bottom of the graph to the top of the graph, or vice versa) then I have an artifact, a jagged line (see red line below.)当参与者从房间的左侧穿过房间到右侧或反之亦然(即从图表的底部到图表的顶部,反之亦然)时,我有一个伪影,一条锯齿线(见下面的红线。)

# adding a red line (jagged) to the plot above
pos_x <- c(4.93,4.58,4.29,4.00,3.66,3.52,3.59,3.72,3.76,3.67)
pos_z <- c(0.42,0.33,0.25,0.17,0.20,0.52,0.80,1.14,1.44,1.72)
df2 <- cbind.data.frame(pos_x,pos_z)
x.3 <- df2$pos_x
z.3 <- df2$pos_z
lines(x.3[order(x.3)], z.3[order(x.3)], xlim=range(x.2), ylim=c(-1,3.5), pch=16, col="red")

锯齿状红线的可视化

If I just use just the function "plot" for that single line, then the trajectory would come out right (see red line below.)如果我只对那条线使用 function “绘图”,那么轨迹会正确显示(见下面的红线。)

# red line continuous
pos_x <- c(4.93,4.58,4.29,4.00,3.66,3.52,3.59,3.72,3.76,3.67)
pos_z <- c(0.42,0.33,0.25,0.17,0.20,0.52,0.80,1.14,1.44,1.72)
df2 <- cbind.data.frame(pos_x,pos_z)
x.3 <- df2$pos_x
z.3 <- df2$pos_z
plot(x.3,z.3,type="l", xlim=range(x.2), ylim=c(-1,3.5), xlab="x", ylab="z",col="red", main = "smooth trajectory using PLOT function")
filledrectangle(wx = 0.2, wy = 0.2,col = "black", mid = c(5.16, 1), angle = 0)
filledrectangle(wx = 0.2, wy = 0.2,col = "red", mid = c(2, 1), angle = 0)
plotcircle(mid = c(3.4, 1), r = 0.05)

平滑红线的可视化

I wonder if you think there is a way I could make the line smoother using these functions, by adjusting some parameters, or if should use other functions.我想知道您是否认为有一种方法可以使用这些功能使线条更平滑,通过调整一些参数,或者是否应该使用其他功能。 I have to say I (still) work mostly in base R when I work with plots, but I welcome solutions in ggplot if needed.我不得不说,当我使用地块时,我(仍然)主要在基础 R 中工作,但如果需要,我欢迎 ggplot 中的解决方案。 Or do you have any other ideas or resources on how to visualize trajectories?或者您对如何可视化轨迹有任何其他想法或资源?

As Allan Cameron noted in the comment, one just needs to simply remove the [order(x.3)] from after x.3 and z.3 in the call to lines :正如 Allan Cameron 在评论中指出的那样,只需在对lines的调用中从x.3z.3之后删除[order(x.3)]

固定的线

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

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