【发布时间】:2023-04-02 03:35:02
【问题描述】:
我正在生成一个 SVG 分面图。我希望在将鼠标悬停在点上时出现工具提示。我在 ggplot/geom_point 中使用text 美学来制作工具提示文本。
产生警告...Warning: Ignoring unknown aesthetics: text。尽管有警告,它实际上会产生一个工具提示,正如我在下面的geom_point 的sprintf 中定义的那样。但是,一旦图表中的分面数量超过 21 个,工具提示只会针对点的子集出现。只是看起来不可靠。但是,删除 text 美学总是会导致在 任何 个方面的 所有 点的默认工具提示。当包含text 美学时,生成的工具提示是默认文本和我定义的定制文本的组合,看起来像这样...screenshot of tooltip... 不理想,但至少它包含我想要显示的信息,特别是将 Rate/'000 作为实际值而不是日志值。
我应该避免这个功能还是有更好、更可靠的方法来实现这个效果?
p <- ggplot(data=df.data_chart_category
,aes(x=TourDate, y=rate_per_thousand_lifts, group=key, colour=factor(key))
,environment = environment()
) +
geom_point(aes(size = size, text = sprintf("Rate/'000: %s<br>Misses: %s<br>Hits: %s<br>Total Lifts: %s", rate_per_thousand_lifts, misses, hits, total_tour_count)), alpha=0.4) +
scale_size(name = "Bin count", breaks = c(10, 50, 100, 500, 800, 1000), range = c(1,12)) +
scale_color_manual("", values = c("Lifter1" = "red", "Lifter2" = "green", "Lifter3" = "lightblue", "Lifter4" = "purple", "NoLifter" = "cyan", "GPS" = "darkgrey")) +
guides(colour = guide_legend(override.aes = list(size=10))) +
facet_wrap(as.formula(paste("~", facet_wrap_column_name)), ncol=number_of_facet_columns) +
labs(x = x_axis_label, y = snr_y_axis_label, title = snr_chart_title, subtitle = snr_chart_subtitle, caption = snr_chart_caption, color="Lifter") +
theme_few() +
theme(axis.text.x = element_text(angle=90, vjust=-0.01)) +
theme(axis.title = element_text(size = axis_font_size)) +
theme(plot.title = element_text(color = '#666666',
face = 'bold',
size = title_font_size,
hjust = 0)) +
scale_y_log10()
pp <- ggplotly(p)
htmlwidgets::saveWidget(widget=pp, file="index.html", selfcontained=FALSE)
【问题讨论】:
-
我查看了绘图对象,很明显正确的文本被分配给绘图对象元素。迭代对象在这里并没有帮助,而且在任何情况下,考虑到方面的存在,这在我看来有点棘手。我已经在https://github.com/liamcaffrey/svg_tooltip.git 放置了一个完整的示例,其中包含 dput 数据。我已经包含了示例 png 和 html/svg 文件。工具提示似乎总是适用于零值,有时也适用于非零值