Reputation: 172
I am generating an SVG facet chart. I want a tooltip to appear on hover over the points. I am using a text
aesthetic in ggplot/geom_point to craft the tooltip text.
A warning is produced... Warning: Ignoring unknown aesthetics: text
. In spite of the warning, it does in fact produce a tooltip as I have defined it below in the sprintf
of geom_point
. However, once the number of facets in the chart exceed 21, the tooltip only appears for a subset of the points. It just seems unreliable. However, removing the text
aesthetic always results in a default tooltip for all points across any number of facets. When including the text
aesthetic the tooltip generated is a combination of default text and the bespoke text that I have defined and looks like this...screenshot of tooltip... not ideal but at least it contains the information I want to show, in particular the Rate/'000 as an actual value rather than as a log value.
Should I avoid this functionality or is there a better, more reliable, way to achieve this effect?
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)
Upvotes: 3
Views: 8882
Reputation: 204
I realise this is old but I found a solution that worked for me.
I kept running into this issue when trying to update the tooltips = "text"
parameter in ggplotly()
and becoming frustrated when for some reason it would not update using the mapping from ggplot.
One weird thing I found is that for some reason when stat = "sum"
it won't update the text in the tooltip and returns the error Ignoring unknown aesthetics: text
when generating the plot. However, both stat = "count"
and stat = "identity"
work just fine.
Upvotes: 1
Reputation: 12087
I don't think the solutions here are correct as this is obviously a bug as the text attribute is perfectly valid in geom_point
or geom_vline
(which I was using) from the point of view of ggplotly so my solution to this would be:
gg <- gg + suppressWarnings(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))
Upvotes: 5
Reputation: 172
I got my example working somewhat as I expected. I wasn't really understanding how to set up bespoke tooltips. I've posted a working example here. Data for the example can still be found at TooltipNotWorkingData.txt
The main issue related to my tooltip parameter placement.
p <- ggplot(data=df.data_chart_category
,aes(x=calendar_day, y=rate_per_thousand, group=key, colour=factor(key), text = tooltip, onclick = clickjs)
,environment = environment()
)
It should be part of ggplot/aes and not geom_point/aes. Referencing tooltip in the ggplotly call pp <- ggplotly(p, tooltip = "tooltip")
is necessary to override the default tooltip.
I'm still having problems with tooltip display on certain size charts and I've opened a new question for that on SO here.
Upvotes: 2
Reputation: 13680
Problems with plotly
's tooltip are usually fixed building the plot object and the fiddling with text
inside it:
for (i in 1:nrow(df.data_chart_category)){
pp$x$data[[i]]$text <- c(pp$x$data[[i]]$text, "")
}
It's kinda hard to check if it works in this case as we don't have the original dataframe or a reproducible example.
Upvotes: 1