mindlessgreen
mindlessgreen

Reputation: 12142

R ggplot: Remove all space around plot

How do I remove all space around the plot area?

Sample data

dfr <- data.frame(x=factor(1:5),y=c(3,5,4,2,4))

ggplot(dfr,aes(x,y))+
  geom_bar(stat="identity")

I tried as below, but there is still space on left and bottom. Bg border colour added to visualise edge.

ggplot(dfr,aes(x,y))+
  geom_bar(stat="identity")+
  labs(x="",y="")+
  theme(axis.ticks=element_blank(),
        axis.text=element_blank(),
        plot.background=element_rect(colour="steelblue"),
        plot.margin=grid::unit(c(0,0,0,0),"cm"))

enter image description here

Upvotes: 3

Views: 4459

Answers (2)

dash2
dash2

Reputation: 2262

As the comment above says, if there's still some frustrating whitespace left, add

theme(axis.ticks.length = unit(0, "pt"))

to your plot. This is true even if you've set axis.ticks = element_blank().

Upvotes: 1

Andrew Gustar
Andrew Gustar

Reputation: 18435

You need to add axis.title=element_blank() to your theme statement.

enter image description here

Upvotes: 7

Related Questions