I'm doing a Shiny app, but in the spirit of making a minimal reproduceable example, I took the Shiny part out. This is just context.
I want to make a plot with the ggplotly()
function. It works fine until I try to change the tooltip.
Without that, my code would look like this:
library(ggplot2)
library(plotly)
n <- 100
samples <- n
act.rec <- n
while(n != 0) {
rand <- ceiling(runif(n, 0, 2))
act <- length(rand[rand == 2])
act.rec <- c(act.rec, act)
n <- n-act
samples <- c(samples, n)
}
test <- data.frame(act = c(act.rec, 0), samples = c(samples, 0), hl = 0:length(samples))
test2 <- test
test2$samples <- 100-test$samples
test <- rbind(cbind(test, dec = "Not decayed"), cbind(test2, dec = "Decayed"))
p <- ggplot(test, aes(x = hl, y = samples, colour = dec))+
stat_smooth(method = "nls", size = 0.8,
formula = y ~ SSasymp(x, Asym, R0, lrc), se = FALSE)+
geom_point(shape = 4, size = 3)+
theme_minimal()+
scale_colour_discrete(name = "Decay Status")+
xlab("Half Lives")+
ylab("Atoms")
ggplotly(p)
This is a simulation of the half-lives of radioactive elements (again, context).
After that, I tried adding the text
argument in the geom_point()
geom. That works fine; if I hover over the crosses, it shows me what I want:
[...]
geom_point(shape = 4, size = 3, aes(text = paste(
"Half-lives: ", hl,
"\nAtoms: ", samples,
"\nDecay Status: ", dec
) ))+
[...]
ggplotly(p, tooltip = "text")
Just a snapshot:
However, when I try to do the same with
stat_smooth
, it disappears.
[...]
stat_smooth(method = "nls", size = 0.8,
formula = y ~ SSasymp(x, Asym, R0, lrc), se = FALSE,
aes(text = paste(
"Half-lives: ", hl,
"\nAtoms: ", samples,
"\nDecay Status: ", dec
) ))+
[...]
ggplotly(p, tooltip = "text")
By the way, it I just do ggplotly(p)
, it has the same effect, only that the tooltip that does exist shows everything twice, once the way I want, once the way it was before. But this is not the point.
I've read multiple posts, like this one, from which I got the idea to use label
instead of text
and this one, which hasn't been answered yet.
Using label
did me nothing – literally. The line didn't disappear, but the tooltip didn't work.
I've got to say, I don't know what to do next. I really hope someone can find out what's wrong, or maybe geom_smooth
(or stat_smooth
) is a special case. Either way, thanks in advance.
PS (edit 2): I also discovered that, if I don't put any variables in text
, it works fine:
[...]
stat_smooth(method = "nls", size = 0.8,
formula = y ~ SSasymp(x, Asym, R0, lrc), se = FALSE,
aes(text = paste(
"Half-lives: "
) ))+
[...]
ggplotly(p)
EDIT: My current code is:
library(ggplot2)
library(plotly)
n <- 100
samples <- n
act.rec <- n
while(n != 0) {
rand <- ceiling(runif(n, 0, 2))
act <- length(rand[rand == 2])
act.rec <- c(act.rec, act)
n <- n-act
samples <- c(samples, n)
}
test <- data.frame(act = c(act.rec, 0), samples = c(samples, 0), hl = 0:length(samples))
test2 <- test
test2$samples <- 100-test$samples
test <- rbind(cbind(test, dec = "Not decayed"), cbind(test2, dec = "Decayed"))
p <- ggplot(test, aes(x = hl, y = samples, colour = dec))+
stat_smooth(method = "nls", size = 0.8,
formula = y ~ SSasymp(x, Asym, R0, lrc), se = FALSE,
aes(label = paste(
"Half-lives: ", hl,
"\nAtoms: ", samples,
"\nDecay Status: ", dec
) ))+
geom_point(shape = 4, size = 3, aes(text = paste(
"Half-lives: ", hl,
"\nAtoms: ", samples,
"\nDecay Status: ", dec
) ))+
theme_minimal()+
scale_colour_discrete(name = "Decay Status")+
xlab("Half Lives")+
ylab("Atoms")
ggplotly(p)