0

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)

Result of the code

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

abhijeet
  • 21
  • 5
  • 4
    It will be easier to help if you can make your code reproducible. Can you run `dput(bor2)` and include the output code in your question? This will allow us to work with the `bor2` data in the form you have it. – Jon Spring Jan 20 '23 at 06:10
  • added data in bor2 – abhijeet Jan 20 '23 at 16:43
  • When I run your code, all the bars have the same width. Do you want them to be the same or should the vary defined? – Quinten Jan 20 '23 at 17:57
  • Using ggplot2 3.3.6, I also get bars that are the same width. – Jon Spring Jan 20 '23 at 18:11
  • @Qui 1) I want same width for all bars but panel width to vary. But as seen in the output I shared, I get different bar widths (bar width in Group 1,2,4 is wider than bar width in Group 3). 2) Also, I get all 4 panels with same widths but I need different panel widths (based on number of bars in that panel). I'm using ggplot2 3.4.0. – abhijeet Jan 20 '23 at 19:26
  • Sorry, I added complete code (can be run). The results are ok/expected when simply using ggplot2 code but once I pass those results to ggplotly (inside flexdashboard) the results are not expected. – abhijeet Jan 20 '23 at 22:21

0 Answers0