删除ggplot2中的绘图边距 [英] Remove Plot Margins in ggplot2

查看:143
本文介绍了删除ggplot2中的绘图边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在R中使用ggplot2和ggsave(与Cairo)生成png图表。我在定制主题以删除页边空间时遇到问题。



  ... + theme(plot.margin = unit(c(0,0, 0,0),mm))

这似乎适用于四边中的两边我的情节,它完全消除了顶部和右侧边缘,但左侧和底部仍然有相当大的余量。有没有办法彻底删除这些?下面的图片来说明问题:




如果一个可重复使用的例子很有用,那么让我知道,我会把它们放在一起。






编辑:

  library(ggplot2 )
library(scales)
library(Cairo)
library(grid)

#设置图表值
line.width = 0.45
axis.font.size = 2.9

#生成一些随机数据
start.date< - as.Date(2011-07-01)
x.month< -seq.Date(start.date,by =month,length.out = 24)
end.date< - max(x.month)

period.a < - rnorm(12,mean = 50,sd = 2)
period.b < - rnorm(12,mean = 55,sd = 2)

x.value <-c(period.a,period.b)

#合并为数据框
x.data< - data.frame(
Month= x.month,
Value= x.value


#构建图表
p < - ggplot(data = x。数据aes(Month,Value))+ geom_line(size = line.width)
p <-p + theme_bw()
p <-p + scale_y_continuous()
p <-p + scale_x_date(limits = c(start.date + 20,end.date-20),breaks =1 month,labels = date_format(%b-%y))
p < - p + theme (axis.text.x = element_text(angle = 90,hjust = 1,vjust = 0.5,size = axis.font.size),
axis.text.y = element_text(size = axis.font.size) ,
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.margin = unit(c(0,0,0,0), mm),
plot.background = element_rect(fill =gray),
panel.grid = element_blank(),
panel.border = element_rect(size = line.width / 2),
axis.ticks = element_line(size = line.width / 3),
axis.ticks.length =单位(0.3,mm),
axis.ticks.margin =单位(0.2,mm))

ggsave(file =c:\\temp\\test.png,plot = p,wid th = 40,height = 15,units =mm,type =cairo-png)


解决方案

根据源代码

a>,您还需要将标签设置为NULL,

  last_plot()+ labs(x = NULL,y = NULL )

或者,设置单位(-0.5,行 code>为底部和左边距。


I'm trying to produce a png chart using ggplot2 and ggsave (with Cairo) in R. I'm having an issue with customising the theme to remove margins.

Currently I'm using:

... + theme(plot.margin=unit(c(0,0,0,0),"mm"))      

This seems to work for two out of four sides of my plot, it removes the top and right hand side margin completely, but there is still a fairly large margin on the left and bottom sides. Is there a way to completely remove these? Image below to illustrate the problem:

If a reproducible example would be useful then let me know and I'll put one together.


Edit:

library("ggplot2")
library("scales")
library("Cairo")
library("grid")

# Set chart values
line.width = 0.45
axis.font.size = 2.9

# Generate some random data
start.date <- as.Date("2011-07-01")
x.month <-seq.Date(start.date, by = "month", length.out = 24)
end.date <- max(x.month)

period.a <- rnorm(12, mean=50, sd=2)
period.b <- rnorm(12, mean=55, sd=2)

x.value <- c(period.a,period.b)

# Combine into dataframe
x.data <- data.frame(
  "Month" = x.month,
  "Value" = x.value
  )

# Build chart
p <- ggplot(data=x.data, aes(Month, Value)) + geom_line(size=line.width)
p <- p + theme_bw()
p <- p + scale_y_continuous()
p <- p + scale_x_date(limits=c(start.date+20,end.date-20), breaks = "1 month",labels = date_format("%b-%y"))
p <- p + theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5, size=axis.font.size),
                axis.text.y=element_text(size=axis.font.size),
                axis.title.x=element_blank(),
                axis.title.y=element_blank(),
                plot.margin=unit(c(0,0,0,0),"mm"),
                plot.background = element_rect(fill = "grey"),
                panel.grid=element_blank(),
                panel.border=element_rect(size=line.width/2),
                axis.ticks=element_line(size=line.width/3),
                axis.ticks.length=unit(0.3, "mm"),
                axis.ticks.margin=unit(0.2, "mm"))

ggsave(file="c:\\temp\\test.png", plot=p, width=40, height=15, units="mm", type ="cairo-png")

解决方案

According to the source code, you also need to set the labels to NULL,

last_plot() + labs(x=NULL, y=NULL)

alternatively, set unit(-0.5, "line") for the bottom and left margins.

这篇关于删除ggplot2中的绘图边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆