R 在交互式发光打印中选择范围
我试图在shiny中创建数据的交互式可视化。可视化显示了一系列部分的分布或历史图。例如,以下代码创建了一个系列,并对系列的两个部分进行了两次选择,然后使用ggplot显示:R 在交互式发光打印中选择范围,r,ggplot2,shiny,interactive,R,Ggplot2,Shiny,Interactive,我试图在shiny中创建数据的交互式可视化。可视化显示了一系列部分的分布或历史图。例如,以下代码创建了一个系列,并对系列的两个部分进行了两次选择,然后使用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 <-
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中的阴影区域来移动区域!使用闪亮的
我玩了一些新的互动选项,如“更多信息,寻找部分互动情节”。我想我记得有一个选项可以指定一个用户可以拖动的区域,但是我再也找不到了
有什么想法吗?我想我找到了一个解决方案,可以在光亮的环境中使用交互式ggplot。代码如下所示:
library(shiny)
library(ggplot2)
ifna <- function(x, elseval = NA) ifelse(is.na(x) || is.null(x), elseval, x)
# two plots: as described in the question
ui <- fluidPage(
uiOutput("plotui"),
plotOutput("plot2")
)
server = function(input, output) {
set.seed(123)
dat <- data.frame(x = 1:1000,
val = cumsum(rnorm(1000, mean = 0.1)))
base <- 200:400 # Base Selection
# reactive expressions to get the values from the brushed area
selmin <- reactive(round(ifna(input$plot_brush$xmin, elseval = 700), 0))
selmax <- reactive(round(ifna(input$plot_brush$xmax, elseval = 900), 0))
# include the brush option: direction = "x" says that y values are fixed (min and max)
output$plotui <- renderUI({
plotOutput("plot", height = 300,
brush = brushOpts(id = "plot_brush", direction = "x",
fill = "blue", opacity = 0.5)
)
})
# render the first plot including brush
output$plot <- renderPlot({
ggplot() + geom_line(data = dat, aes(x = x, y = val)) +
geom_rect(aes(xmin = base[1], xmax = base[length(base)],
ymin = -Inf, ymax = Inf), alpha = 0.5, fill = "red") +
geom_rect(aes(xmin = 700, xmax = 900,
ymin = -Inf, ymax = Inf), alpha = 0.1, fill = "blue") +
ylab("Value") + xlab("t")
})
# render the second plot reactive to the brushed area
output$plot2 <- renderPlot({
# prepare the data
pdat <- rbind(data.frame(y = dat[dat$x %in% base, "val"],
type = "Base"),
data.frame(y = dat[dat$x %in% selmin():selmax(), "val"],
type = "Selection"))
ggplot(pdat, aes(x = y, fill = type)) +
geom_histogram(alpha = 0.5, position = "dodge") +
scale_fill_manual(name = "", values = c("red", "blue")) +
theme(legend.position = "bottom") + ylab("Frequency") + xlab("Value")
})
}
# run the app
shinyApp(ui, server)
这使得深蓝色框是交互式的,因为你可以把它推来推去,下面的图形会更新
正如评论中提到的,下面是一些修改后的教程示例。请注意,动态图需要一个timeseries对象来绘制,有关更多信息,请参阅官方。摘要统计信息可由您选择的软件包执行。还要注意,阴影区域是用户指定的
rm(list = ls())
library(shiny)
library(dygraphs)
library(xts)
library(rCharts)
index <- as.Date(c(seq(Sys.time(), length.out = 1000, by = "days")))
dat <- data.frame(x = index,y = cumsum(rnorm(1000, mean = 0.1)))
dat <- xts(dat[,-1], order.by=dat[,1])
ui <- fluidPage(
titlePanel("Shaded Regions using dygraphs and rCharts by Pork Chop"),
sidebarLayout(
sidebarPanel(
sliderInput("range_one", "Range One:",min = 100, max = 1000, value = c(200,300)),
sliderInput("range_two", "Range Two:",min = 100, max = 1000, value = c(500,600)),width=3),
mainPanel(
column(12,dygraphOutput("dygraph")),
column(12,showOutput("summary", "Highcharts"))
)
)
)
server <- function(input, output) {
output$dygraph <- renderDygraph({
dygraph(dat, main = "Sample Data") %>%
dyShading(from = index[input$range_one[1]], to = index[input$range_one[2]], color = "#FFE6E6") %>%
dyShading(from = index[input$range_two[1]], to = index[input$range_two[2]], color = "#CCEBD6")
})
output$summary <- renderChart2({
Selection1 <- dat[input$range_one[1]:input$range_one[2]]
Selection2 <- dat[input$range_two[1]:input$range_two[2]]
subset_data <- data.frame(merge(Selection1,Selection2))
a <- rCharts:::Highcharts$new()
a$chart(type = "column")
a$title(text = "Summary Stats")
a$yAxis(title = list(text = "Count"))
a$data(subset_data)
a$exporting(enabled=T)
a$set(width = 1200,height = "100%",slider = TRUE)
return(a)
})
}
shinyApp(ui, server)
你看过D3库了吗?我认为你应该按照建议自己去看看那些JS库,并通过教程进行练习。常见的是rCharts、HighCharts和D3,因为您的目标并不是那么直接,因为我喜欢rCharts,有一个选项使用ggplot2和Shinny的交互功能。我想我在这个闪亮的应用程序中找到了一个例子:如果你使用画笔方向=x,你会得到一个区域,可以用来指定第二个图形的输入!还不需要JS…数据帧必须是索引对象还是timeseries?我更喜欢data.frame或data.table,尽量避免使用xts对象!这也是一个有趣的方法,看起来很有视觉吸引力!但是,我更喜欢我的解决方案,因为它允许更用户友好的交互。如果我没弄错的话,你的方法可以用ggplot实现。不过,谢谢你的回答!