【问题标题】:Add custom data label in ggplotly scatterplot在 ggplotly 散点图中添加自定义数据标签
【发布时间】:2018-09-02 10:12:13
【问题描述】:

我想在光标位于每个数据点而不是 x 和 y 值时显示每个数据点的 Species。我使用iris 数据集。此外,我希望能够单击数据点以使标签持久化,并且当我在图中选择一个新点时不会消失。 (如果可能的话 )。基本是标签。持久性问题是一个优点。这是我的应用程序:

## Note: extrafont is a bit finnicky on Windows, 
## so be sure to execute the code in the order 
## provided, or else ggplot won't find the font

# Use this to acquire additional fonts not found in R
install.packages("extrafont");library(extrafont)
# Warning: if not specified in font_import, it will 
# take a bit of time to get all fonts
font_import(pattern = "calibri")
loadfonts(device = "win")

#ui.r
library(shiny)
library(ggplot2)
library(plotly)
library(extrafont)
library(ggrepel)
fluidPage(

  # App title ----
  titlePanel(div("CROSS CORRELATION",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Input CSV-File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),


      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")





    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Table",
                           shiny::dataTableOutput("contents")),
                  tabPanel("Correlation Plot",
                           tags$style(type="text/css", "
           #loadmessage {
                                      position: fixed;
                                      top: 0px;
                                      left: 0px;
                                      width: 100%;
                                      padding: 5px 0px 5px 0px;
                                      text-align: center;
                                      font-weight: bold;
                                      font-size: 100%;
                                      color: #000000;
                                      background-color: #CCFF66;
                                      z-index: 105;
                                      }
                                      "),conditionalPanel(condition="$('html').hasClass('shiny-busy')",
                                                          tags$div("Loading...",id="loadmessage")
                                      ),
                           fluidRow(
                             column(3, uiOutput("lx1")),
                           column(3,uiOutput("lx2"))),
                           hr(),
                           fluidRow(
                             tags$style(type="text/css",
                                        ".shiny-output-error { visibility: hidden; }",
                                        ".shiny-output-error:before { visibility: hidden; }"
                             ),
                           column(3,uiOutput("td")),
                           column(3,uiOutput("an"))),
                           fluidRow(
                           plotlyOutput("sc"))
      ))
  )))
#server.r
function(input, output) {


  output$contents <- shiny::renderDataTable({

    iris
  })


  output$lx1<-renderUI({
    selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                choices = colnames(iris[,1:4]), 
                selected = "Lex1")
  })
  output$lx2<-renderUI({
    selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                choices = colnames(iris[,1:4]), 
                selected = "Lex2")
  })

  output$td<-renderUI({
    radioButtons("td", label = h4("Trendline"),
                 choices = list("Add Trendline" = "lm", "Remove Trendline" = ""), 
                 selected = "")
  })

  output$an<-renderUI({

    radioButtons("an", label = h4("Correlation Coefficient"),
                 choices = list("Add Cor.Coef" = cor(subset(iris, select=c(input$lx1)),subset(iris, select=c(input$lx2))), "Remove Cor.Coef" = ""), 
                 selected = "")
  })  


 output$sc<-renderPlotly({

   p1 <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2))+

     # Change the point options in geom_point
     geom_point(color = "darkblue") +
     # Change the title of the plot (can change axis titles
     # in this option as well and add subtitle)
     labs(title = "Cross Correlation") +
     # Change where the tick marks are
     scale_x_continuous(breaks = seq(0, 2.5, 30)) +
     scale_y_continuous(breaks = seq(0, 2.5, 30)) +
     # Change how the text looks for each element
     theme(title = element_text(family = "Calibri", 
                                size = 10, 
                                face = "bold"), 
           axis.title = element_text(family = "Calibri Light", 
                                     size = 16, 
                                     face = "bold", 
                                     color = "darkgrey"), 
           axis.text = element_text(family = "Calibri", 
                                    size = 11))+
     theme_bw()+
     geom_smooth(method = input$td)+
     annotate("text", x = 10, y = 10, label = as.character(input$an))
   ggplotly(p1) %>%
     layout(hoverlabel = list(bgcolor = "white", 
                              font = list(family = "Calibri", 
                                          size = 9, 
                                          color = "black")))

 }) 




}

【问题讨论】:

    标签: r shiny plotly ggplotly


    【解决方案1】:

    1.工具提示

    您可以通过多种方式更改工具提示,如 here 所述。要在工具提示中只显示Species,应该可以这样:

    library(ggplot2)
    library(plotly)
    p1 <- ggplot(iris, aes_string(x = "Sepal.Length", 
                                    y = "Sepal.Width",
                                    key = "Species")) +
          geom_point()
    ggplotly(p1, source = "select", tooltip = c("key"))
    

    2。永久标签

    我不确定如何在单击时将plotly 工具提示留在该点上,但您可以使用plotly click event 来获取单击点,然后将geom_text 图层添加到您的ggplot

    3.最小示例

    我已经修改了您的代码以制作一个更简单的示例。通常,如果您创建一个 minimal example 并删除应用程序中不需要重新创建您的问题的部分(例如更改字体),这会很有帮助。

    library(shiny)
    library(plotly)
    library(ggplot2)
    
    ui <- fluidPage(
      plotlyOutput("iris")
    )
    
    server <- function(input, output, session) {
      output$iris <- renderPlotly({
          # set up plot
          p1 <- ggplot(iris, aes_string(x = "Sepal.Length", 
                                        y = "Sepal.Width",
                                        key = "Species")) +
              geom_point()
    
          # get clicked point
          click_data <- event_data("plotly_click", source = "select")
          # if a point has been clicked, add a label to the plot
          if(!is.null(click_data)) {
              label_data <- data.frame(x = click_data[["x"]],
                                       y = click_data[["y"]],
                                       label = click_data[["key"]],
                                       stringsAsFactors = FALSE)
             p1 <- p1 + 
                 geom_text(data = label_data,
                           aes(x = x, y = y, label = label),
                           inherit.aes = FALSE, nudge_x = 0.25)
          }
          # return the plot
          ggplotly(p1, source = "select", tooltip = c("key"))
      })
      }
    
    shinyApp(ui, server)
    

    编辑:保留所有标签

    您可以使用reactiveValues 将每次点击存储在响应式data.frame 中,并将此data.frame 用于您的geom_text 层。

    library(shiny)
    library(plotly)
    library(ggplot2)
    
    ui <- fluidPage(
        plotlyOutput("iris")
    )
    
    server <- function(input, output, session) {
        # 1. create reactive values
        vals <- reactiveValues()
        # 2. create df to store clicks
        vals$click_all <- data.frame(x = numeric(),
                                    y = numeric(),
                                    label = character())
        # 3. add points upon plot click
        observe({
            # get clicked point
            click_data <- event_data("plotly_click", source = "select")
            # get data for current point
            label_data <- data.frame(x = click_data[["x"]],
                                     y = click_data[["y"]],
                                     label = click_data[["key"]],
                                     stringsAsFactors = FALSE)
            # add current point to df of all clicks
            vals$click_all <- merge(vals$click_all,
                                    label_data, 
                                    all = TRUE)
        })
        output$iris <- renderPlotly({
            # set up plot
            p1 <- ggplot(iris, aes_string(x = "Sepal.Length", 
                                          y = "Sepal.Width",
                                          key = "Species")) +
                geom_point() + 
                # 4. add labels for clicked points
                geom_text(data = vals$click_all,
                          aes(x = x, y = y, label = label),
                          inherit.aes = FALSE, nudge_x = 0.25)
            # return the plot
            ggplotly(p1, source = "select", tooltip = c("key"))
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这是一个非常有帮助的特殊答案。是否可以在单击后保持显示的值并且在选择另一个位置时不会消失?我将编辑我的初始帖子以使其清楚,因为我知道我描述它的方式可能会造成混淆。
    • @firmo23 乐于助人!我已经用一个选项更新了我的答案。让我知道它是否适合你
    • 您的代码似乎适用于该数据集,但在尝试将其应用于我的实际数据集时出现一个奇怪的错误。特别是我无法生成 geom_smooth。直到最近我还可以。我在谈论我的实际数据集,因为我可以用 iris 制作。我想知道是否可以私下与您联系。再次感谢
    • 我创建了一个可能对stackoverflow.com/questions/49502917/…987654326@有帮助的新问题
    • 另一个问题是,当最初创建情节时,我将点与一条线连接起来,当我更新它时,这条线消失了。
    猜你喜欢
    • 2014-06-28
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多