37

我正在尝试绘制一个没有数据以外信息的图。没有轴;没有网格;无题; 只是情节。

但是我不断获得无法删除的额外边距和填充。

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

生成此图像:阴谋

我想要的是这样的:情节理想

我不知道如何摆脱蓝色并使深灰色与边缘齐平。

有人可以提供一些建议吗?

4

4 回答 4

34

这是仅绘制面板区域的方法:

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

在此处输入图像描述

于 2013-01-14T11:00:22.533 回答
29

ggplot2_2.0.0可以使用theme_void

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

在此处输入图像描述

于 2016-03-01T22:14:15.097 回答
10

尝试

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

您可能需要为 0 刻度长度提交错误。

于 2013-01-14T07:53:16.757 回答
0

如果您只想删除 theme_bw() 中的网格,可以使用:

+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
于 2021-07-22T07:32:35.933 回答