I would like to generate a hovertext for ggplotly()
using aes()
along the lines of
mapping <- aes(text = paste0(
"Value: ",
columnName
))
where columnName
is a variable containing a column name as a string. Now, I don't want columnName
recognized as a column name itself. If columnName == "col1"
the result I want is
aes(text = paste0(
"Value: ",
col1
))
which evaluates to the aesthetic mapping `text` -> `paste0("Value: ", col1)`
.
I have tried using aes_string()
, but
columnName <- "col1"
aes_string(text = paste0(
"Value: ",
columnName
))
just evaluates to the aesthetic mapping `x` -> `Value:col1`
.
How do I make this work?
Apparently OP wants to create an expression object in order to evaluate it later on. That can be achieved using bquote
(or substitute
or some function from the tidyverse wich probably involves exclamation marks):
columnName <- "col1"
columnName <- as.name(columnName) #first turn the character string into a quoted name
mapping <- bquote(aes(text = paste0(
"Value: ",
.(columnName) #this substitutes the name into the expression
))
)
#aes(text = paste0("Value: ", col1))