簡體   English   中英

R 閃亮畫筆放大繪圖范圍

[英]R shiny brush zoom in plot ranges

在此處輸入圖片說明 我開發了一個交互式相關熱圖: https : //lingjun.shinyapps.io/code/

問題是,當我在左圖上用畫筆放大並獲得正確的圖時,右圖左下角的 x 和 y 軸總是有過多的標簽。 我希望標簽能被銳利地剪掉。

服務器類中的代碼是:

observe({
    brush <- input$zoom_brush
    if (!is.null(brush)) {
        ranges$x <- c(round(brush$xmin), round(brush$xmax))
        ranges$y <- c(round(brush$ymin), round(brush$ymax))
    } else {
        ranges$x <- NULL
        ranges$y <- NULL
    }
})
output$zoomplot <- renderPlot({


        x.index <- y.index <- 1:300

        zoomplot <- ggplot(melt(result[x.index, y.index]), aes(Var1, Var2, fill = value)) + geom_tile() + xlab("Q1") + ylab("Q2")+ scale_fill_gradient2(low = "blue",  high = "red",limits=c(-1, 1), guide=FALSE)+coord_cartesian(xlim = ranges$x, ylim = (ranges$y),expand=F)



        zoomplot

    },  height = 500, width = 500)

這是用戶界面代碼:

shinyUI(fluidPage(


  titlePanel("SCIP survey response correlation heatmap"),
  selectInput("Correlation", 
          label = "Choose which to display",
          choices = list("corPSR", "Spearman", "difference"),
          selected = "corPSR"),

  fluidRow(
column( width=5,

  h4("Click and drag to zoom in"),
  plotOutput("heatmap", 
             #click = "plot1_click",
             brush = brushOpts( id = "zoom_brush", resetOnNew = TRUE)),
  h4("Points near click"),
  verbatimTextOutput("click_info")),

column(width=7,

  h4("Click to see details"),
  plotOutput("zoomplot", click="plot1_click"))

))

棘手。 這可以說是coord_cartesian一個錯誤,但只發生在因子坐標中。

讓它起作用的一種方法是不用它,只過濾融化的數據框。 請注意,您正在過濾因子坐標的整數值。

這里有一些代碼可以滿足您的需求。

library(shiny)
library(reshape2)

n1 <- 90000
n2 <- 90000
nr <- 300
nc <- 300
set.seed(1)
x <- matrix(rnorm(n1), nrow=nr, ncol=nc)
y <- matrix(rnorm(n2), nrow=nr, ncol=nc)
result <- cor(x,y)

ui <- fluidPage(
  mainPanel(
    h2("baseplot"),plotOutput("baseplot",width="100%", height="600px",brush="zoom_brush"),
    h2("zoomplot"),plotOutput("zoomplot",width="100%", height="600px")
  )
)

## server.R
server <- function(input, output) {

  ranges <- reactiveValues(x=NULL,y=NULL)
  observe({
    brush <- input$zoom_brush
    if (!is.null(brush)) {
      ranges$x <- c(round(brush$xmin), round(brush$xmax))
      ranges$y <- c(round(brush$ymin), round(brush$ymax))
    } else {
      ranges$x <- NULL
      ranges$y <- NULL
    }
  })
  meltDf <- reactive({
    x.index <- y.index <- 1:300
    mdf <- melt(result[x.index, y.index])
    mdf$Var1 <- as.factor(mdf$Var1)
    mdf$Var2 <- as.factor(mdf$Var2)
    mdf
  })
  output$baseplot <- renderPlot({

    mdf <- meltDf()

    ggplot(mdf, aes(Var1, Var2, fill = value)) + 
      geom_tile() + xlab("Q1") + ylab("Q2")+ 
      scale_fill_gradient2(low = "blue",  high = "red",limits=c(-1, 1), guide=FALSE)
  },  height = 500, width = 500)

  output$zoomplot <- renderPlot({

    if (is.null(ranges$x)) return(NULL)

    mdf <- meltDf()
    print(ranges$x)
    print(ranges$y)
    mdf <- mdf[ ranges$x[1]<=as.integer(mdf$Var1) & as.integer(mdf$Var1)<= ranges$x[2],]
    mdf <- mdf[ ranges$y[1]<=as.integer(mdf$Var2) & as.integer(mdf$Var2)<= ranges$y[2],]
    ggplot(mdf, aes(Var1, Var2, fill = value)) + 
            geom_tile() + xlab("Q1") + ylab("Q2")+ 
            scale_fill_gradient2(low = "blue",high = "red",limits=c(-1, 1), guide=FALSE)
  },  height = 500, width = 500)
}
shinyApp(ui,server)

它看起來像這樣: 在此處輸入圖片說明

這似乎是一個在 ggplot2 中已被糾正的舊錯誤,但以某種方式重新出現在這種閃亮的情節中?

只要您不再設置限制,除了 coord_cartesian 之外,您還可以使用比例。 弄清楚如何設置中斷和標簽仍然很棘手,這取決於您的變量是因子還是字符。

您將需要 scale_x_discrete() 和 scale_y_discrete() 並且在字符變量的情況下,如下所示:

p + scale_x_discrete(breaks = sort(plot_data$Var1)[round(min(ranges$x)):round(max(ranges$x))], labels = sort(plot_data$Var1)[round(min(ranges$x)):round(max(ranges$x))])

在索引之前對變量進行排序應該模仿 ggplot 的繪圖行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM