How to update to custom tool tip for ggbarplot when converting to ggplotly / plotly?
You you achieve your desired result via + aes(text = paste("Freq:", freq))
which adds your tooltip to the set of global aesthetics:
library(ggpubr)
library(plotly)
p <- ggbarplot(
data = data,
y = "# of Patients",
x = "concept name",
orientation = "horiz",
fill = "#D91E49",
color = "#D91E49",
ylab = "Cohort Population",
xlab = "",
width = .5
) +
aes(text = paste("Freq:", freq)) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(vjust = 1)) +
theme_bw() +
ggtitle("Distribution of Drug Treatments in US population") +
theme(plot.title = element_text(size = 10, face = "bold")) +
theme(plot.caption = element_text(size = 7, color = "red")) +
theme(legend.title = element_blank())
ggplotly(p)
Show tooltip on plotly based on x-axis
You can set the hovermode to "x unified":
## libraries
# library(tidyverse)
library(plotly)
## fake data
dat <- data.frame(date = seq(as.Date("1910/1/1"), as.Date("1910/1/10"), "days"),
pred = 1:10,
ci_low = seq(0, 9, 1),
ci_upper = seq(2, 11, 1))
## plot
p1 <- dat %>%
ggplot(aes(x = date, y = pred)) +
geom_line(color = "red", aes(group = 1, text = paste("date:", date, "\npred:", pred, "\nci_low:", ci_low, "\nci_upper:", ci_upper))) +
geom_ribbon(aes(x = date, ymin = ci_low, ymax = ci_upper), alpha = 0.2, linetype = 0)
## plotly-fy
ggplotly(p1, tooltip = c("text")) %>%
layout(hovermode = "x unified") %>%
style(hoverinfo = "skip", traces = 2)
Edit: To control the displayed hoverinfo we can use ggplotly
's tooltip
argument along with a custom "text" aesthetic. The hoverinfo of the geom_ribbon-trace can be hidden via style()
.
In the plotly book you can find some great examples regarding this.
Modify tooltip info of a plotly graph created via ggplotly
You can add some tooltip info with the text
aesthetic:
library(plotly)
gg <- ggplot(test) +
geom_point(aes(x = x, y = y, color = Var1,
text = paste0("Value: ", value, "</br>Max: ", max_value)),
size = 4, alpha = 0.5)
ggplotly(gg)
If you want only value
and max_value
:
gg <- ggplot(test) +
geom_point(aes(x = x, y = y, color = Var1,
text = paste0("Value: ", value, "</br></br>Max: ", max_value)),
size = 4, alpha = 0.5)
ggplotly(gg, tooltip = "text")
How to choose variable to display in tooltip when using ggplotly?
You don't need to modify the plotly
object as suggested by @royr2. Just add label = name
as third aesthetic
ggplot(data = d, aes(x = seq, y = value, label = name)) + geom_line() + geom_point()
and the tooltip will display name
in addition to seq
and value
.
The ggplotly
help file says about tooltip
parameter:
The default, "all", means show all the aesthetic mappings (including the unofficial "text" aesthetic).
So you can use the label
aesthetic as long as you don't want to use it for geom_text
.
BTW: I've also tried text
instead of label
ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line() + geom_point()
but then ggplot2
complained
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
and plotted only points. I had to add a dummy group to geom_line
to remove the issue:
ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line(group = 1) + geom_point()
(But note if you put the dummy group as fourth aesthetic inside aes()
it will appear by default also in the tooltip.)
However, I find the unofficial text
aesthetic can become useful alongside label
if you want to have different strings plotted by geom_text
and shown in the tooltip.
Edit to answer a question in comments:
The tooltip
parameter to ggplotly()
can be used to control the appearance. ggplotly(tooltip = NULL)
will suppress tooltips at all. ggplotly(tooltip = c("label"))
selects the aesthetics to include in the tooltip.
Show tooltip for only one layer in ggplot2 and plotly
You can suppress the tooltip on black dots using the style
function:
ggplotly(gg, tooltip=c("y")) %>%
layout(hovermode = "x unified") %>%
style(hoverinfo = "skip", traces = 1)
For more examples, see chapter Controlling Tooltips of the book Interactive web-based data visualization with R, plotly, and shiny.
Related Topics
Count Total Missing Values by Group
Fixing a Multiple Warning "Unknown Column"
How to Use Black-And-White Fill Patterns Instead of Color Coding on Calendar Heatmap
R Subtract Value for the Same Id (From the First Id That Shows)
What Are Examples of When Seq_Along Works, But Seq Produces Unintended Results
Counting Occurrence of Particular Letter in Vector of Words in R
X^(1/3)' Behaves Differently for Negative Scalar 'X' and Vector 'X' with Negative Values
Fit Many Formulae at Once, Faster Options Than Lapply
How to Specify Names of Columns for X and Y When Joining in Dplyr
Back-To-Back Barplot with Independent Axes R
Operator Precedence of "Unary Minus" (-) and Exponentiation (^) Outside VS. Inside Function
R - Unable to Install R Packages - Cannot Open the Connection
How to Get the Second Sub Element of Every Element in a List
Rmarkdown in Shiny Application