Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Modify aesthetic mapping in ggplot2 to obtain correct plotly legend

$
0
0

I have a ggplot2 chart that I want to render with plotly. The x and y variable are obtained from a character variable as in the example below:

library(ggplot2)
library(plotly)
dset <- data.frame(x =  runif(10), y = runif(10))
head(dset)
#>            x         y
#> 1 0.45902147 0.9842178
#> 2 0.04331170 0.8337590
#> 3 0.02202882 0.8607866
#> 4 0.27971306 0.4250171
#> 5 0.35531015 0.7182533
#> 6 0.61235609 0.9905286

# vars for x and y aesthetics
varx <- "x"
vary <- "y"

# ggplot2 chart
p <-
  ggplot(dset, aes(get(varx), get(vary))) + xlab(varx) + ylab(vary) + geom_point()

# convert to plotly
ggplotly(p)

The plotly output displays get(varx) in the tooltip and I would like it to display x. I believe the text is inherited from the aesthetic mapping.

print(p[[4]])
#> Aesthetic mapping: 
#> * `x` -> `get(varx)`
#> * `y` -> `get(vary)`

Created on 2020-01-20 by the reprex package (v0.3.0)

Is there a way to change the aesthetic mapping before converting the chart to plotly? If not, what is the easiest way to modify the tooltip in plotly?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>