R 查找geom_抖动使用的随机x值
我希望能够从顶部有抖动点的方框图中选择观察结果。通过点击找到类别,查看y值并选择观察值,我取得了一定的成功。以下代码显示了我到目前为止的进度:R 查找geom_抖动使用的随机x值,r,ggplot2,shiny,R,Ggplot2,Shiny,我希望能够从顶部有抖动点的方框图中选择观察结果。通过点击找到类别,查看y值并选择观察值,我取得了一定的成功。以下代码显示了我到目前为止的进度: # ------------------------------Load Libraries--------------------------------- library(shiny) library(ggplot2) library(dplyr) # -------------------------Print Boxplot to Scree
# ------------------------------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_数据函数,感谢Peter Ellis建议我使用geom_point而不是geom_jitter。这两条评论都有助于我解决问题 我要做的是在全局环境中创建一个新的plot对象来抖动这些点。然后使用layer_data函数返回新创建的x值 最后,使用这些x值,我创建了一个新的plot对象,并使用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)
我感谢aosmith告诉我有关layer_数据函数的信息,感谢Peter Ellis建议我使用geom_point而不是geom_jitter,这两条评论都有助于我解决问题 我要做的是在全局环境中创建一个新的plot对象来抖动这些点。然后使用layer_data函数返回新创建的x值 最后,使用这些x值,我创建了一个新的plot对象,并使用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的建议很容易地解决了这个问题,我自己使用抖动抖动点
我把它变成了一个答案,因为我认为它应该更明显,当我看这页的时候,我差点错过了它 为了像我这样可能在谷歌上搜索这个问题的人的利益,我使用彼得·埃利斯(Peter Ellis)的建议很容易地解决了这个问题,他建议我自己使用抖动来抖动这些点
我把它变成了一个答案,因为我认为它应该更明显,当我看这页的时候,我差点错过了它 也许你可以通过layer_数据提取用于绘图的数据集?如果您将绘图命名为g1,它将类似于layer_datag1,i=2。与其尝试从geom_抖动中获取x坐标,我建议您在绘制管道的绘图部分之前,使用geom_point制作x坐标。也就是说,作为第一步,你自己进行抖动。也许你可以通过layer_数据拉出用于绘图的数据集?如果您将绘图命名为g1,它将类似于layer_datag1,i=2。与其尝试从geom_抖动中获取x坐标,我建议您在绘制管道的绘图部分之前,使用geom_point制作x坐标。也就是说,作为第一步,你自己做一些紧张的事情。