首页 > 解决方案 > R中的文本注释

问题描述

我在图表中添加了文本注释。A不幸的是,当我为一个情节(例如 Product )有多个文本时,我的想法不起作用。如果我们选择产品A,则不会显示任何内容。带有我想在图表和图形上显示的文本的数据框:

        Date Product Value  Text
1 2020-01-03       A    61 Hello
2 2020-01-07       A    27 Hello
3 2020-01-04       B    57 Hello
4 2020-01-05       C   147 Hello
5 2020-01-06       D    31 Hello

在此处输入图像描述

我的代码:

library(shiny)
library(plotly)
library(shinyWidgets)
set.seed(666)

df1 <- data.frame(Date = rep(seq(as.Date("2020-01-03"), by="day", len=12),10,replace = TRUE), 
                  Product = rep(LETTERS[1:10], each = 12), 
                  Value = sample(c(0:300),120, replace = T))

df2 <- data.frame(Date = rep(seq(as.Date("2020-01-03"), by="day", len=5),replace = TRUE), 
                  Product = rep(c(LETTERS[1:4],"A"), each = 1), 
                  Text = rep("Hello",5))

df1 <- full_join(df1, df2, by=c("Product","Date"))

ui <- fluidPage(
  pickerInput("All", "Choose", multiple = F, choices = unique(df1$Product)                                          ,
              options = list(`max-options` = 4,size = 10)),
  plotlyOutput('plot')
  
)
server <- function(input, output) {
  
  trend<- reactive({
    df1 %>% 
      filter(Product %in% input$All) %>% 
      arrange(Date) %>% 
      droplevels()
  })
  output$plot <- renderPlotly({
    
    mdata2 <- trend()
    mdata2<-mdata2 %>% drop_na(Text)

    annotations = list()
    annotation <- list(x = mdata2$Date,
                       y = mdata2$Value,
                       text = mdata2$Text,
                       showarrow = T)
    
    annotations[[i]] <- annotation
      
    plot_ly(data=trend(), x=~Date,  y = ~Value,
            type = 'scatter', mode = 'lines+markers')%>%
      layout(annotations = annotations) 
  })
 
}
shinyApp(ui = ui, server = server)

标签: rshinyannotationsrender

解决方案


Your object annotations[[i]] is not defined properly. Try this

library(shiny)
library(plotly)
library(shinyWidgets)
set.seed(666)

df1 <- data.frame(Date = rep(seq(as.Date("2020-01-03"), by="day", len=12),10,replace = TRUE), 
                  Product = rep(LETTERS[1:10], each = 12), 
                  Value = sample(c(0:300),120, replace = T))

df2 <- data.frame(Date = rep(seq(as.Date("2020-01-03"), by="day", len=5),replace = TRUE), 
                  Product = rep(c(LETTERS[1:4],"A"), each = 1), 
                  Text = rep("Hello",5))

df1 <- full_join(df1, df2, by=c("Product","Date"))

ui <- fluidPage(
  pickerInput("All", "Choose", multiple = F, choices = unique(df1$Product)                                          ,
              options = list(`max-options` = 4,size = 10)),
  plotlyOutput('plot')
  
)
server <- function(input, output) {
  
  trend<- reactive({
    df1 %>% 
      filter(Product %in% input$All) %>% 
      arrange(Date) %>% 
      droplevels()
  })
  output$plot <- renderPlotly({
    
    mdata2 <- trend() %>% drop_na(Text)
    
    annotations <- c() 
    n <- nrow(mdata2)
    if (n>0) {
      lapply(1:n, function(i){
        rowdata <- mdata2[i,]
        annotation <- data.frame(x = rowdata$Date,
                                 y = rowdata$Value,
                                 text = rowdata$Text,
                                 showarrow = T)
        annotations <<- rbind(annotations, annotation)
      })
    }
    
    
    plot_ly(data=trend(), x=~Date,  y = ~Value,
            type = 'scatter', mode = 'lines+markers') %>%
      layout(annotations = annotations)
  })
  
}
shinyApp(ui = ui, server = server)

output


推荐阅读