Двойные аннотации для точки рассеяния на графическом графике с использованием ggplot - PullRequest
1 голос
/ 24 марта 2019

Я разработал блестящее приложение для построения двухплоскостного графика.

Один из графиков представляет собой точечный график, и выбранная точка будет выделена изменением цвета и размера.Мои коды здесь:

output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)

# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)


g <- ggplot(Kcl_V %>% slice(-subject_id), aes(x = Vd, y = Cl)) + # Initialize ggplot object
  geom_point(colour = "#F8766D",size = 3)+
  geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).

}) Информация отображается правильно, однако выбранная точка всегда имеет двойные аннотации, подобные этой: enter image description here

Любойзнаете, как этого избежать?Спасибо!

1 Ответ

1 голос
/ 25 марта 2019

Это происходит потому, что вы установили aes в обоих ggplot() и втором вызове на geom_point(), поэтому для всех данных во втором geom_point у вас есть 2 набора эстетических отображений, что приводит к 2 наборам всплывающих подсказок .

Есть 2 способа решить эту проблему. Во-первых, вы можете удалить набор aes в ggplot и вместо этого установить его в каждом geom_point отдельно, один набор данных и эстетическое отображение для выбранной точки и один для невыбранных точек.

output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)

  # Select the data for the chosen compartment using a switch statement.
  # For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
  subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)

  g <- ggplot() + # Initialize ggplot object
    geom_point(data = Kcl_V[-subject_id, ],aes(x = Vd, y= Cl),colour = "#F8766D",size = 3)+
    geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
  p <- ggplotly(g) # Convert to a plotly object.
  # Doesn't create legend, but when hover over, does give label (which has to be the column name).
})

В качестве альтернативы, вы можете создать логическую переменную в Kcl_V, которая указывает, принадлежит ли строка выбранной точке, и использовать эту переменную для установки эстетических отображений цвета и размера.

output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)

  # Select the data for the chosen compartment using a switch statement.
  # For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
  selected_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)

  g <- ggplot(Kcl_V %>% dplyr::mutate(selected = subject_id == selected_id),aes(x = Vd, y= Cl,color = selected, size = selected)) + # Initialize ggplot object
    scale_color_manual(values = c("TRUE"="#00BFC4","FALSE"="#F8766D")) +
    scale_size_manual(values = c("TRUE"=4,"FALSE"=3))
  p <- ggplotly(g) # Convert to a plotly object.
  # Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
...