I intend to have same width of bars across all 4 panels. Also, I do not want fixed width of all 4 panels as number of bars vary by panel. Following is my code and results. I'm using ggplotly inside flexdashboard.
ggplot2 results are fine/expected but once I pass ggplotly the results are not expected.
Edit: updated -> complete code using ggplotly inside flexdashboard
---
title: "Bar Plots"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Bar Plots
=====================================
Column
-----------------------------------------------------------------------
### First Bar Plot
```{r include=FALSE}
bor2 <- dput(structure(list(SUBJID = structure(c("4010-001", "1043-002", "1043-005",
"1036-002", "1043-007", "1043-004", "3103-001", "4006-001",
"7004-001"), label = "Subject"),
PCHG = structure(c(38.2978723404255, 105.714285714286, -79.4326241134752, -57.4468085106383, -55.5555555555556,
-17.741935483871, -10.5555555555556, 3.61904761904761, 31.8840579710145),
label = "Percent Change from Baseline"),
GROUP = c("Group 1","Group 2", "Group 3", "Group 3", "Group 3", "Group 3", "Group 3",
"Group 3", "Group 5")),
row.names = c(NA, -9L),
class = c("tbl_df", "tbl", "data.frame"), label = "MAP"))
```
```{r}
library (plotly)
library (ggplot2)
library (tidyverse)
library (haven)
bp <- ggplot(bor2,
aes(x = SUBJID, y = PCHG, fill = GROUP)) +
geom_bar(stat = "identity", position = "dodge") +
theme(panel.spacing = unit(.025, "cm"),
axis.text.x = element_text(size = 5, angle = -45)) +
facet_grid(~GROUP, scales = "free_x", space = "free_x", drop = TRUE) +
labs(x=NULL, y="Percent change from baseline (%)")
ggsave(filename="bp-ggplot2.pdf", width=9.25, height=6.25, units="in", device="pdf", dpi=300, paper="USr")
ggplotly(bp)
I used facet_grid with scales="free_x" and space="free_x" but I do not see the result (all 4 panels still have same widths and panels with Group 1,2,4 have a wider bar width compare to Group 3)
I tried looking at similar thread here but didn't help. https://stackoverflow.com/questions/38101512/the-same-width-of-the-bars-in-geom-barposition-dodge