如何将带边框的数据标签添加到 R plotly 条形图?
How to add bordered data labels to R plotly bar chart?
我正在使用 R 并使用 plotly 的 add_text() 函数将数据标签添加到条形图中。但是,我想知道如何将数据标签添加到填充和带边框的矩形中以使其更易于查看。我不确定我是否可以使用 add_annotations() 来做到这一点。我希望在此图表上呈现类似数据标签的内容:
这是我当前代码的示例:
my_tibble <- tibble(mins = runif(10,10,30),
week = 1:10,
exercise = c("a", "b", "b", "b", "a", "a", "b", "b", "b", "a"),
weight = runif(10,150,160))
plot_ly(data = my_tibble, hoverinfo = "none") %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins,
color = ~exercise,
hovertext = ~weight,
hovertemplate = paste('<b>Week</b>: %{x}', "%{hovertext}", '<extra></extra>')
) %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins
) %>%
layout(
barmode = 'stack'
)%>%
add_text(
x = ~week,
y = 8,
text = "label",
showlegend = F
)
我知道的唯一方法是将文本添加为注释,而不是跟踪。
我将此添加到您的代码中,而没有更改您编写的任何内容:
layout(annotations = list(x = ~week, y = 8, showarrow = F,
bgcolor = "white", text = "label"))
我正在使用 R 并使用 plotly 的 add_text() 函数将数据标签添加到条形图中。但是,我想知道如何将数据标签添加到填充和带边框的矩形中以使其更易于查看。我不确定我是否可以使用 add_annotations() 来做到这一点。我希望在此图表上呈现类似数据标签的内容:
这是我当前代码的示例:
my_tibble <- tibble(mins = runif(10,10,30),
week = 1:10,
exercise = c("a", "b", "b", "b", "a", "a", "b", "b", "b", "a"),
weight = runif(10,150,160))
plot_ly(data = my_tibble, hoverinfo = "none") %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins,
color = ~exercise,
hovertext = ~weight,
hovertemplate = paste('<b>Week</b>: %{x}', "%{hovertext}", '<extra></extra>')
) %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins
) %>%
layout(
barmode = 'stack'
)%>%
add_text(
x = ~week,
y = 8,
text = "label",
showlegend = F
)
我知道的唯一方法是将文本添加为注释,而不是跟踪。
我将此添加到您的代码中,而没有更改您编写的任何内容:
layout(annotations = list(x = ~week, y = 8, showarrow = F,
bgcolor = "white", text = "label"))