40

I am trying to make a plot with no information beyond the data. No axes; no grid; no title; just the plot.

But I keep getting extra margins and padding that I can't remove.

library(ggplot2)
library(grid)

theme_bare <- theme(
  axis.line = element_blank(), 
  axis.text.x = element_blank(), 
  axis.text.y = element_blank(),
  axis.ticks = element_blank(), 
  axis.title.x = element_blank(), 
  axis.title.y = element_blank(), 
  #axis.ticks.length = unit(0, "lines"), # Error 
  axis.ticks.margin = unit(c(0,0,0,0), "lines"), 
  legend.position = "none", 
  panel.background = element_rect(fill = "gray"), 
  panel.border = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  panel.margin = unit(c(0,0,0,0), "lines"), 
  plot.background = element_rect(fill = "blue"),
  plot.margin = unit(c(0,0,0,0), "lines")
)

ggplot() + 
  geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
  theme_bare

Produces this image: plot

What I want is this: plot ideal

I can't figure out how to get rid of the blue and make the dark gray flush with the edges.

Could any one offer some advice?

sharoz
  • 6,157
  • 7
  • 31
  • 57
  • For the second part, you'll want to add `scale_*_*(expand = c(0,0))` for each axis. – joran Jan 14 '13 at 04:28
  • @joran: SO I tried adding `+ scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0))`, but it says `Discrete value supplied to continuous scale` whether I use continuous or discrete. – sharoz Jan 14 '13 at 04:56
  • 1
    As a heads up, setting Date to numeric and applying the two `scale_*_*(expand = c(0,0))` functions removes the grey on the outside of the graph, but does nothing for the blue. – sebastian-c Jan 14 '13 at 05:24

4 Answers4

36

Here is the way to plot only the panel region:

p <- ggplot() + geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
  scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
  theme(line = element_blank(),
        text = element_blank(),
        title = element_blank())

gt <- ggplot_gtable(ggplot_build(p))
ge <- subset(gt$layout, name == "panel")

grid.draw(gt[ge$t:ge$b, ge$l:ge$r])

enter image description here

Community
  • 1
  • 1
kohske
  • 65,572
  • 8
  • 165
  • 155
32

From ggplot2_2.0.0 you can use theme_void:

ggplot() + 
  geom_area(data = economics, aes(x = date, y = unemploy), linetype = 0) +
  theme_void()

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • 1
    there's still some space around the plot – baptiste Mar 01 '16 at 22:22
  • @baptiste Thanks for pointing this out. I was focusing on the "_No axes; no grid; no title;_" part of the question. – Henrik Mar 01 '16 at 22:24
  • A minor note, this approach doesn't seem to work with `ggtext::element_markdown()` (it causes the html not to render, so instead you get raw html as text overlaid on the image) – stevec May 11 '20 at 08:27
10

try

last_plot() + theme(axis.ticks.length = unit(0.001, "mm")) + labs(x=NULL, y=NULL)

you may want to file a bug for the 0 tick length.

baptiste
  • 75,767
  • 19
  • 198
  • 294
-1

If you just want to remove the grid in theme_bw(), you can use:

+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Minstein
  • 542
  • 5
  • 10