narrow plot area in ggplot

Hi all! Thanks in advance. I'm working on a bar chart with a single bar which is too wide for my visual liking, but I've found that narrowing the width of the bar only reduces the width of the bar and not the panel around it--which leads to the title of the plot floating up in the air above it (my final chart will not have a border or background, which would visually cue where the panel ends and the plot title begins). Any suggestions for how to fix this?

library(tidyverse)

df <- tibble(
    brand = c("Facebook", "Google", "Twitter"),
    market_share = c(.4,.5,.1)
)

ggplot(df) + 
    aes(x = "Total", y = market_share, fill = brand) + 
    geom_bar(stat = "identity") + 
    labs(title = "My Title!") + 
    coord_flip() + 
    theme_bw()

Created on 2019-10-03 by the reprex package (v0.3.0)

The size of the plot is dependent on the graphic device so depending on what the format of your final output is going to be you could for example change the size while saving the file

ggsave(g, height = ..., width = ...)

or define it on the code chunk if you are using an .Rmd file

```{r, fig.height=..., fig.width=...}
# Ploting code
```

For example:

---
title: "R Notebook"
output: html_notebook
---

```{r, fig.height=2, fig.width=10}
library(tidyverse)

df <- tibble(
    brand = c("Facebook", "Google", "Twitter"),
    market_share = c(.4,.5,.1)
)

ggplot(df) + 
    aes(x = "Total", y = market_share, fill = brand) + 
    geom_col(width = 0.5) +
    labs(title = "My Title!") + 
    coord_flip() + 
    theme_void() +
    theme(plot.margin = margin(30, 10, 0, 10))
```

2 Likes

Awesome! Any ideas for how this might work in a shiny app? I'm guessing I'd have to look into figure sizing in app....

shiny::renderPlot() takes width and height arguments.

renderPlot({}, width = ..., height = ...)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.