我確實有類似的行為,
ggplotly:

代碼 :
library(fitdistrplus)
set.seed(123)
X <- rpois(20,3)
fit.p.mme <- fitdist(data = X, distr = "pois", method = "mme")
fit.p.mle <- fitdist(data = X, distr = "pois", method = "mle")
g1 <- cdfcomp(list(fit.p.mme, fit.p.mle), addlegend = TRUE, legendtext = c("P MME", "P MLE"), plotstyle = "ggplot")
g1 <- g1 theme_gray()
g1
g1 %>% ggplotly()
試圖在g1$data$ind沒有成功的情況下編輯標簽。
uj5u.com熱心網友回復:
您可以在 Plotly 構建中手動更改名稱。
為了找出我構建繪圖并運行lapply以輸出名稱和圖例組的名稱的來源。在情節中,離散的顏色分配將得到自己的蹤跡。因此,trace 的名稱通常是生成時圖例中的名稱。
我也不需要先打電話ggplotly()。
g2 <- plotly_build(g1)
invisible(
lapply(1:length(g2$x$data),
function(j) {
message(paste0(j, " ", g2$x$data[[j]]$name, " & ",
g2$x$data[[j]]$legendgroup))
})
)
# 1 &
# 2 &
# 3 &
# 4 &
# 5 (pois,1) & (pois,1)
# 6 (pois.1,1) & (pois.1,1)
所以有 6 條痕跡,這些是它們的名字(減去我粘貼在那里的 & 符號)。
您可以直接更改這些名稱。
g2$x$data[[5]]$name <- "P MME"
g2$x$data[[5]]$legendgroup <- "P MME"
g2$x$data[[6]]$name <- "P MLE"
g2$x$data[[6]]$legendgroup <- "P MLE"
如果您希望圖例以 y 軸為中心并帶有繪圖背景顏色,您也可以在此處執行此操作。
g2$x$layout$legend$y <- .5 # if you want to move the legend to the middle
g2$x$layout$legend$bgcolor <- g2$x$layout$plot_bgcolor # to use the same bg color
g2 # let's see it

肯定不一樣,但更接近。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/488017.html
上一篇:如何用標準偏差繪制平均線?
