2013-07-22 3 views
24

g에서 ggplot2 및 ggsave (카이로 포함)를 사용하여 R에 PNG 차트를 생성하려고합니다. 테마를 사용자 정의하여 여백을 제거하는 데 문제가 있습니다.ggplot2의 플롯 여백 제거

이 내 음모의 2 ~ 4 중 측면에 대해 작동하는 것 같다
... + theme(plot.margin=unit(c(0,0,0,0),"mm"))  

, 그것은 완전히 상단과 오른쪽 마진을 제거하지만 상당히 큰 마진은 여전히 ​​존재 : 현재

내가 사용하고 있습니다 왼쪽과 아래쪽에. 이것들을 완전히 제거 할 수있는 방법이 있습니까? 재현 예 유용 할 경우 알려 주시면 함께 1 점을 추가하는 듯합니다

enter image description here

: 아래의 이미지는 문제를 설명합니다.


편집 :

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

답변

17

the source code에 따르면, 당신은 또한

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

이 대안 하단과 왼쪽 여백 unit(-0.5, "line")을 설정, NULL로 레이블을 설정해야합니다.

+4

예, 또는 하나의 레이블을 제어하기위한''xlab (NULL)''. 좀 더 보편적으로 사용되는''xlab ("")''은''axis.title.x = element_blank()''처럼 텍스트를 제거하지만 afaik는 제거하지 않습니다. 실제로, 침침한 사람의 연결을 추적하면 56 행은 다음과 같이 확인합니다 : "if (널 (null) (레이블 $ x)) 단위 (0,"lines ") else 단위 (0.5,"lines ")" – PatrickT

+0

도움 : http://stackoverflow.com/questions/22945651/how-to-remove-space-between-axis-area-plot-in-ggplot2 –