[英]Rescaling of y axis on an interactive plot depending on zoom
我在rshiny上有一个交互式图,我希望轴根据用户缩放的位置动态缩放。 我想我可以将scale_y_continuous
用于scale_y_continuous
的动态元素,但是然后我需要知道用户当前帧的y上下值(已缩放)。 有没有办法用ggplot / rshiny做到这一点?
require(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel(title=h4("example", align="center")),
mainPanel(plotlyOutput("plot"))
)
##server
server <- function(input,output){
dat<-reactive({
num<-c(1,2,3,4,5,6,7,8,9)
let<-c("A","B","C","D","E","F","G","H","I")
df<-data.frame(num,let)
df
})
output$plot<-renderPlotly({
gg <- ggplot(dat(),aes(x=let,y=num))+geom_point(aes(colour='red'))
gg2 <- ggplotly(gg) %>% layout(dragmode="select")
gg2
})
}
shinyApp(ui = ui, server = server)
试试看 它不使用密谋,而是相同的基本思想。 本质上,它将过滤数据到笔刷。 双击将完全恢复。
require(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel(title=h4("example", align="center")),
mainPanel(
plotOutput("plot", brush = brushOpts("brush_p")))
)
##server
server <- function(input,output){
dat<-reactive({
num<-c(1,2,3,4,5,6,7,8,9)
let<-c("A","B","C","D","E","F","G","H","I")
df<-data.frame(num,let)
df
})
dat2 <- reactive({
y_max <- input$brush_p$ymax
y_min <- input$brush_p$ymin
if(is.null(y_max)) return(dat())
dat() %>% filter(num > y_min & num < y_max)
})
output$plot<-renderPlot({
gg <- ggplot(dat2(),aes(x=let,y=num))+geom_point(aes(colour='red'))
gg
})
}
shinyApp(ui = ui, server = server)
由reprex软件包 (v0.2.0)创建于2018-08-19。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.