在条形图上,"销售"和"份额"变量通过条形图显示,而"成本"通过红线显示.现在我想删除这条红线,只在方框中保留数字,并在图例图中添加相应的变量.此外,我想将"share"的平均值作为Y轴上的一条水平线相加

df <- data.frame (model  = c("A", "B", "C","D","E","F"),
                      share = c(12,20,15,9,60,20),
                      sale = c(16,25,18,14,67,28),
                      cost = c(14,19,28,24,57,28))

#set levels of model by cost
df$model <- factor(df$model, levels = arrange(df, desc(df$cost))$model)

library(tidyverse)

df_long <- df %>% 
  pivot_longer(
    cols = -model
  ) 


df_long %>% 
  filter(name != "cost") %>% 
  plot_ly(x = ~model, y = ~value, color = ~name, type = "bar", 
          customdata = ~name,  colors = c("blue", "gray"),
          hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
                                 "Name: %{customdata}<extra></extra>")) %>%
  add_lines(inherit = F, data = df, x = ~model, 
            y = ~cost, color = I("red"),
            name = "cost",
            hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
                                   "Name: cost<extra></extra>")) %>% 
  add_annotations(data = df, x = ~model, y = ~cost, text = ~cost,
                  bgcolor = "white", bordercolor = "black", 
                  xshift = 15, yshift = 15, showarrow = F) %>% 
  layout(barmode = "group")

enter image description here

推荐答案

实际上,这比你想象的要容易得多. colored颜色 不是I('red'),而是可以更改为I('transparent').现在,按成本计算的盒子看起来有点令人讨厌.如果它看起来像出售和共享旁边的盒子,可能会更好.

至于水平线,我已经添加了它.我不知道你是否想给它贴上标签,一种特定的 colored颜色 ...只是你想要这条线.这就是我所补充的.我 Select 用红色,因为它很显眼.

df_long %>% 
  filter(name != "cost") %>% 
  plot_ly(x = ~model, y = ~value, color = ~name, type = "bar", 
          customdata = ~name, colors = c("blue", "gray"),
          hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
                                 "Name: %{customdata}<extra></extra>")) %>%
  add_lines(inherit = F, data = df, x = ~model, 
            y = ~cost, color = I("transparent"),
            name = "cost",
            hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
                                   "Name: cost<extra></extra>")) %>% 
  add_annotations(data = df, x = ~model, y = ~cost, text = ~cost,
                  bgcolor = "white", bordercolor = "black", 
                  xshift = 15, yshift = 15, showarrow = F) %>% 
  layout(barmode = "group",
         shapes = list(type = "line", x0 = 0, x1 = 1, xref = "paper",
                       y0 = mean(df$share), y1 = mean(df$share),
                       line = list(color = "red"),
                       name = "Average Share")) -> pL

or = ("function(el){
      costLeg = document.querySelectorAll('g.traces')[2];    /* third legend entry */
      costLC = costLeg.lastChild.cloneNode(true); /* copy the current rect element */
      costLC.removeAttribute('pointer-events');       /* remove the pointer events */
      costLeg.removeAttribute('class');             /*stop refresh from changing it*/
      costLC.setAttribute('x', 15);                       /* starting point of box */
      costLC.setAttribute('y', -5);          /* 12 from middle; account for stroke */
      costLC.setAttribute('width', 11);                  /* 12; account for stroke */
      costLC.setAttribute('height', 10);     /* 12 from middle; account for stroke */
      costLC.setAttribute('style',
          'fill: rgb(0, 0, 0); fill-opacity: 0; stroke-width: 2px; stroke: black;');
      costLeg.insertBefore(costLC, costLeg.lastChild);
      }")

pL %>% htmlwidgets::onRender(or)

enter image description here

R相关问答推荐

在之前合并的数据.tables中分配新列后.internal.selfref无效

如何通过Exams2黑板对非整数字的问题进行评分

按崩溃类别分类的指数

如果索引重复,聚合xts核心数据

在垂直轴中包含多个ggplot2图中的平均值

如何计算R数据集中每个女性的子元素数量?

仅在Facet_WRAP()中的相应方面包含geom_abline()

Data.table';S GForce-将多个函数应用于多列(带可选参数)

使用RSelenium在R中抓取Reddit时捕获多个标签

从R中发出的咕噜声中的BUG?

如何使用For-R循环在向量中找到一系列数字

如何在R中使用混合GAM模型只对固定的影响因素进行适当的预测?

将多个列合并为一个列的有效方法是什么?

创建在文本字符串中发现两个不同关键字的实例的数据框

快速合并R内的值

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

R将函数参数传递给ggploy

附加中缀操作符

如何将图例文本添加到图例符号中