如何在 plotly pie 中调整悬停标签内的长文本字符串?
How to adjust a long text string inside a hover label in plotly pie?
我想在我的饼图上的悬停标签内放置一个长文本字符串。我希望文本自动适应标签,以便正确显示。
此外,我想删除 Trace 0
示例数据集:
df.prescripciones.3.direccionadas.grafica = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS",
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR",
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP",
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO",
"Total"), `2021-06` = c(113844, 113844, 65234911, 113844,
113844, 113844, 113844, 113844, 113844), Total = c(113844,
113844, 65234911, 113844, 113844, 113844, 113844, 113844,
113844)), row.names = c(NA, -9L), class = c("tabyl", "tbl_df",
"tbl", "data.frame"), core = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS",
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR",
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP",
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO"
), `2021-06` = c(113844, 189728293, 65234911, 37038984, 5506387,
1198435, 1372392, 383629)), row.names = c(NA, -8L), class = "data.frame"), tabyl_type = "two_way", totals = c("col",
"row"))
创建饼图的部分服务器代码:
paired1 <- brewer.pal(n = 10, name = "Paired")
output$plot4 <- renderPlotly(
plot4 <- plot_ly( df.prescripciones.3.direccionadas.grafica ,
labels = ~Profesional,
values = ~Total,
type = 'pie',
marker = list(colors = paired1),
texttemplate = ~ paste ("<br>",
"%{percent} <br>"),
hovertemplate = ~ paste ( Profesional,
" <br>",IPS," <br>",
"%{percent} <br>",
"$","%{value} <br>" ) ) %>%
layout( title = list ( text = "Top 10 Profesionales"),
font = list (size = 11) ,
hoverlabel = list(font=list(size=11) ) ) %>%
layout(showlegend = F) %>%
layout(legend = list(x = -0.5, y = 0.5)) %>%
layout(autosize = T, margin = list(l = 25,
r = 65,
b = 40,
t = 60)) %>%
layout(plot_bgcolor = "rgba(0, 0, 0, 0)",
paper_bgcolor = "rgba(0, 0, 0, 0)"
)
)
生成的最终饼图:
似乎没有关于定义文本长度的论据。但是您可以在 hovertemplate 中拆分长文本,因此每 x 个字符中断一次。
可以在 post 中找到纯文本的示例 - 使用了 gsub
。在下面的代码中,将每 45 个字符添加一次中断。
要删除“Trace 0”,请在悬停模板末尾添加 <extra></extra>
。
代码
plot_ly( df.prescripciones.3.direccionadas.grafica ,
labels = ~IPS,
values = ~Total,
type = 'pie',
marker = list(colors = paired1),
texttemplate = ~ paste ("<br>",
"%{percent} <br>"),
# added gsub and <extra></extra>
hovertemplate = ~ paste (gsub('(.{1,45})(\s|$)', '\1\n', IPS),
"%{percent} <br>",
"$","%{value} <br> <extra></extra>" )) %>%
layout( title = list ( text = "Top 10 Profesionales"),
font = list (size = 11) ,
hoverlabel = list(font=list(size=11) ) ) %>%
layout(showlegend = F) %>%
layout(legend = list(x = -0.5, y = 0.5)) %>%
layout(autosize = T, margin = list(l = 25,
r = 65,
b = 40,
t = 60)) %>%
layout(plot_bgcolor = "rgba(0, 0, 0, 0)",
paper_bgcolor = "rgba(0, 0, 0, 0)"
)
Post
我想在我的饼图上的悬停标签内放置一个长文本字符串。我希望文本自动适应标签,以便正确显示。
此外,我想删除 Trace 0
示例数据集:
df.prescripciones.3.direccionadas.grafica = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS",
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR",
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP",
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO",
"Total"), `2021-06` = c(113844, 113844, 65234911, 113844,
113844, 113844, 113844, 113844, 113844), Total = c(113844,
113844, 65234911, 113844, 113844, 113844, 113844, 113844,
113844)), row.names = c(NA, -9L), class = c("tabyl", "tbl_df",
"tbl", "data.frame"), core = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS",
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR",
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP",
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO"
), `2021-06` = c(113844, 189728293, 65234911, 37038984, 5506387,
1198435, 1372392, 383629)), row.names = c(NA, -8L), class = "data.frame"), tabyl_type = "two_way", totals = c("col",
"row"))
创建饼图的部分服务器代码:
paired1 <- brewer.pal(n = 10, name = "Paired")
output$plot4 <- renderPlotly(
plot4 <- plot_ly( df.prescripciones.3.direccionadas.grafica ,
labels = ~Profesional,
values = ~Total,
type = 'pie',
marker = list(colors = paired1),
texttemplate = ~ paste ("<br>",
"%{percent} <br>"),
hovertemplate = ~ paste ( Profesional,
" <br>",IPS," <br>",
"%{percent} <br>",
"$","%{value} <br>" ) ) %>%
layout( title = list ( text = "Top 10 Profesionales"),
font = list (size = 11) ,
hoverlabel = list(font=list(size=11) ) ) %>%
layout(showlegend = F) %>%
layout(legend = list(x = -0.5, y = 0.5)) %>%
layout(autosize = T, margin = list(l = 25,
r = 65,
b = 40,
t = 60)) %>%
layout(plot_bgcolor = "rgba(0, 0, 0, 0)",
paper_bgcolor = "rgba(0, 0, 0, 0)"
)
)
生成的最终饼图:
似乎没有关于定义文本长度的论据。但是您可以在 hovertemplate 中拆分长文本,因此每 x 个字符中断一次。
可以在 post 中找到纯文本的示例 - 使用了 gsub
。在下面的代码中,将每 45 个字符添加一次中断。
要删除“Trace 0”,请在悬停模板末尾添加 <extra></extra>
。
代码
plot_ly( df.prescripciones.3.direccionadas.grafica ,
labels = ~IPS,
values = ~Total,
type = 'pie',
marker = list(colors = paired1),
texttemplate = ~ paste ("<br>",
"%{percent} <br>"),
# added gsub and <extra></extra>
hovertemplate = ~ paste (gsub('(.{1,45})(\s|$)', '\1\n', IPS),
"%{percent} <br>",
"$","%{value} <br> <extra></extra>" )) %>%
layout( title = list ( text = "Top 10 Profesionales"),
font = list (size = 11) ,
hoverlabel = list(font=list(size=11) ) ) %>%
layout(showlegend = F) %>%
layout(legend = list(x = -0.5, y = 0.5)) %>%
layout(autosize = T, margin = list(l = 25,
r = 65,
b = 40,
t = 60)) %>%
layout(plot_bgcolor = "rgba(0, 0, 0, 0)",
paper_bgcolor = "rgba(0, 0, 0, 0)"
)
Post