我的目标是创建一个包含标签和悬停效果的交互式多边形。这是一个例子:
library(dplyr)
library(ggplot2)
library(ggiraph)
df <- data_frame(
x = c(0, 0, 3, 5, 3, 6, 0),
y = c(0, 4, 6, 5, 3, 0, 0),
tooltip = replicate(7, "Hello")
)
text_df <- data_frame(
x = 2,
y = 3,
label = "TEST",
tooltip = "Hello"
)
gg <- ggplot() +
geom_polygon_interactive(
data = df,
aes(
x = x,
y = y,
tooltip = tooltip,
data_id = tooltip
)
) +
geom_text_interactive(
data = text_df,
aes(
x = x,
y = y,
label = label,
tooltip = tooltip,
),
color = "white",
size = 5
)
ggiraph(
ggobj = gg,
hover_css = "fill:red;",
)
当我将鼠标悬停在多边形内的任何地方(标签除外)上时,它的颜色会发生变化并且文本仍然可见。所以,一切都很好。我也想对标签有完全相同的效果。我希望多边形改变颜色并且文本在将鼠标悬停在标签上时保持不变。下面的尝试填充了标签,使其难以辨认。
# using the same data frame from the above-given example
gg2 <- ggplot() +
geom_polygon_interactive(
data = df,
aes(
x = x,
y = y,
tooltip = tooltip,
data_id = tooltip
)
) +
geom_text_interactive(
data = df,
aes(
x = 2,
y = 3,
label = "TEST",
tooltip = tooltip,
data_id = tooltip
),
color = "white",
size = 5
)
ggiraph(
ggobj = gg2,
hover_css = "fill:red;",
)
是否可以调整每个 ggplot 组件的悬停效果?