在 r 中使用 plot_ly() 在折线图中的点上方显示文本标签
Display the text label above the point in the line chart using plot_ly() in r
我正在尝试使用 ploy_ly()
函数可视化折线图。我需要在折线图上方显示标签。但是标签显示在点上,看起来很乱。
使用的dataframe如下:
Quarter average
1 2018 Q1 47.6
2 2018 Q2 46.4
3 2018 Q3 45.7
4 2018 Q4 45.5
5 2019 Q1 45.7
6 2019 Q2 46.3
7 2019 Q3 45.7
使用plot_ly()
可视化折线图的代码如下:
plot_ly(teamAverageperweek, x = teamAverageperweek$Quarter,
text = teamAverageperweek$average,
hoverinfo = 'text',
hovertext = paste('Quarter of Date: ', teamAverageperweek$Quarter,
'<br> Avg Hours Per Week: ',teamAverageperweek$average),
showlegend = FALSE)%>%
add_trace(y = teamAverageperweek$average,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgb(242,142,43)',
width = 3)) %>%
add_trace(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
type = 'scatter',
mode = 'markers',
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(
range = c(20,70),
title = "Avg Hours Per Week"
)
) %>%
layout(hoverlabel = list(bgcolor= 'white')) %>%
layout(showlegend = FALSE) %>%
add_annotations(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
text = teamAverageperweek$average,
xref = "x",
yref = "y",
yanchor = 'center',
xanchor = 'top',
showarrow = FALSE)
是否可以在点上方显示文字标签?
提前致谢!!
可以,您可以使用 textposition = "bottom center"
。
Here 您可以找到 textposition 的选项。
好吧,我对你的代码做了很多修改,但我认为那样更易读。希望能帮到你。
例如你可以使用:
teamAverageperweek %>% plot_ly(...)
代替 plot_ly(data = teamAverageperweek)
x = ~Quarter
代替 teamAverageperweek$Quarter
您只能使用一个轨迹并设置 mode = 'lines+markers+text'
而不是添加 2 个轨迹并使用 annotations()
您也可以设置layout(... , ...)
的所有参数,用逗号分隔。
这是您的代码,但已修改:
library(dplyr)
library(plotly)
# Yours data
teamAverageperweek = data.frame(
Quarter = c("2018 Q1","2018 Q2","2018 Q3","2018 Q4",
"2019 Q1","2019 Q2","2019 Q3"),
average = c(47.6,46.4,45.7,45.5,
45.7,46.3,45.7)
)
# The Plot
teamAverageperweek %>%
plot_ly(x = ~Quarter,
text = ~average,
hoverinfo = 'text',
hovertext = ~paste('Quarter of Date: ', Quarter,
'<br> Avg Hours Per Week: ',average),
showlegend = FALSE)%>%
add_trace(y = ~average,
type = 'scatter',
mode = 'lines+markers+text',
line = list(color = 'rgb(242,142,43)',
width = 3),
textposition = "bottom center", # here the text position
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(range = c(20,70),
title = "Avg Hours Per Week"
),
hoverlabel = list(bgcolor= 'white')
)
此处输出:
我正在尝试使用 ploy_ly()
函数可视化折线图。我需要在折线图上方显示标签。但是标签显示在点上,看起来很乱。
使用的dataframe如下:
Quarter average
1 2018 Q1 47.6
2 2018 Q2 46.4
3 2018 Q3 45.7
4 2018 Q4 45.5
5 2019 Q1 45.7
6 2019 Q2 46.3
7 2019 Q3 45.7
使用plot_ly()
可视化折线图的代码如下:
plot_ly(teamAverageperweek, x = teamAverageperweek$Quarter,
text = teamAverageperweek$average,
hoverinfo = 'text',
hovertext = paste('Quarter of Date: ', teamAverageperweek$Quarter,
'<br> Avg Hours Per Week: ',teamAverageperweek$average),
showlegend = FALSE)%>%
add_trace(y = teamAverageperweek$average,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgb(242,142,43)',
width = 3)) %>%
add_trace(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
type = 'scatter',
mode = 'markers',
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(
range = c(20,70),
title = "Avg Hours Per Week"
)
) %>%
layout(hoverlabel = list(bgcolor= 'white')) %>%
layout(showlegend = FALSE) %>%
add_annotations(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
text = teamAverageperweek$average,
xref = "x",
yref = "y",
yanchor = 'center',
xanchor = 'top',
showarrow = FALSE)
是否可以在点上方显示文字标签?
提前致谢!!
可以,您可以使用 textposition = "bottom center"
。
Here 您可以找到 textposition 的选项。
好吧,我对你的代码做了很多修改,但我认为那样更易读。希望能帮到你。
例如你可以使用:
teamAverageperweek %>% plot_ly(...)
代替plot_ly(data = teamAverageperweek)
x = ~Quarter
代替teamAverageperweek$Quarter
您只能使用一个轨迹并设置
mode = 'lines+markers+text'
而不是添加 2 个轨迹并使用annotations()
您也可以设置
layout(... , ...)
的所有参数,用逗号分隔。
这是您的代码,但已修改:
library(dplyr)
library(plotly)
# Yours data
teamAverageperweek = data.frame(
Quarter = c("2018 Q1","2018 Q2","2018 Q3","2018 Q4",
"2019 Q1","2019 Q2","2019 Q3"),
average = c(47.6,46.4,45.7,45.5,
45.7,46.3,45.7)
)
# The Plot
teamAverageperweek %>%
plot_ly(x = ~Quarter,
text = ~average,
hoverinfo = 'text',
hovertext = ~paste('Quarter of Date: ', Quarter,
'<br> Avg Hours Per Week: ',average),
showlegend = FALSE)%>%
add_trace(y = ~average,
type = 'scatter',
mode = 'lines+markers+text',
line = list(color = 'rgb(242,142,43)',
width = 3),
textposition = "bottom center", # here the text position
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(range = c(20,70),
title = "Avg Hours Per Week"
),
hoverlabel = list(bgcolor= 'white')
)
此处输出: