交互式 R 图
我有一个 R 图,我希望用户能够单击图表上的某个点,并且我想知道他们单击的位置。有点像鼠标监听器。我正在使用 RStudio。这有可能吗?基本上,对于他们点击的点,我想显示另一个图,其中包含有关该点的更多详细信息。
这就是我所得到的:
data <- data.frame(x=c(1,2,3,4,5),y=c(1,2,3,4,5))
x11()
plot(data)
loc <- locator(n=1)
if(loc$x > 2) {
x11()
plot(c(1,2,3),c(5,6,7))
}
loc <- locator(n=1)
if(loc$x > 2) {
x11()
plot(c(4,5,6),c(5,6,7))
}
所以,第一个图出现了,有 5 个数据点。如果用户单击 x 值超过 2,那么我想打开另一个图,该图绘制您看到列出的数据点(3 个数据点)。然后,如果他们关闭该图并在原始的 5 个数据点图上再次单击过去 2,那么我希望它打开一个新图,其中包含您在第二个 if 语句中看到的 3 个数据点。
但它给我带来了错误和 I/O 问题。
I have an R plot and I'd like the user to be able to click a spot on the graph, and I'd like to know where they clicked. Sort of like a mouse listener. I'm using RStudio. Is this possible in any way? Basically, for a point they click on, I want to display another plot with more details about that point.
Here's what I have:
data <- data.frame(x=c(1,2,3,4,5),y=c(1,2,3,4,5))
x11()
plot(data)
loc <- locator(n=1)
if(loc$x > 2) {
x11()
plot(c(1,2,3),c(5,6,7))
}
loc <- locator(n=1)
if(loc$x > 2) {
x11()
plot(c(4,5,6),c(5,6,7))
}
So, the first plot comes up that has 5 data points. If the user clicks past an x value of 2, then I want to open another plot, which plots the data points you see listed (3 data points). Then, if they close that plot and they click past 2 AGAIN on the original, 5 data point plot, then I want it to open up a new plot with the 3 data points you see in the second if statement.
But it's giving me errors and I/O issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您对内置绘图窗格执行
locator()
调用,这在 RStudio 中效果很好。我认为是针对 X11 调用locator
导致了 RStudio 中的问题。绘制 X11 详细信息后,调用 dev.set(0) 使 RStudio 绘图窗格再次处于活动状态。(说实话,我不确定为什么
dev.set(0)
有效,必须与编写 RStudio 图形设备的开发人员核实。)This works fine in RStudio if you perform the
locator()
call against the built-in plots pane. I think it's callinglocator
against X11 that causes the problems in RStudio. After plotting the X11 detail info, calldev.set(0)
to make the RStudio plots pane active again.(To be honest, I'm not sure why
dev.set(0)
works, would have to check with the developer who wrote the RStudio graphics device.)(我意识到这不是一个完整的答案,但它作为注释的格式不太好。)要使特定设备成为活动设备,您需要确定其编号。执行您的代码后,我可以获取设备列表并将第一个 X11 设备设置为当前设备,
因此请尝试以下操作:
在我的计算机上,您还需要单击第一个图形窗口的标题来公开它,因为 Mac Gui 不带活动设备到前台。
(I realize this is not a complete answer but it wouldn't format very well as a comment.) To make a particular device the active device you need to determine its number. After executing your code, I can get my list of devices and set the first X11 device to be current
So try this:
On my machine you need to also click on the first graphics window's title to expose it because the Mac Gui does not bring the active device to the foreground.