自定义直接制作的绘图更灵活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))
