简体   繁体   中英

ggplot2 theme with no axes or grid

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:阴谋

What I want is this:情节理想

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?

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])

在此处输入图片说明

From ggplot2_2.0.0 you can use theme_void :

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

在此处输入图片说明

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.

如果你只是想在 theme_bw() 中删除网格,你可以使用:

+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM