gpt4 book ai didi

r - 使用ggplotly和facet_wrap时,只出现第一个facet的数据

转载 作者:行者123 更新时间:2023-12-04 08:01:04 26 4
gpt4 key购买 nike

我正在使用 ggplot() + geom_boxplot() + facet_wrap() 为多个组绘制箱线图.我想用 ggplotly()在此对象上使用悬停工具提示,以便我可以快速查看与异常值相关的数据。 (为什么我不只是在绘图上进行注释?我将在工具提示中添加自定义文本,该文本比我希望出现在绘图上的固定注释中更广泛。)
当我使用 ggplotly ,将显示构面,但仅显示第一个构面的数据。 facet_grid也是如此. (我更喜欢使用 facet_wrap 以便我可以在不同方面之间使用自由尺度。)
有没有办法获取ggplotly以这种方式处理方面?我在 ggplotly documentation 中没有看到任何内容.我见过其他例子,人们有 other issues with their facets但所有方面都在那里。

# make data: 
# B: normal distribution divided across 3 groups
# C: group Z is on a different scale
df <- data.frame(A = rep(LETTERS[seq( from = 24, to = 26 )], 26*5),
B = rnorm(26*15))
df <- df %>% mutate(C = ifelse(A == 'Z', B*7,
ifelse(A == 'Y', B + 7, B)))

# plot using facet_wrap
pwrap <- ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(facets = 'A')
# ggplot object shows fine
pwrap
enter image description here
# ggplotly object only shows first facet
ggplotly(pwrap)
enter image description here
# plot using facet_grid
pgrid <- ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_grid(.~A)
# again, ggplot object shows fine
pgrid
# but ggplotly object is limited to first facet
ggplotly(pgrid)

# my goal: facet_wrap to allow 'free' y-scales for variable C, with ggplotly hover info
ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales = 'free')

session 信息:
R 版本 4.0.3 (2020-10-10)
平台:x86_64-w64-mingw32/x64(64位)
在以下环境下运行:Windows 10 x64(内部版本 19041)
ggExtra_0.9
plotly_4.9.3
ggplot2_3.3.3

最佳答案

它真的来了,但你看不到它,如果你zoom out你填写找到所有的图,你可以像下面这样修改你的代码以使其工作,这里我已经将列的宽度更改为mean(df$C) .
附带说明一下,您可以使用 scales='free' 使它变得更好。在 facet_wrap(facets = 'A', scales='free')命令,但在这种情况下,您有三个垂直轴。

pwrap <- ggplot(df, aes(y = C)) +
geom_boxplot(width=mean(abs(df$C))) +
theme_classic() +
facet_wrap(facets = 'A')
# ggplot object shows fine
pwrap
ggplotly(pwrap)
另一个版本(缩放版本),使用 x = 1 和 scales='free' 并且不使用宽度选项,两个图的不同之处在于第二个图居中(未附加图),请告诉我帮助:
# plot using facet_wrap
pwrap <- ggplot(df, aes(x = 'X', y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales='free')
# ggplot object shows fine
pwrap
ggplotly(pwrap)
第一个版本的输出 :
enter image description here

关于r - 使用ggplotly和facet_wrap时,只出现第一个facet的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66466736/

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