首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找geom_jitter使用的随机x值

查找geom_jitter使用的随机x值
EN

Stack Overflow用户
提问于 2017-01-14 02:31:44
回答 2查看 451关注 0票数 1

我希望能够从顶部抖动点的箱形图中选择观测值。我已经取得了一定的成功,通过点按来查找类别,查看y值并选择观察值。下面的代码显示了我到目前为止的进度:

代码语言:javascript
复制
# ------------------------------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值,但我没有找到任何方法来访问它。任何帮助找到这些信息的人都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-18 02:08:03

感谢aosmith告诉我有关layer_data()函数的信息,感谢Peter Ellis建议我使用geom_point()而不是geom_jitter(),这两条评论都对我解决问题很有帮助。

我要做的是在全局环境中创建一个新的plot对象来抖动这些点。然后使用layer_data()函数返回新创建的x值。

最后,使用这些x值,我创建了一个新的plot对象,并使用geom_point()将这些点分层。对于任何感兴趣的人,这里是完整的代码。

代码语言:javascript
复制
# ------------------------------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)
票数 0
EN

Stack Overflow用户

发布于 2019-05-10 22:12:02

为了让像我这样可能在谷歌上搜索这个问题的人受益,我使用Peter Ellis的建议非常容易地解决了这个问题,我自己使用jitter()来抖动这些点。

我把它变成了一个答案,因为我认为它应该更明显,当我看着这个页面时,我几乎错过了它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41641236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档