[英]Positioning legend title on top of horizontal legend causes loss of interactivity in ggiraph
When I used guides() specifications to customise legend appearance, I found that the interactive nature of the legend in the plot is lost.当我使用 guides() 规范自定义图例外观时,我发现图例中图例的交互性丢失了。
The following code modified from the example in the ggiraph documentation.以下代码修改自 ggiraph 文档中的示例。
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p1 <- p + scale_fill_manual_interactive(
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { paste0("alert(\"", as.character(breaks), "\")") },
labels = function(breaks) {
lapply(breaks, function(br) {
label_interactive(
as.character(br),
data_id = as.character(br),
onclick = paste0("alert(\"", as.character(br), "\")"),
tooltip = as.character(br)
)
})
}
) +
guides(fill=guide_legend(title.position = "top")) +
theme(legend.position="top")
girafe_options(girafe(ggobj = p1),
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
Without the guides(fill=guide_legend(title.position = "top"))
line, the legend retains its interactivity.如果没有
guides(fill=guide_legend(title.position = "top"))
行,图例将保留其交互性。
Any help is highly appreciated!任何帮助都非常感谢!
Instead of changing the title position via guides()
you could achieve your desired result without losing the interactivity via the guide
argument of scale_fill_manual_interactive
:不是通过
guides()
更改标题位置,您可以通过scale_fill_manual_interactive
的guide
参数在不丢失交互性的情况下实现您想要的结果:
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c("Guy", "Ginette", "David", "Cedric", "Frederic"),
gender = c("Male", "Female", "Male", "Male", "Male"),
height = c(169, 160, 171, 172, 171)
)
p <- ggplot(dat, aes(
x = name, y = height, fill = gender,
data_id = name
)) +
geom_bar_interactive(stat = "identity")
p1 <- p + scale_fill_manual_interactive(
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) {
as.character(breaks)
},
tooltip = function(breaks) {
as.character(breaks)
},
onclick = function(breaks) {
paste0("alert(\"", as.character(breaks), "\")")
},
labels = function(breaks) {
lapply(breaks, function(br) {
label_interactive(
as.character(br),
data_id = as.character(br),
onclick = paste0("alert(\"", as.character(br), "\")"),
tooltip = as.character(br)
)
})
},
guide = guide_legend(title.position = "top")
) +
theme(legend.position = "top")
girafe_options(
girafe(ggobj = p1),
opts_hover_key(girafe_css("stroke:red", text = "stroke:none;fill:red"))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.