1

The following code produces the attached plot. How to get rid of/ remove the regions marked in RED?

    df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
    ggplot(df, aes(trt, outcome)) +
    geom_col()

enter image description here

I modified the code as follows but no change.

df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
   
ggplot(df, aes(trt, outcome)) +
   geom_col() + 
   theme(
    plot.margin = unit(c(0,0,0,0), "mm")
  )
morgan121
  • 2,213
  • 1
  • 15
  • 33
Mainul Islam
  • 1,196
  • 3
  • 15
  • 21

2 Answers2

4

You can set expand to 0 on both the axis :

library(ggplot2)
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))

ggplot(df, aes(trt, outcome)) +
  geom_col() + 
  scale_x_discrete(expand = c(0,0)) + 
  scale_y_continuous(expand = c(0,0))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

the margin option is for outside the plotting window. To change within the plotting window try this:

ggplot(df, aes(trt, outcome)) +
  geom_col() + 
  coord_cartesian(expand = F)
morgan121
  • 2,213
  • 1
  • 15
  • 33