4
我正在一個鏈接的陰謀(類似於卡爾森Sievert的The SharedData plot pipeline在情節爲R.該圖顯示了我目前懸停的一個項目的傳說。但是,現在圖例顯示兩個元素,一個用於條形圖和一個用於折線圖。過濾圖例鏈接的意見與情節
- 如何刪除第一個傳說元素(紅色正方形的柱狀圖),只保留了折線圖的圖例(紅線) ?
這裏是當前代碼:
library(ggplot2)
library(crosstalk)
library(plotly)
sd <- SharedData$new(txhousing, ~city)
base <- plot_ly(sd, color = I("black")) %>%
group_by(city) %>%
layout(showlegend = TRUE)
p1 <- base %>%
summarise(has = sum(is.na(median))) %>%
filter(has > 0) %>%
arrange(has) %>%
add_bars(x = ~has, y = ~factor(city, levels = city),
hoverinfo = "none", showlegend = FALSE) %>%
layout(
barmode = "overlay",
xaxis = list(title = "Number of months missing"),
yaxis = list(title = "")
)
p2 <- base %>%
add_lines(x = ~date, y = ~median, alpha = 0.3, showlegend = FALSE) %>%
layout(xaxis = list(title = ""))
gp <- subplot(p1, p2, titleX = TRUE, widths = c(0.3, 0.7)) %>%
layout(margin = list(l = 120)) %>%
highlight(color = "red",
defaultValues = "Victoria",
selected = attrs_selected(showlegend = TRUE, mode = "lines"
))
gp
有人在某處說過,傳說元素的最終刪除可能有效,但它在這裏並不適合我。
gp$x$data[[1]]$showlegend <- FALSE
據我知道你不能這樣做,直接在Plotly。但是,您可以將JavaScript代碼添加到您的情節,以照顧您的需要。 –