gpt4 book ai didi

r - 在 R 中创建绘图图表以在计数和百分比之间切换

转载 作者:行者123 更新时间:2023-12-05 02:07:46 24 4
gpt4 key购买 nike

总体思路是创建一个可以在显示计数和百分比之间切换的绘图图表。我可以使用 updatemenu 更改显示的轨迹,并且轴刻度可以动态更改,但是当我在下面的示例中切换到“%”时,图例消失了。

我一点也不擅长绘图,这就是我使用 ggplotly() 的原因(我的真实示例更复杂,ggplolty() 效果很好! ) 我想知道是否必须手动将图例添加到 % traces (3, 4) 以在第一条迹线不可见时显示图例?

library(ggplot2)
library(plotly)

df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a",
"b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433,
0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125,
0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
row.names = c(NA, -10L))

p <- ggplot(df, aes(day, n, color = outcome)) +
geom_line() +
geom_line(aes(y = pct))

ggplotly(p, dynamicTicks = TRUE) %>%
style(visible = FALSE, traces = 3:4) %>%
layout(
updatemenus = list(
list(
buttons = list(
list(args = list("visible", list(TRUE, TRUE, FALSE, FALSE)),
label = "n"),

list(args = list("visible", list(FALSE, FALSE, TRUE, TRUE)),
label = "%")
)
)
)
)

enter image description here

enter image description here

注意:这也发布在 RStudio Community 上,但尚未收到任何答复。

最佳答案

@mfherman - 这是我想出的:

library(ggplot2)
library(plotly)
library(scales)

df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a",
"b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433,
0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125,
0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
row.names = c(NA, -10L))

p <- ggplot(df, aes(day, n, color = outcome)) +
geom_line() +
geom_line(aes(y = pct)) +
theme(legend.position="bottom", legend.box = "horizontal")



chart_type <- list(
type = "buttons",
direction = "right",
xanchor = 'center',
yanchor = "top",
x = 0.5,
y = 1.27,
buttons = list(
list(
label = "nvals",
method = "update",
args = list( list("visible", list(TRUE, TRUE, FALSE, FALSE)),
list( yaxis = list( range = c(0,200) ,
ticksuffix = "") )
)),
list(
label = "%vals",
method = "update",
args = list( list("visible", list(FALSE, FALSE, TRUE, TRUE)),
list( yaxis = list( range = c(0,100) ,
ticksuffix = "%") )
))
))

# https://plotly.com/r/custom-buttons/#relayout-button



p2 <- ggplotly(p, dynamicTicks = TRUE, width = 640, height = 420) %>%
style(visible = FALSE, traces = 3:4) %>%
layout(
legend = list(orientation = "h",y = 0.8, x = 0.8),
updatemenus = list( chart_type )
)


for (i in 1:ncol(df)){
p2$x$data[[i]]$showlegend <- TRUE
}
p2

重要部分在底部 -> 看起来 p2$x$data[[i]]$showlegend 值在第二张图表上默认设置为 false。可能值得在 github 项目上作为一个问题打开,以便将其添加为布局列表中的一个选项。看起来他们现在只有一个 hide_legend 选项……很奇怪。

https://github.com/ropensci/plotly/issues

感谢这个问题帮助我解决了这个问题:

https://github.com/ropensci/plotly/issues/842

enter image description here enter image description here

编辑:根据评论中的要求添加了 % yaxis

关于r - 在 R 中创建绘图图表以在计数和百分比之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com