【发布时间】:2015-10-28 11:26:50
【问题描述】:
我正在尝试以闪亮的方式创建数据的交互式可视化。可视化显示系列部分的分布(或直方图)。例如,下面的代码创建了一个系列和系列中的两个选择(两个是固定的),然后使用 ggplot 显示:
library(ggplot2)
set.seed(123)
dat <- data.frame(x = 1:1000,
y = cumsum(rnorm(1000, mean = 0.1)))
sel1 <- 200:400 # selection 1
sel2 <- 700:900 # Selection 2
# create a plot of the series
ggplot() + geom_line(data = dat, aes(x = x, y = y)) +
geom_rect(aes(xmin = sel1[1], xmax = sel1[length(sel1)],
ymin = -Inf, ymax = Inf), alpha = 0.5, fill = "red") +
geom_rect(aes(xmin = sel2[1], xmax = sel2[length(sel2)],
ymin = -Inf, ymax = Inf), alpha = 0.5, fill = "blue")
# Histogramm preparation
# create another df that contains the selection of the two selections
pdat <- rbind(data.frame(y = dat[dat$x %in% sel1, 2],
sel = 1),
data.frame(y = dat[dat$x %in% sel2, 2],
sel = 2))
# plot the histograms
ggplot(pdat, aes(x = y, fill = as.factor(sel))) +
geom_histogram(alpha = 0.5, position = "dodge")
现在我希望用户能够使用闪亮来移动这些区域(最好通过拖动图 1 中的阴影区域!)。
我玩弄了闪亮的(新)交互式选项(更多信息here,查找“交互式绘图”部分)。我想我记得有一个选项可以指定一个区域,用户可以拖动它,但我再也找不到了。
有什么想法吗?
【问题讨论】:
-
您查看过
D3库吗? -
我认为您应该自己研究这些 JS 库(如建议的那样)并阅读可以练习的教程。常见的是
rCharts、HighCharts、D3,因为你的目标不是那么简单 -
尽管我很喜欢
rCharts,但有一个选项使用ggplot2和闪亮的交互能力。我想我在这个闪亮的应用程序中找到了一个示例:shiny.rstudio.com/gallery/plot-interaction-advanced.html 如果您使用BRUSH方向 = "x",您将获得一个可用于指定第二个图形输入的区域!不需要 JS,但... -
数据框必须是索引对象还是时间序列?
-
我更喜欢 data.frame 或 data.table 并尽量避免使用
xts-objects!
标签: r ggplot2 shiny interactive