在绘图线图上添加标签

时间:2019-09-17 13:54:46

标签: r graph plotly

我想在库中将标签添加到3个折线图的上方(每个标记共4个标签)。 这是我想要的数据框和绘图的代码。 在评论中,您可以找到我尝试过但没有起作用的内容:“ add_text(textfont = t,textposition =“ top right”)“

谢谢

library(plotly)

df <- data.frame("Type" = c('A','B','R','M'),
`Nr.of.Delivered`= c(3000081,   300028, 100073, 100016),
`Nr.of.openviewed`=c(900001,   100009, 60006,  60005),
  `Nr.of.unique.clicks`= c(107361,21531,14191,14427),
`open.rate`= c(25.9,32.8,39.4,43.54),
        `click.rate`= c(4,6.4,8.90,10.61), 
"cto"= c(11.57, 17.89,21.52,23.29))

df %>%  
   plot_ly () %>%
   add_trace(x=~Type,
             y=~`Nr.of.Delivered`,
             type='bar',
             name="Nr.of.Delivered",
             marker = list(color = tier_pal, size=0.5)) %>%
   add_trace(x=~Type,
             y=~`open.rate`,
             type="scatter",
             mode="markers+lines",
             yaxis = "y2",
             name = "% of open rate", 
             marker = list(color = " #a67c39"),
             line = list(color = " #a67c39")) %>%
   #add_text(textfont = t, textposition = "top right") %>% 
   add_trace(df ,
             x=~Type,
             y=~`click.rate`,
             type="scatter",
             mode="markers+lines",
             yaxis = "y2",
             name="% of click rate", 
             marker = list(color = " #804638"),
             line = list(color = " #804638") ) %>%
   #add_text(textfont = t, textposition = "top right") %>% 
   add_trace(df ,
             x=~Type,
             y=~cto,
             type="scatter",
             mode="markers+lines",
             yaxis = "y2",
             color="red",
             name="% of CTO", 
             marker = list(color = toRGB( 'dark blue')),
             line = list(color = toRGB( 'dark blue'))) %>%
   #add_text(textfont = t,textposition = "top right") %>% 
   layout (title = plot_name,
           titlefont=list(size=14),
           yaxis2 = list(overlaying = "y",
                         side = "right",
                         name = "Percentages",
                         ticksuffix="%",
                         range=c(0,60),
                         showgrid = F),
           yaxis = list(showgrid=F),
           legend = list(x = 100, y = 1.2)) 

1 个答案:

答案 0 :(得分:0)

我不确定为什么这完全行不通,但是似乎您可以将文本标签直接合并到每个plotly跟踪中。

  add_trace(x=~Type,
            y=~open.rate,
            type="scatter",
            mode="markers+lines",
            yaxis = "y2",
            name = "% of open rate", 
            marker = list(color = " #a67c39"),
            line = list(color = " #a67c39"),
            text =~open.rate, textfont = t, textposition= "top right")

顺便问一下,t参数中的变量textfont = t是什么?