ryamy
May 16, 2024, 3:27am
1
Following code produce expected bar and facet_strip width in ggplot2 figure. But after it is converted to plotly object, bar width and strip width collapsed. How can I adjust it?
(expected)
(collapsed in plotly... )
## sample data
df <- tibble(Group=c('A', 'A', 'A', 'B', 'C'),
Sample=sprintf('S%s', seq(1, 5)),
Value=seq(1, 5),
N=c(1, 2, 3, 1, 1))
## ggplot object show expected plots
## (same bar width, differect strip width)
df %>%
ggplot(aes(x=Sample, y=Value)) +
geom_col(position='dodge') +
facet_row(~Group, scales='free_x', space='free') -> g
## ploly dows not show as expected
## (different bar width, same strip width)
htmlwidgets::saveWidget(ggplotly(g), './example.html')
Im find this result so close that you want:
# using you df data
library(ggplot2)
library(plotly)
library(dplyr)
g <- df %>%
ggplot(aes(x=Sample, y=Value)) +
geom_col(position='dodge') +
facet_wrap(~Group, scales='free_x', ncol = 3)
plotly_obj <- ggplotly(g)
plotly_obj <- plotly_obj %>%
style(width = 0.4) # adjust the width as needed
plotly_obj <- plotly_obj %>%
layout(
margin = list(l = 40, r = 40, b = 50, t = 20, pad = 4)) # Adjust margins as necessary
plotly_obj
ryamy
May 16, 2024, 7:22am
3
Thanks @M_AcostaCH !
I am so sorry but I am realized that I uploaded inappropriate examples. I will create new issue. Thanks!
system
Closed
May 23, 2024, 7:23am
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.