自定义直接在plotly中制作的绘图更加灵活,但是使用ggplotly也可以进行请求的操作。下面是一个关于 iris 数据集的例子:
library(plotly)
library(ggplot)
定义悬停信息:
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
hoverinfo = 'text',
text = ~Species)
在调用 ggplot 时,使用 ggplotly 将文本参数留空:
z <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
geom_point()
并将参数tooltip 设置为ggplotly:
ggplotly(z, tooltip="Species")
相比:
z <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
geom_point(aes(text = Species))
ggplotly(z)
编辑:自定义文本:
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
hoverinfo = 'text',
text = ~paste(Species,
'</br></br>', Petal.Length))