在ggplotly散点图中添加自定义数据标签

2024-05-14

我想显示Species对于每个数据点,当光标位于该点上方而不是 x 和 y 值时。我用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")))

 }) 




}

1. 工具提示

您可以通过多种方式更改工具提示,如下所述here https://stackoverflow.com/questions/36325154/how-to-choose-variable-to-display-in-tooltip-when-using-ggplotly。只是为了展示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点击事件 https://plot.ly/r/shiny-coupled-events/获取单击的点,然后添加geom_text层到你的ggplot.

3. 最小示例

我已经修改了您的代码以制作一个更简单的示例。一般来说,如果您创建一个最小的例子 https://stackoverflow.com/a/48343110/8099834并删除应用程序中不需要重新创建问题的部分(例如更改字体)。

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)

编辑:保留所有标签

您可以使用以下命令将每次点击存储在反应式 data.frame 中reactiveValues并使用这个 data.frame 为您geom_text layer.

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在ggplotly散点图中添加自定义数据标签 的相关文章

  • 如何通过组度量的平均值在 df 内排列 dplyr:: 组?

    借鉴吴卡拉的设计https stackoverflow com a 26555424 9350837 https stackoverflow com a 26555424 9350837答案 我希望根据各个组汇总测量的平均值对分组 df 进
  • Shiny :针对所有错误显示一条消息

    我在 R 的 Shiny 中有一个应用程序 我想处理消息 以便用户看不到发生了什么错误 我知道通过 tags style type text css shiny output error visibility hidden shiny ou
  • R 中具有 p 值的相关矩阵

    假设我想要传导相关矩阵 library dplyr data iris iris gt select if is numeric gt cor y iris Petal Width method spearman gt round 2 现在
  • 如何在R中创建for循环? [复制]

    这个问题在这里已经有答案了 可能的重复 如何在 R 编程中启动 for 循环 https stackoverflow com questions 4162363 how to start a for loop in r programmin
  • R 无法回忆起内存中的对象

    我正在构建一个包含多个步骤的函数 其中每个步骤都会创建一个对象 某个步骤失败 temp3 并且无法找到前面的步骤对象 错误 未找到对象 temp2 我不知道为什么 我有类似的函数 遵循完全相同的结构 每个步骤都遵循先前创建的对象 在函数内
  • R 获取子字符串和正则表达式?

    我有一组文件名字符串 我想提取 符号之后但文件扩展名之前的所有字符 例如 文件名之一是 HelloWorld you txt 我想返回字符串you 这是我的代码 hashPos grep name fixed TRUE dotPos len
  • 如何对范围内的行进行分组并考虑第三列?

    我有一个遗传数据集 我想对基因组中物理上靠近的遗传变异 行进行分组 我想对每条染色体基因组中某些点范围内的基因进行分组 chrom 我的 点 数据集包含变体 行需要在一定范围内的位置 如下所示 chrom low high 1 500 17
  • R ggplot2 分面保持比率但覆盖/定义输出图大小

    我目前正在使用 ggplot2 来比较不同组的统计数据 每个组属于不同的区域 这是通过运行 R 脚本的 Web 应用程序 tikiwiki CMS 插件 R 完成的 每个区域我可以有 2 到 30 个或更多组 相同的 R 脚本针对唯一网页中
  • ggplot2 中的中心图标题

    这个简单的代码 以及今天早上我的所有脚本 已经开始在 ggplot2 中给我一个偏离中心的标题 Ubuntu version 16 04 R studio version Version 0 99 896 R version 3 3 2 G
  • 在列标题和配对变量中嵌入数据的数据透视表

    假设我有这样的数据 不幸的是 变量值嵌入在列名称中 library tidyr library dplyr dat lt tribble group var1 var meta1 var2 var meta2 group1 5 2 cat
  • 解压 R 数据框中的列表

    我有一个dataframe其中一个字段包含不同长度的列表 我想将该字段中列表的每个元素提取到其自己的字段中 以便我可以将结果收集到一个很长的字段中dataframe每个列表元素都有一个 id 这是一个例子dataframe dat lt s
  • 使用 broom 和 tidyverse 对不同的因变量进行回归

    我正在寻找一个 Tidyverse 扫帚解决方案来解决这个难题 假设我有不同的 DV and a specificIVS 集 我想执行一个考虑每个 DV 和这组特定 IV 的回归 我知道我可以使用类似 for i in 或 apply fa
  • R 中舍入到下一个数量级的算法

    如果标题不清楚 我很抱歉 但我无法简洁地解释它 给定一个浓度向量 我想将最大值四舍五入到下一个数量级 即 345 到 1000 另外 我想将最小值四舍五入到较低的数量级 即 3 2 到 1 这些浓度也可能低于 1 因此例如 0 034 需要
  • 在 R 中计算大矩阵的零空间

    我找不到任何函数或包来计算 a 的零空间或 QR 分解 bigmatrix from library bigmemory 在 R 中 例如 library bigmemory a lt big matrix 1000000 1000 typ
  • R xts 对象中从每日时间序列到每周时间序列

    我正在使用 Zoo 和 xts 包来分析财务数据 ts 包不太合适 因为金融系列有周末 没有可用数据 我读到了 xts 包中可用的 apply 函数 apply daily x FUN apply weekly x FUN apply mo
  • 关于在 LyX 中生成和交叉引用 knitr 图的意见

    我的目标是在 LyX 中包含一个knitr图 我可以在我的文档中交叉引用 我 插入了浮动图像 添加了标题和标签 在浮动图像中插入了 ERT 而不是图像 我所做的图片如下 我在这里检查过类似的问题 但没有人做我所做的事情 所以我在这里问 有没
  • k折交叉验证 - 如何自动获得预测?

    这可能是一个愚蠢的问题 但我只是找不到一个包来做到这一点 我知道我可以编写一些代码来获得我想要的东西 但如果有一个函数可以自动完成它那就太好了 所以基本上我想对 glm 模型进行 k 倍交叉验证 我想自动获取每个验证集的预测和实际值 因此
  • 按新年拆分日期行

    我有来自一家医院的包含许多变量的数据 以及每行的起始日期和截止日期 这告诉我们每行何时 有效 每行的有效期最长为一年 test data frame ID c 10 10 10 12 12 Disease c P P P D P Pass
  • R中的for循环和if函数

    我正在用 R 中的 if 函数编写一个循环 表格如下 ID category 1 a 1 b 1 c 2 a 2 b 3 a 3 b 4 a 5 a 我想使用 for 循环和 if 函数添加另一列来计算每个分组的 ID 如下所示的计数列 I
  • 将 R 中的列中的单引号替换为双引号

    我在 R 中的数据框有一个 A 列 其中有带单引号的字符串数据 Column A Hello World Hi World Good morning world 我想做的是将单引号替换为双引号并实现如下所示的输出 Column A Hell

随机推荐