r - 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)
解决方案
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)
推荐阅读
- python - 400 错误,在 Flask 应用程序中加载带有表单和数据库的路由时
- excel - 插入大量数据
- reactjs - Map 方法不适用于基于 Firestore 的 Next 应用程序
- python - 从图像中获取文本的更快方法
- javascript - 如何使用 Pug 缩小/丑化内联 JavaScript?
- python - 当用户猜对了单词时,如何停止 Hangman python 程序?
- react-native - Storybook 在 Web 和 IOS 上的普通应用程序中都反应原生
- c# - 密码保护使用 NPOI 打开 Excel (xlsx) 文件
- html - Jekyll Github 页面未在 404.md 上加载 CSS
- python - 在 Anaconda 中使用 pip-sync requirements.txt?