簡體   English   中英

查找geom_jitter使用的隨機x值

[英]Finding the random x-values used by geom_jitter

我希望能夠從頂部有抖動點的箱形圖中選擇觀測值。 通過點擊點擊查找類別,查看y值並選擇觀察,我獲得了一些成功。 以下代碼顯示了我目前的進展:

# ------------------------------Load Libraries---------------------------------

library(shiny)
library(ggplot2)
library(dplyr)

# -------------------------Print Boxplot to Screen-----------------------------

ui <- fluidPage(plotOutput('irisPlot', click = 'irisClick'))

server <- function(input, output){

# --------------------------Store Clicked Points-------------------------------  

  clicked <- reactiveValues(rows = rep(TRUE,nrow(iris)))

# ---------------------------Modify the Dataset--------------------------------  

  IRIS <- reactive({iris %>% mutate(index = clicked$rows)})

# ---------------------Select Points Through Plot Click------------------------  

  observeEvent(
    input$irisClick,{
      nS <- iris %>% mutate(selected = rep(FALSE,nrow(iris)))  
      lvls <- levels(iris$Species)
      plant <- lvls[round(input$irisClick$x)]
      pxl <- which(
        sqrt((iris$Sepal.Width-input$irisClick$y)^2) %in%
        min(sqrt((iris$Sepal.Width-input$irisClick$y)^2)) 
      )
      point <- iris[pxl,'Sepal.Width']
      nS[nS$Species == plant & nS$Sepal.Width %in% point,'selected'] <- TRUE
      clicked$rows <- xor(clicked$rows, nS$selected)
    })

# --------------------------Generate the Boxplot-------------------------------  

  output$irisPlot <- renderPlot({
    set.seed(1)
    ggplot(IRIS(), aes(x = Species, y = Sepal.Width))+
      geom_boxplot(na.rm = TRUE,outlier.shape = NA)+
      geom_jitter(
        na.rm = TRUE,
        width = .8,
        aes(shape = index, size = index, colour = index)
      )+
      theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = 'black', fill = NA),
        legend.position = "none"
      )+
      scale_shape_manual(values = c('FALSE'= 1,'TRUE'= 19))+
      scale_size_manual(values = c('FALSE' = 4, 'TRUE'= 2))+
      scale_colour_manual(values = c('TRUE' = "#428BCA", 'FALSE' = '#FAA634'))
  })

}

shinyApp(ui, server)

正如我所說的那樣,代碼大部分都可以使用,但它可能不一致。 有時它無法找到一個點,有時它會選擇一大組點或選擇一個點在盒子圖的另一側。 我認為解決這個問題的最好方法是同時使用x和y坐標來選擇點,因為x值是隨機生成的,我需要geom_jitter()告訴我它對於給定的圖使用了什么x值但是我一直無法找到任何方法來訪問它。 任何幫助找到這些信息將不勝感激。

感謝aosmith告訴我有關layer_data()函數的信息,感謝Peter Ellis建議我使用geom_point()而不是geom_jitter(),這兩條評論都有助於我解決我的問題。

我要做的是在全局環境中創建一個新的繪圖對象來抖動點。 然后使用layer_data()函數返回新創建的x值。

最后,使用這些x值,我創建了一個新的繪圖對象,並使用geom_point()將點分層。 以下是任何感興趣的人的完整代碼。

# ------------------------------Load Libraries---------------------------------

library(shiny)
library(ggplot2)
library(dplyr)

# ----------------------------Generate X Coords--------------------------------

set.seed(1)
g1 <- ggplot(iris, aes(x = Species, y = Sepal.Width))+
  geom_boxplot(na.rm = TRUE,outlier.shape = NA)+
  geom_jitter(na.rm = TRUE,width = .8)
xPoints <- layer_data(g1, i = 2)$x

# -------------------------Print Boxplot to Screen-----------------------------

ui <- fluidPage(
  plotOutput('irisPlot', click = 'irisClick')
)

server <- function(input, output){

# --------------------------Store Clicked Points-------------------------------  

  clicked <- reactiveValues(rows = rep(TRUE,nrow(iris)))
  rand <- reactiveValues(x = rep(NA,nrow(iris)))

# ---------------------------Modify the Dataset--------------------------------  

  IRIS <- reactive({iris %>% mutate(index = clicked$rows)})

# ---------------------Select Points Through Plot Click------------------------  

  observeEvent(
    input$irisClick,{
      nS <-data.frame( iris,  x = xPoints)
      point <- nearPoints(
        df = nS,
        coordinfo = input$irisClick,
        xvar = 'x',
        yvar = 'Sepal.Width',
        allRows = TRUE
      )
      clicked$rows <- xor(clicked$rows, point$selected_)
    })

# --------------------------Generate the Boxplot-------------------------------  

  output$irisPlot <- renderPlot({
   ggplot(IRIS(), aes(x = Species, y = Sepal.Width))+
      geom_boxplot(na.rm = TRUE,outlier.shape = NA)+
      geom_point(
        aes(
          x = xPoints,
          y = iris$Sepal.Width,
          shape = index,
          size = index,
          colour = index 
        ),
        inherit.aes = FALSE
      )+
      theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = 'black', fill = NA),
        legend.position = "none"
      )+
      scale_shape_manual(values = c('FALSE'= 1,'TRUE'= 19))+
      scale_size_manual(values = c('FALSE' = 4, 'TRUE'= 2))+
      scale_colour_manual(values = c('TRUE' = "#428BCA", 'FALSE' = '#FAA634'))
  })
  output$x <- renderPlot({

  })
}

shinyApp(ui, server)

只是為了像我這樣可能在谷歌搜索這個問題的人的利益,我很容易使用Peter Ellis建議用抖動()自己抖動點來解決它。

我正在把它變成一個答案,因為我認為它應該更加明顯,當我看到這個頁面時,我幾乎錯過了它。

暫無
暫無

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

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