文本标注中有无可能用R语言消除标注?

db2dz4w8  于 12个月前  发布在  R语言
关注(0)|答案(1)|浏览(112)

我使用这些数据:
| 月年|推销员|客户服务|投资组合|销售|单位|
| - -----|- -----|- -----|- -----|- -----|- -----|
| 2022年1月2日|0015| 0| 0| 0| 0|
| 2022年1月3日|0015| 0| 0| 0| 0|
| 2022年1月4日|0015| 1| 1| 62709.12| 4.56|
| 2022年1月5日|0015| 5个|4| 266568.56|二十六点九七|
| 2022年1月6日|0015|十四|七个|1251578.81|二百四十七点七六|
| 2022年1月7日|0015|十五|六|1419364.39| 333.6|
要在plotly R中创建双曲线图,请执行以下操作:

p <- plot_ly(df, x = ~`Month-year`, y = ~Portafolio, type = 'scatter', mode = 'lines+markers', name = "Portafolio",marker=list(color='#2ca02c',line = list(color = '#2ca02c')),line=list(color='#2ca02c')) %>%
add_trace(x = ~`Month-year`, y = ~Clients, type = 'scatter', mode = 'lines+markers', yaxis = "y2", name = "Clients",marker=list(color='#ff7f0e'),line=list(color='#ff7f0e')) %>%
layout(yaxis2 = list(title="Clients", side = "right", overlaying = "y", side ="right", automargin=T, showgrid=FALSE,rangemode='tozero'), yaxis=list(showgrid=FALSE,rangemode='tozero'), xaxis=list(showticklabels=TRUE,visible=TRUE),title = "0015",legend = list(orientation = 'h',x = 0, y = 1), shapes = list(list(type = "rect", text = 'Festival', fillcolor = "green", line = list(color = "green"),opacity = 0.2, y0 = 0.0, y1 = max(df$Clients), x0 = "2022-06-01", x1 = "2022-07-01",plot_bgcolor = "#e5ecf6"))) %>%
add_text(showlegend = FALSE,x = "2022-06-15", y = max(df$Clients)+max(df$Clients)*0.03, text = "Festival",textfont = list(color = 'green', size = 15))

问题是不希望标记位于文本注解的中心:
In the text annotation "Festival" in a mark in the middle :-(
有人能帮我吗?

30byixjq

30byixjq1#

将inherit设置为false,如下所示:

add_text(..., inherit = FALSE)

因为看起来标记模式被向下传递到add_text层。
因此:

df <- read.table(
  text =
    "
Month-year  Salesman    Clients Portafolio  Sales   Units
1/2/2022    0015    0   0   0   0
1/3/2022    0015    0   0   0   0
1/4/2022    0015    1   1   62709.12    4.56
1/5/2022    0015    5   4   266568.56   26.97
1/6/2022    0015    14  7   1251578.81  247.76
1/7/2022    0015    15  6   1419364.39  333.6
", header = T, check.names = F
)

plot_ly(df,
  x = ~`Month-year`,
  y = ~Portafolio,
  type = "scatter",
  mode = "lines+markers",
  name = "Portafolio",
  marker = list(color = "#2ca02c", line = list(color = "#2ca02c")),
  line = list(color = "#2ca02c")
) %>%
  add_trace(
    x = ~`Month-year`,
    y = ~Clients,
    type = "scatter",
    mode = "lines+markers",
    yaxis = "y2", name = "Clients",
    marker = list(color = "#ff7f0e"),
    line = list(color = "#ff7f0e")
  ) %>%
  layout(
    yaxis2 = list(
      title = "Clients", side = "right", overlaying = "y",
      side = "right", automargin = T, showgrid = FALSE,
      rangemode = "tozero"
    ), yaxis = list(
      showgrid = FALSE,
      rangemode = "tozero"
    ),
    xaxis = list(showticklabels = TRUE, visible = TRUE), title = "0015",
    legend = list(orientation = "h", x = 0, y = 1), shapes =
      list(list(
        type = "rect", text = "Festival", fillcolor = "green",
        line = list(color = "green"), opacity = 0.2, y0 = 0.0,
        y1 = max(df$Clients), x0 = "2022-06-01", x1 = "2022-07-01",
        plot_bgcolor = "#e5ecf6"
      ))
  ) %>%
  add_text(
    showlegend = FALSE, x = "2022-06-15", y = max(df$Clients) + max(df$Clients) * 0.03,
    text = "Festival", textfont = list(color = "green", size = 15), inherit = F
  )

相关问题