【问题标题】:Add text labels to on top of markers (multiple traces) in R plotly将文本标签添加到 R 中的标记(多条迹线)顶部
【发布时间】:2020-06-29 17:04:02
【问题描述】:

我无法在相同的绘图上使用多条轨迹在标记顶部显示文本。代码如下:

plot_ly(x = ~SWITCHING_SUMS_NET()$week, y = ~SWITCHING_SUMS_NET()$V1, 
        name = 'Affordability', type = 'scatter', mode = 'lines+markers', 
        text=~SWITCHING_SUMS_NET()$V1, textposition="top center") %>%
    add_trace(y = ~SWITCHING_SUMS_NET()$V2, name = 'Experience Change', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V2) %>% 
    add_trace(y = ~SWITCHING_SUMS_NET()$V3, name = 'Unavailability', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V3) %>% 
    add_trace(y = ~SWITCHING_SUMS_NET()$V4, name = 'Attracted buy another brand', 
              mode = 'lines+markers', text=~SWITCHING_SUMS_NET()$V4) %>%    
    layout(xaxis = list(title = ''),
           yaxis = list (title = 'Count',showticklabels = T), 
           legend = list(orientation = 'h', xanchor = "center",x = 0.5,y= -0.3))

输出图像:

【问题讨论】:

标签: r shiny plotly shinydashboard r-plotly


【解决方案1】:

当您必须在 ggplot 中的 plotly 或 geoms 中执行多个 add_trace(或其他 plotly 函数)时,请研究将数据框从宽变为长的可能性。这样你就可以创建一个分组列并且只做一个add_trace(这里是ggplot2的一个例子:Making multi-line plots in R using ggplot2)。

此外,最好使用反应函数将数据读入绘图函数之外的闪亮(即plot_data <- SWITCHING_SUMS_NET())变量中。这样,您以后可以对数据应用过滤器、重塑或其他必要的功能,并且只需进行最少的编辑,您的绘图就会再次生成。此外,您的输入数据保持不变。

考虑到上述提示,这是解决此问题的最佳方法。在这里,我有两条痕迹,一条用于文本,另一条用于标记和线条(在plot_ly 函数本身内)。另一种方法是使用我认为效率不高的注释。在这里阅读更多:https://plotly.com/r/text-and-annotations/

## packages
library(shiny)
library(plotly)
library(dplyr)
library(tidyr)

## reading reactive to a variable
plot_data <- SWITCHING_SUMS_NET()

plot_data %>%
   ## rename variables to the names to be used in the plot
   rename_at(c("V1", "V2", "V3", "V4"), 
             list(~c("Affordability", "Experience Change", 
                     "Unavailability", "Attracted buy another brand"))) %>% 
  ## reshape data from wide to long
  pivot_longer(-week) %>% 
 ## plot the rehsaped data 
  ## add a trace for liens and markers with legend
 plot_ly(x = ~week, y = ~value, color = ~name,
         type = 'scatter', mode = 'lines+markers',
         showlegend = T) %>%
  ## add a trace for text without legend
   ## insert `color = I("Black")` to have texts in one for all
  add_text(text=~value, textposition="top center",
            showlegend = F) %>% 
  ## set the layout
  layout(xaxis = list(title = ''),
         yaxis = list (title = 'Count', showticklabels = T), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5, y = -0.3))
  

示例:

我还使用iris 数据集添加了一个插图,因为您尚未共享您的数据。

library(dplyr)
library(tidyr)
library(plotly)

iris %>% 
  filter(Species == "setosa") %>% 
  slice(1:10) %>% 
  arrange(Sepal.Length) %>% 
  pivot_longer(-c(Species, Sepal.Length)) %>% 

 plot_ly(x=~Sepal.Length, y=~value, color = ~name,
         type = 'scatter', mode = 'lines+markers', 
         showlegend = T) %>% 
   add_text(text=~value, textposition="top center",
            showlegend = F) %>% 
   layout(xaxis = list(title = ''),
         yaxis = list (title = 'Count', showticklabels = T), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5, y = -0.3))

【讨论】:

  • 感谢您的提示,我刚刚将模式修改为 'lines+markers+text' 并像魅力一样工作
猜你喜欢
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
相关资源
最近更新 更多