![](/img/trans.png)
[英]First and last facets using facet_wrap with ggplotly are larger than middle facets
[英]When using ggplotly with facet_wrap, only the data of the first facet appears
我正在使用ggplot() + geom_boxplot() + facet_wrap()
为多个组绘制箱线图。 我想在这个 object 上使用ggplotly()
来使用 hover 工具提示,以便我可以快速查看与异常值相关的数据。 (为什么我不只是在 plot 上进行注释?我将在工具提示中添加自定义文本,该文本比我希望出现在 plot 上的固定注释中更广泛。)
当我使用ggplotly
时,会显示构面,但只显示第一个构面的数据。 facet_grid
也是如此。 (我更喜欢使用facet_wrap
以便我可以在不同方面之间使用自由比例。)
有没有办法让ggplotly
以这种方式处理方面? 我在ggplotly 文档中看不到任何内容。 我见过其他例子,人们在他们的方面有其他问题,但所有方面都在那里。
# 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
# ggplotly object only shows first facet
ggplotly(pwrap)
# 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 info: R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19041) ggExtra_0.9
plotly_4.9.3 ggplot2_3.3.3
它实际上来了,但你看不到它,如果你zoom out
你填充找到所有的情节,你可以修改你的代码,如下所示,使其工作,在这里我已经将宽度更改为平均列的mean(df$C)
. 在旁注中,您可以通过在facet_wrap(facets = 'A', scales='free')
命令中使用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 居中(图未附加),请告诉我是否帮助:
# 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)
Output 从第一个版本开始:
Pkumar 的解决方案很接近(定义 x 变量),但 x 轴的行为仍然不像我预期的那样。 解决方案是确保 x 变量是一个因素。
# plot using facet_wrap
pwrap <- ggplot(df, aes(x = as.factor(A), y = C)) + # x is a factor (as.factor('X') also OK if trying to match PKumar's answer)
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales='free')
# ggplot object
pwrap
# now a functional
ggplotly(pwrap)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.